在創建主鍵或其它索引時,SQL Server總是自動將字段的順序設置為升序排列;升序是默認設置,是為了保持與 SQL Server 早期版本的兼容性。建索引時索引列的順序應該按照常用查詢中的排序方式排序。
我們做個試驗創建一個表,其中主鍵上的聚集索引按照id倒敘排列,然後分別倒敘順序select數據,比較select的時間:
+測試代碼
if object_id('test_indexorder','U') is not null
begin
truncate table test_indexorder
drop table test_indexorder
end
go
create table test_indexorder
(
id int identity(1,1) not null,
name varchar(20) not null,
content varchar(50) not null,
co1 varchar(50),
co2 varchar(50),
co3 varchar(50),
co4 varchar(50),
co5 varchar(50),
constraint pk_testorder primary key clustered(
id desc
)
)
go
--insert 1000000 條數據
set nocount on;
declare @t datetime;
set @t = getdate();
DECLARE @cn int;
set @cn = 1000000;
while(@cn > 0)
begin
insert into test_indexorder(name,content,co1,co2,co3,co4,co5)
VALUES(
'name' + cast(@cn as varchar(10)),
cast(newid() as varchar(50)),
cast(newid() as varchar(50)),
cast(newid() as varchar(50)),
cast(newid() as varchar(50)),
cast(newid() as varchar(50)),
cast(newid() as varchar(50)));
set @cn = @cn -1;
end
print '插入時間(毫秒):';
print datediff(millisecond,@t,getdate());
set nocount off;
GO
checkpoint
dbcc freeproccache
dbcc dropcleanbuffers
GO
go
set nocount on;
declare @t datetime;
set @t = getdate();
with t_rn as (
select *,rn = ROW_NUMBER() OVER (ORDER BY id desc) FROM test_indexorder
)
SELECT id,name,content,co1,co2,co3,co4,co5 from t_rn WHERE rn between 19007 and 19057;
print '查詢時間(毫秒)'
print datediff(millisecond,@t,getdate())
set @t = getdate();
with t_rn as (
select *,rn = ROW_NUMBER() OVER (ORDER BY id asc) FROM test_indexorder
)
SELECT id,name,content,co1,co2,co3,co4,co5 from t_rn WHERE rn between 17007 and 17057;
print '查詢時間(毫秒)'
print datediff(millisecond,@t,getdate())
set nocount off;
以下是查詢時間結果
查詢時間(毫秒)
393
查詢時間(毫秒)
606
按照和索引相同順序從100萬條數據中取50條時需要393毫秒,相反順序時需要606毫秒。造成的性能影響還是挺大的。
結論:
在建索引時要考慮常用查詢的排序方式,在建主鍵時要特別注意,因為SQL Server會自動按照升序來建,這時候如果您的查詢多數用主鍵列倒敘排列,記得要修改一下默認的設置。