Why is this an Index Scan and not a Index Seek?

后端 未结 5 994
南方客
南方客 2020-12-24 14:59

Here\'s the query:

SELECT      top 100 a.LocationId, b.SearchQuery, b.SearchRank
FROM        dbo.Locations a
INNER JOIN  dbo.LocationCache b ON a.LocationId          


        
5条回答
  •  生来不讨喜
    2020-12-24 15:18

    I did a quick test and came up with the following

    CREATE TABLE #Locations
    (LocationID INT NOT NULL ,
    CountryID INT NOT NULL ,
    [Type] INT NOT NULL 
    CONSTRAINT PK_Locations
            PRIMARY KEY CLUSTERED ( LocationID ASC )
    )
    
    CREATE NONCLUSTERED INDEX [LocationsIndex01] ON #Locations
    (
        CountryID ASC,
        [Type] ASC
    )
    
    CREATE TABLE #LocationCache
    (LocationID INT NOT NULL ,
    SearchQuery VARCHAR(50) NULL ,
    SearchRank INT NOT NULL 
    CONSTRAINT PK_LocationCache
            PRIMARY KEY CLUSTERED ( LocationID ASC )
    
    )
    
    CREATE NONCLUSTERED INDEX [LocationCacheIndex01] ON #LocationCache
    (
        LocationID ASC,
        SearchQuery ASC,
        SearchRank ASC
    )
    
    INSERT INTO #Locations
    SELECT 1,1,1 UNION
    SELECT 2,1,4 UNION
    SELECT 3,2,7 UNION
    SELECT 4,2,7 UNION
    SELECT 5,1,1 UNION
    SELECT 6,1,4 UNION
    SELECT 7,2,7 UNION
    SELECT 8,2,7 --UNION
    
    INSERT INTO #LocationCache
    SELECT 4,'BlahA',10 UNION
    SELECT 3,'BlahB',9 UNION
    SELECT 2,'BlahC',8 UNION
    SELECT 1,'BlahD',7 UNION
    SELECT 8,'BlahE',6 UNION
    SELECT 7,'BlahF',5 UNION
    SELECT 6,'BlahG',4 UNION
    SELECT 5,'BlahH',3 --UNION
    
    SELECT * FROM #Locations
    SELECT * FROM #LocationCache
    
    SELECT      top 3 a.LocationId, b.SearchQuery, b.SearchRank
    FROM        #Locations a
    INNER JOIN  #LocationCache b ON a.LocationId = b.LocationId
    WHERE       a.CountryId = 2
    AND         a.[Type] = 7
    
    DROP TABLE #Locations
    DROP TABLE #LocationCache
    

    For me, the query plan shows to seeks with a nested loop inner join. If you run this, do you get both seeks? If you do, then do a test on your system and create a copy of your Locations and LocationCache table and call them say Locations2 and LocationCache2 with all the indexes and copy your data into them. Then try your query hitting the new tables?

提交回复
热议问题