Sql server SQL查询-在两个表之间匹配数据
我有两张桌子 在表A中,大约有1000万行, 表B中大约有500k行Sql server SQL查询-在两个表之间匹配数据,sql-server,Sql Server,我有两张桌子 在表A中,大约有1000万行, 表B中大约有500k行 TableA (10million rows) Url ------------------------------------------- http://www.example.com/data/tuesday-morning http://www.example.com/data/wednesday-evening TableB (500k rows) Keyword Value --------
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
我想在TableA中搜索TableB中的所有关键字,并找到匹配项,其中一个匹配项将其值更新为1
我使用MERGE,但问题是进行搜索至少需要10个小时
我每天都会进行搜索,因为表B中的关键字每天都在更新
在这两个表之间进行最快查找的最佳SQL查询是什么
非常感谢如果我了解您的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+'%'
摆脱“从表格中选择Url”肯定会降低查询速度,使用表格A和此行是唯一的方法-使用全文索引。也就是说,采用tinka在下面提出的方法,但您必须用特定于全文索引的语言构造替换“%”+t2.keyword+'%”,这要快得多。
-- 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+'%'