我持有上出現了一次事件,如史表:的SQL Server索引設計
EventType | OccurredOn | Other.. | Data..
--------------------------------------------
1 | 2014/10/02 09:00 | foo | bar
1 | 2014/10/02 10:00 | foo | bar
2 | 2014/10/03 09:00 | foo | bar
1 | 2014/10/04 09:00 | foo | bar
3 | 2014/10/05 09:00 | foo | bar
2 | 2014/10/05 09:00 | foo | bar
4 | 2014/10/05 09:00 | foo | bar
我所有的問題在該表上會的形式爲:
select [OccurredOn], [OtherData]
from [Data]
where [EventType] = @type and [OccurredOn] between @start and @end
也許與grouping
和選擇聚合數據。
行會按時間順序幾乎總是被插入,當他們被插入了時間順序,將OccurredOn
應該是合理的「近期」 - 即不太遠之前已經插入了「最新」行
應該如何我把這個表格索引爲有效選擇?
大概我的PK應該是聚簇的,並且包括OccurredOn
它,但是沒有天然的唯一鍵,所以大概我需要一個autoincrement int
的列呢?
更新
行而言,該表將有幾百萬,可能是數以百萬計10S
我使用SQL Server 208R2
首先,[不要使用'BETWEEN'進行日期範圍查詢](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the-devil-have-in-common.aspx) 。 – 2014-10-02 16:51:45
感謝protip – 2014-10-02 16:53:56