连接两个表时提高查询速度

时间:2014-11-20 02:32:36

标签: sql gis geospatial spatial

我是SQL新手并尝试加快在存储过程中多次使用的查询。我试图使用查询匹配地方的名称。一些地方有不同长度的名称,例如同一地址可能被称为麦当劳里卡顿'在一张桌子和Mac Donalds Riccarton'在另一个。

我已将每个表格中的每个单词分成单独的列,即麦当劳,里卡顿'和Mac,Donalds,Riccarton'。这些首先被称为第一个单词,第二个被称为第二个单词等等......正如您将在我的查询中看到的那样

通过使用直接比较或使用soundex,我试图根据松散的匹配术语匹配它们,例如soundex(mac)= soundex(macdonalds)           和soundex(riccarton)= soundex(riccarton)他们应该是一样的。

查询尝试将第一个单词与所有其他列匹配,即第一个匹配第一个或第二个或第三个或第四个或最后一个...最后一个是最后一个单词或任何长于第四个单词的单词组名字..

我对我当前的查询感到满意,只是它有点慢。当匹配表中包含50个名称的表和600个名称时,大约需要2分钟。它显然必须尝试匹配每个'或''循环直到它翻倒,然后尝试下一个循环...使它变慢。如果可能的话我想避免使用游标。提前干杯。

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 
                                    )

1 个答案:

答案 0 :(得分:1)

您可以在SQL Server Management Studio中尝试执行计划Icon of execution plan。它让你知道缺少索引(如果它需要索引)列表与sql中的索引创建脚本。创建索引后,查询将比之前运行得更快。

这是加快SQL Server中查询速度的一种方法。