2016-08-22 32 views
2

私は2つのテーブルを持っています。テーブルAでSQLクエリ - 2つのテーブル間のデータの一致

周りに1000万行、
一つが自分を更新するために一致して周り500K行

TableA (10million rows) 
Url 
------------------------------------------- 
http://www.example.com/data/tuesday-morning 
http://www.example.com/data/wednesday-evening 



TableB (500k rows) 
Keyword   Value 
---------  ---------- 
Sunday    0 
Monday    0 
Tuesday    0 
Wednesday   0 

私はTableATableB内のすべてのキーワードを検索し、一致するものを見つけたいが、そこにあるテーブルBでありValue1

私はMERGEを使用しますが、問題を解決するには少なくとも10時間かかることがあります。

キーワードは、これらの2つのテーブル間の最速のルックアップを行うための最善のSQLクエリになりますどうTableB

MERGE INTO TableB As TB 
USING (Select Url From TableA) As TA 
ON TA.Url LIKE 'http://www.example.com/data/'+TB.Keyword+'-%' 
WHEN MATCHED THEN 
UPDATE SET TB.Value=1; 

で毎日更新しているので、私は、その検索毎日作るのだろうか?

多くのおかげ

+1

'それは確かにこの行にちょうど' TableA' – gofr1

+0

が唯一の方法を使用量、クエリが遅くなります(TableAのからURLを選択) 'を取り除く - フルテキストインデックスを使用します。つまり、以下のtinka(http://stackoverflow.com/a/39080778/2746150)で提案されているアプローチを採用していますが、%like + t2.keyword + '%' 'のように' full 'テキストインデックスは高速です。 –

答えて

1

私はあなたのQは、この解決策になるかもしれません理解していれば、あなたを助ける、あなたのレコードに起こってはまず少数のデータであなたを適用いただきました!あなたは整流することができるので、あなたはIDか何かでいくつかのWHERE句を適用することができますすべてのデータを適用することができます。

-- declare table1 
declare @table1 table 
(url varchar(max)) 

insert into @table1 
values 
('http://www.example.com/data/tuesday-morning'), 
('http://www.example.com/data/tuesday-morning'), 
('http://www.example.com/data/noday-morning') 


-- declare table2 
declare @table2 table 
(keyword varchar(33), val int) 

insert into @table2 
values 
('monday',0), 
('tuesday',0) 

-- select 
select * from 
@table1 t1 join 
@table2 t2 on t1.url like '%'+t2.keyword+'%' 

-- update 
update 
@table2 
set val =1 
from 
@table1 t1 join 
@table2 t2 on t1.url like '%'+t2.keyword+'%' 

    -- select again 
select * from 
@table1 t1 join 
@table2 t2 on t1.url like '%'+t2.keyword+'%' 
関連する問題