sql - Storing data in two CSV strings vs two db tables for fastest comparison -


the scenario have 2 lists:

a: 23,45,g5,33

b: 11,12,45,g9

we want fastest mechanism in sql server see if of values b available in a, in example 45 in must return true.

the solution should describe way store lists (csv, tables etc.) , comparison mechanism.

each list relatively small (average 10 values in each) comparison being made many many times (very few writes, many many reads)

if stuck delimited string, consider following:

example:

declare @yourtable table ([cola] varchar(50),[colb] varchar(50)) insert @yourtable values   ('23,45,g5,33' ,'11,12,45,g9') ,('no,match'    ,'found,here')   select *   @yourtable  cross apply (                 select match=isnull(sum(1),0)                   [dbo].[udf-str-parse-8k](cola,',') b1                  join  [dbo].[udf-str-parse-8k](colb,',') b2 on b1.retval=b2.retval              ) b 

returns

cola          colb          match 23,45,g5,33   11,12,45,g9   1 no,match      found,here    0 

the udf if interested

create function [dbo].[udf-str-parse-8k] (@string varchar(max),@delimiter varchar(25)) returns table  return (         cte1(n)   (select 1 (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n(n)),            cte2(n)   (select top (isnull(datalength(@string),0)) row_number() on (order (select null)) (select n=1 cte1 a,cte1 b,cte1 c,cte1 d) ),            cte3(n)   (select 1 union select t.n+datalength(@delimiter) cte2 t substring(@string,t.n,datalength(@delimiter)) = @delimiter),            cte4(n,l) (select s.n,isnull(nullif(charindex(@delimiter,@string,s.n),0)-s.n,8000) cte3 s)      select retseq = row_number() on (order a.n)           ,retval = ltrim(rtrim(substring(@string, a.n, a.l)))       cte4 ); --orginal source http://www.sqlservercentral.com/articles/tally+table/72993/ --select * [dbo].[udf-str-parse-8k]('dog,cat,house,car',',') --select * [dbo].[udf-str-parse-8k]('john||cappelletti||was||here','||') 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -