表中包括  型号 批号 级别 这三个字段,表中记录如下,
0530  A  良
0530  A  次
0648  B  良
0648  B  次
0530  C  良
0648  A  次本人想得到的是如何结果
0530  A  良
0530  A  次
0530  C  良
0648  A  次
0648  B  良
0648  B  次
请问能否创建索引来加快查询速度,以及怎样创建?

解决方案 »

  1.   


    select *
    from     (
            select '良' as A 
            union all 
            select '次' as A
        ) t
    order by a/*次
    良*/因为次、良升序排序是次在前,而良在后。如果这三个字段是逻辑关键字,可以创建聚集索引:型号   升序
    批号   升序
    级别   降序
      

  2.   

    if object_id('tbTest') is not null
    drop table tbTest
    GO
    ----创建测试数据
    create table tbTest(型号 varchar(10),批号 varchar(10), 级别 varchar(10))
    insert tbTest
    select '0530',  'A',  '良' union all
    select '0530',  'A',  '次' union all
    select '0648',  'B',  '良' union all
    select '0648',  'B',  '次' union all
    select '0530',  'C',  '良' union all
    select '0648',  'A',  '次'
    ----为型号列创建聚集索引
    create CLUSTERED index idx_tbTest_型号 on tbTest(型号)
    GO----查询
    select * from tbTest order by 型号,批号----清除测试环境
    drop table tbTest/*结果
    型号       批号       级别         
    ---------- ---------- ---------- 
    0530       A          良
    0530       A          次
    0530       C          良
    0648       A          次
    0648       B          良
    0648       B          次
    */
      

  3.   

    --楼上这种处理方式有问题,如果追加顺序不同,可能查询结果就不对了。--如:if object_id('tbTest') is not null
    drop table tbTest
    GO
    ----创建测试数据
    create table tbTest(型号 varchar(10),批号 varchar(10), 级别 varchar(10))
    insert tbTest
    select '0530',  'A',  '良' union all
    select '0530',  'A',  '次' union all
    select '0648',  'B',  '次' union all
    select '0648',  'B',  '良' union all
    select '0530',  'C',  '良' union all
    select '0648',  'A',  '次'
    ----为型号列创建聚集索引
    create CLUSTERED index idx_tbTest_型号 on tbTest(型号)
    GO----查询
    select * from tbTest order by 型号,批号
    ----清除测试环境
    drop table tbTest/*结果
    型号       批号       级别         
    ---------- ---------- ---------- 
    0530       A          良
    0530       A          次
    0530       C          良
    0648       A          次
    0648       B          良
    0648       B          次
    */
      

  4.   


    --楼上这种处理方式有问题,如果追加顺序不同,可能查询结果就不对了。--所以,要指定三个字段:--如:select * from tbTest order by 型号,批号,级别 desc