2014-11-20 2 views
0

SQL에 익숙하지 않고 저장 프로 시저에서 여러 번 사용 된 쿼리의 속도를 높이려고합니다. 나는 쿼리를 사용하여 장소의 이름을 일치 시키려고 노력하고있다. 장소의 일부는 다른 길이의 이름을 가지고 있습니다. 예를 들어 같은 주소는 한 테이블에서 'McDonalds Riccarton'이라고하고 다른 테이블에서는 'Mac Donalds Riccarton'이라고 부를 수 있습니다.두 테이블을 결합 할 때 쿼리 속도가 향상됨

각 테이블의 단어를 각각 'McDonalds, 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

SQL Server Management Studio에 실행 계획 Icon of execution plan을 시도 할 수 있습니다. SQL에서 색인 작성 스크립트를 사용하여 누락 된 색인 (색인이 필요한 경우) 목록을 알려줍니다. 인덱스를 생성 한 후 쿼리가 더 빠르게 실행됩니다.

쿼리 속도를 높이는 한 가지 방법은 SQL Server입니다.

+0

건배 저는 입력 변수가있는 저장 프로 시저가 작동하여 코드에 오류가 있다고 (정상적으로 존재하지 않음) 또는 저장 프로 시저로 실행할 때 문제가 있음을 알고 있습니다. 분명히 SP에 대한 색인을 작성할 필요가 없습니다. –

관련 문제