Query Index for Dictionary Based Queries

后端 未结 3 677
孤城傲影
孤城傲影 2020-12-21 18:21

What would be the most efficient way to query and index for the following:

SELECT * Persons.LastName A-D
SELECT * Persons.LastName E-L
SELECT * Persons.LastN         


        
3条回答
  •  醉梦人生
    2020-12-21 18:48

    You predicate is sargable.

    If you run this query on an indexed field:

    SELECT  *
    FROM    persons
    WHERE   last_name >= 'a'
            AND last_name < 'e'
    

    it produces the following plan:

      |--Nested Loops(Inner Join, OUTER REFERENCES:([MYDB].[dbo].[PERSONS].[ID]) OPTIMIZED)
           |--Index Seek(OBJECT:([MYDB].[dbo].[PERSONS].[IX_PERSONS_LAST_NAME]), SEEK:([MYDB].[dbo].[PERSONS].[LAST_NAME] >= 'a' AND [MYDB].[dbo].[PERSONS].[LAST_NAME] < 'E'),  WHERE:([MYDB].[dbo].[PERSONS].[LAST_NAME] like '[a-d]%') ORDERED FORWARD)
           |--Clustered Index Seek(OBJECT:([MYDB].[dbo].[PERSONS].[IX_PERSONS_LAST_NAME]), SEEK:([MYDB].[dbo].[PERSONS].[ID]=[MYDB].[dbo].[PERSONS].[ID]) LOOKUP ORDERED FORWARD)
    

    which is equivalent to running this query:

    SELECT  *
    FROM    persons
    WHERE   last_name >= 'a'
            AND last_name < 'e'
    

提交回复
热议问题