代码之家  ›  专栏  ›  技术社区  ›  Wei Lin

在这个演示中,我应该在每一列上创建一个索引还是只创建一个非聚集索引(index scanseek)

  •  2
  • Wei Lin  · 技术社区  · 8 年前

    测试DDL:

    CREATE TABLE TestTable([col1] varchar(2), [col2] varchar(2), [col3] varchar(2));   
    INSERT INTO TestTable ([col1], [col2], [col3]) VALUES ('a1', 'b1', 'c1');
    

    示例1(仅一个非聚集索引):

    Create Nonclustered Index Index_TestTable  on  TestTable ([col1], [col2], [col3]) ;
    
    select  [col2], [col3] from TestTable
    where  [col2] = 'b1'  ;
    

    结果:

    Index Scan
    

    示例2(两个非聚集索引):

    Create Nonclustered Index Index_TestTable  on  TestTable ([col1], [col2], [col3]) ;
    Create Nonclustered Index Index_TestTable2  on  TestTable ([col2], [col1], [col3]) ;
    select  [col2], [col3] from TestTable
    where  [col2] = 'b1'  ;
    

    结果:

    Index Seek
    

    问: 我应该在每列上创建一个索引还是只创建一个索引?

    select .. where col1,col2,col3 的列顺序是随机的。

    3 回复  |  直到 8 年前
        1
  •  1
  •   Hooman Bahreini    8 年前

        2
  •  1
  •   Ankur Patel    8 年前

    col2

    Create Nonclustered Index Index_TestTable  on  TestTable ([col2]) ;
    
        3
  •  1
  •   Ping    8 年前

    TestTable

    Index_TestTable2 where [col2] = 'b1' Index_TestTable here