Sql 在连接两个表时提高查询速度
我是SQL新手,正在尝试加速存储过程中多次使用的查询。我正在尝试使用查询匹配地名。有些地方有不同长度的名称,例如,同一地址在一张表中称为“麦当劳Riccarton”,在另一张表中称为“Mac Donalds Riccarton” 我已将每个表格中的每个单词分为单独的栏,即“麦当劳,Riccarton”和“Mac,Donalds,Riccarton”。第一个词叫做第一个词,第二个词叫做第二个词,等等。。。正如您将在我的查询中看到的 通过使用直接比较或使用soundex,我试图根据松散的匹配术语进行匹配,例如soundex(mac)=soundex(macdonalds) 和soundex(riccarton)=soundex(riccarton)它们应该是相同的 查询尝试将第一个单词与所有其他列匹配,即第一个匹配第一列、第二列、第三列、第四列或最后一列。。。最后一个是最后一个单词或任何比第四个单词长的单词组 我对当前的查询很满意,只是有点慢。在一个表中匹配50个名称,另一个表中匹配600个名称时,大约需要2分钟。很明显,它必须尝试在每个“或”循环中匹配,直到它倒下,然后尝试下一个循环。。。慢慢来。如果可能的话,我希望避免使用游标。提前欢呼Sql 在连接两个表时提高查询速度,sql,gis,geospatial,spatial,Sql,Gis,Geospatial,Spatial,我是SQL新手,正在尝试加速存储过程中多次使用的查询。我正在尝试使用查询匹配地名。有些地方有不同长度的名称,例如,同一地址在一张表中称为“麦当劳Riccarton”,在另一张表中称为“Mac Donalds Riccarton” 我已将每个表格中的每个单词分为单独的栏,即“麦当劳,Riccarton”和“Mac,Donalds,Riccarton”。第一个词叫做第一个词,第二个词叫做第二个词,等等。。。正如您将在我的查询中看到的 通过使用直接比较或使用soundex,我试图根据松散的匹配术语进行
insert into
dbo.MatchedCulture_Recreation_Sports
select
loc.id,
loc.name,
cpn.cpn_id,
cpn.cpn_name
from
#NZF_CPN_Culture_Recreation_Sports cpn
inner join #Locations_Culture_Recreation_Sports loc
on
(
--where they match in string size see if the names match exactly
(
cpn.stringsize = loc.stringsize and
cpn.first = loc.first and
loc.last = cpn.last and
cpn.second = loc.second and
cpn.third = loc.third and
cpn.fourth = loc.Fourth
)
or
--or where they arent equal and the name isnt one word
(
cpn.stringsize <> loc.stringsize and
cpn.stringsize <> 1 and
loc.stringsize <>1 and
(
-- see if the first word matches anything
cpn.first = loc.first or
cpn.first = loc.second or
cpn.first = loc.third or
cpn.first = loc.fourth or
cpn.first = loc.last
)
and
-- and the last word matches anything
(
cpn.last = loc.first or
cpn.last = loc.second or
cpn.last = loc.third or
cpn.last = loc.fourth or
cpn.last = loc.last
)
and
-- and the sec matches anything
(
cpn.second = loc.first or
cpn.second = loc.second or
cpn.second = loc.third or
cpn.second = loc.fourth or
cpn.second = loc.last
)
-- or if the there are 3 words in one and 2 words in another try soundex
)
or
(
cpn.stringsize = 2 and
loc.stringsize = 3 and
cpn.stringsize <> 1 and
loc.stringsize <> 1 and
(
SOUNDEX(cpn.first) = SOUNDEX(loc.first) or
SOUNDEX(cpn.first) = SOUNDEX(loc.second) or
SOUNDEX(cpn.first) = SOUNDEX(loc.last)
)
and
(
SOUNDEX(cpn.last) = SOUNDEX(loc.first) or
SOUNDEX(cpn.last) = SOUNDEX(loc.second) or
SOUNDEX(cpn.last) = SOUNDEX(loc.last)
)
)
-- or if the there are 3 words in the other and 2 words in one try soundex
or
(
cpn.stringsize = 3 and
loc.stringsize = 2 and
cpn.stringsize <> 1 and
loc.stringsize <> 1 and
(
SOUNDEX(loc.first) = SOUNDEX(cpn.first) or
SOUNDEX(loc.first) = SOUNDEX(cpn.second) or
SOUNDEX(loc.first) = SOUNDEX(cpn.last)
)
and
(
SOUNDEX(loc.last) = SOUNDEX(cpn.first) or
SOUNDEX(loc.last) = SOUNDEX(cpn.second) or
SOUNDEX(loc.last) = SOUNDEX(cpn.last)
)
)
or
-- or if the there are more than 3 words in one and 3 words in another try soundex
(
cpn.stringsize <3 and
loc.stringsize = 3 and
cpn.stringsize <> 1 and
loc.stringsize <> 1 and
(
SOUNDEX(loc.first) = SOUNDEX(cpn.first) or
SOUNDEX(loc.first) = SOUNDEX(cpn.second) or
SOUNDEX(loc.first) = SOUNDEX(cpn.third) or
SOUNDEX(loc.first) = SOUNDEX(cpn.last)
)
and
(
SOUNDEX(loc.second) = SOUNDEX(cpn.first) or
SOUNDEX(loc.second) = SOUNDEX(cpn.second) or
SOUNDEX(loc.second) = SOUNDEX(cpn.third) or
SOUNDEX(loc.second) = SOUNDEX(cpn.last)
)
and
(
SOUNDEX(loc.last) = SOUNDEX(cpn.first) or
SOUNDEX(loc.last) = SOUNDEX(cpn.second) or
SOUNDEX(loc.last) = SOUNDEX(cpn.third) or
SOUNDEX(loc.last) = SOUNDEX(cpn.last)
)
)
or
-- or if the there are 3 words in the other and 3 words in one try soundex
(
cpn.stringsize = 3 and
loc.stringsize < 3 and
cpn.stringsize <> 1 and
loc.stringsize <> 1 and
(
SOUNDEX(cpn.first) = SOUNDEX(loc.first) or
SOUNDEX(cpn.first) = SOUNDEX(loc.second) or
SOUNDEX(cpn.first) = SOUNDEX(loc.third) or
SOUNDEX(cpn.first) = SOUNDEX(loc.last)
)
and
(
SOUNDEX(cpn.second) = SOUNDEX(loc.first) or
SOUNDEX(cpn.second) = SOUNDEX(loc.second) or
SOUNDEX(cpn.second) = SOUNDEX(loc.third) or
SOUNDEX(cpn.second) = SOUNDEX(loc.last)
)
and
(
SOUNDEX(cpn.last) = SOUNDEX(loc.first) or
SOUNDEX(cpn.last) = SOUNDEX(loc.second) or
SOUNDEX(cpn.last) = SOUNDEX(loc.third) or
SOUNDEX(cpn.last) = SOUNDEX(loc.last)
)
)
)
and
-- search within a distance
(
loc.latitude < cpn.Maxlat and
loc.latitude > cpn.Minlat and
loc.longitude < cpn.Maxlon and
loc.longitude > cpn.Minlon
)
插入到
dbo.Matched文化、娱乐、体育
挑选
地址:,
位置名称,
cpn.cpn_id,
cpn.cpn\u名称
从…起
#NZF_CPN_文化_娱乐_体育CPN
内部连接#地点#文化(娱乐)体育场所
在…上
(
--它们在字符串大小上的匹配位置,查看名称是否完全匹配
(
cpn.stringsize=位置stringsize和
cpn.first=位置第一和
loc.last=cpn.last和
cpn.second=位置秒和
cpn.third=第三和第三位置
cpn.fourth=位置4
)
或
--或者他们不相等,名字不是一个词
(
cpn.stringsize loc.stringsize和
cpn.stringsize 1和
位置1和尺寸1
(
--看看第一个单词是否匹配
cpn.first=loc.first或
cpn.first=位置秒或
cpn.first=位置三或
cpn.first=位置4或
cpn.first=loc.last
)
及
--最后一个字跟什么都匹配
(
cpn.last=位置第一或
cpn.last=位置秒或
cpn.last=第三位或
cpn.last=第四或第四位置
cpn.last=loc.last
)
及
--美国证券交易委员会什么都匹配
(
cpn.second=位置第一或
cpn.second=位置秒或
cpn.second=第三位或
cpn.second=第四位或
cpn.second=loc.last
)
--或者,如果一个单词中有3个单词,另一个单词中有2个单词,请尝试soundex
)
或
(
cpn.stringsize=2和
loc.stringsize=3和
cpn.stringsize 1和
位置1和尺寸1
(
SOUNDEX(cpn.first)=SOUNDEX(loc.first)或
SOUNDEX(cpn.first)=SOUNDEX(位置二)或
SOUNDEX(cpn.first)=SOUNDEX(loc.last)
)
及
(
SOUNDEX(cpn.last)=SOUNDEX(位置第一)或
SOUNDEX(cpn.last)=SOUNDEX(位置秒)或
SOUNDEX(cpn.last)=SOUNDEX(loc.last)
)
)
--或者,如果另一个单词中有3个单词,一个单词中有2个单词,请尝试soundex
或
(
cpn.stringsize=3和
loc.stringsize=2和
cpn.stringsize 1和
位置1和尺寸1
(
SOUNDEX(位置第一)=SOUNDEX(cpn.第一)或
SOUNDEX(位置第一)=SOUNDEX(cpn.秒)或
SOUNDEX(位置第一)=SOUNDEX(cpn.last)
)
和