有一表:出勤記錄
其中有字段:年月、日期、工號、姓名、部門、級別、上下班刷卡時間等
現有如下SQL
select * from 出勤記錄 where 年月='2009/09' order by 部門,姓名,日期表中共有記錄50W條左右,每月大概5W左右的記錄,查詢是非常的慢,現在想通過聚集索引來優化,
我的索引該建在哪些字段上呢?
1.建在年月上?
2.建在年月、部門、姓名、日期?
3.或是其它?另如何清除查詢分析器中的緩存?
就是在第一次招待某SQL語句後,再次執行,那麼第二次的時間明顯快過第一次,這樣對測試效率
有影響,有沒有辦法去掉這影響?謝謝!

解决方案 »

  1.   

    只有管理员才有权限使用以下两个命令:
    --从缓冲池中删除所有清除缓冲区
    DBCC DROPCLEANBUFFERS--从过程高速缓存中删除所有元素
    DBCC FREEPROCCACHE
      

  2.   

    select * from 出勤記錄 where 年月='2009/09' order by 部門,姓名,日期这个语句索引建立在年月上 一般来说建立在连接字段上
      

  3.   

    1:建在年月上
    2:清除数据和执行计划缓存:  DBCC DROPCLEANBUFFERS
      DBCC FREEPROCCACHE
      

  4.   

    具体可以看看
    http://blog.csdn.net/fredrickhu/archive/2009/10/18/4693918.aspx
    索引的使用
      

  5.   

     1.建在年月上?
     2.建在年月、部門、姓名、日期?两个都试一下,然后贴一下消息里的io读。create index ix_01 on 出勤記錄(年月)
    go
    set statistics io on
    select * from 出勤記錄 with(index=ix_01) where 年月='2009/09' order by 部門,姓名,日期
    create index ix_02 on 出勤記錄(年月,部門,姓名,日期)
    go
    set statistics io on
    select * from 出勤記錄 with(index=ix_02) where 年月='2009/09' order by 部門,姓名,日期
      

  6.   

    這是建在年月上的
    第一次
    (47690 row(s) affected)
    Table 'pe21'. Scan count 2, logical reads 4802, physical reads 61, read-ahead reads 4364.第二次
    (47690 row(s) affected)
    Table 'pe21'. Scan count 2, logical reads 4786, physical reads 0, read-ahead reads 0.這是建在年月、部門、姓名、日期上的
    第一次
    (47690 row(s) affected)
    Table 'pe21'. Scan count 2, logical reads 4803, physical reads 61, read-ahead reads 4799.
    第二次
    (47690 row(s) affected)
    Table 'pe21'. Scan count 2, logical reads 4787, physical reads 0, read-ahead reads 0.
      

  7.   

    To: perfectaction
    上樓是我這讀取的I/O消自息,請幫忙看看