2016-08-22 63 views
2

我有2个表,即;SQL查询 - 两个表之间的数据匹配

TableA中有大约1000万行,
TableB中大约50万行

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所有关键字,并找到匹配,其中一个匹配更新有其Value1

我使用MERGE,但问题是至少需要10个小时才能进行搜索。

我会作出这样的搜索每天,由于这些关键字在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; 

每日更新什么将是最好的SQL查询,使这两个表之间最快的查找?

非常感谢

+1

摆脱'(选择URL从表A)'这是肯定减慢您的查询,只使用'TableA' – gofr1

+0

随着该行数量的唯一方法 - 使用全文索引。也就是 - 采用以下由tinka(http://stackoverflow.com/a/39080778/2746150)提出的方法,但是您必须用特定于全文的语言结构来替换'like'%'+ t2.keyword +'%''文本索引更快。 –

回答

1

如果我理解您的问与答可能是该解决方案将帮助您,您可以通过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+'%'