我有一个表,用于捕获用户正在运行的主机平台。该表的定义很简单:
IF OBJECT_ID('[Auth].[ActivityPlatform]', 'U') IS NULL
BEGIN
CREATE TABLE [Auth].[ActivityPlatform] (
[ActivityPlatformId] [tinyint] IDENTITY(1,1) NOT NULL
,[ActivityPlatformName] [varchar](32) NOT NULL
,CONSTRAINT [PK_ActivityPlatform] PRIMARY KEY CLUSTERED ([ActivityPlatformId] ASC)
,CONSTRAINT [UQ_ActivityPlatform_ActivityPlatformName] UNIQUE NONCLUSTERED ([ActivityPlatformName] ASC)
) ON [Auth];
END;
GO
Run Code Online (Sandbox Code Playgroud)
它存储的数据是基于 JavaScript 方法枚举的,该方法使用来自浏览器的信息(我不知道更多,但可以在需要时找到):
但是,当我在SELECT没有显式的情况下执行基本操作ORDER BY时,执行计划显示它正在使用UNIQUE NONCLUSTERED索引而不是CLUSTERED索引进行排序。
SELECT * FROM [Auth].[ActivityPlatform]
Run Code Online (Sandbox Code Playgroud)
当显式指定 时ORDER BY,它正确地按 排序ActivityPlatformId。
SELECT * FROM [Auth].[ActivityPlatform] ORDER BY [ActivityPlatformId]
Run Code Online (Sandbox Code Playgroud)
DBCC SHOWCONTIG('[Auth].[ActivityPlatform]') WITH ALL_LEVELS, TABLERESULTS 显示没有表碎片。
我错过了什么可能导致这个?我一直认为该表是在聚集索引上创建的,它应该自动隐式地对其进行排序,而无需指定ORDER BY …
performance index sql-server execution-plan query-performance
create table t
(
id int,
id1 int
)
create index Example_Index
On t(id,id1)
create index Example1_Index
On t(id1,id)
Run Code Online (Sandbox Code Playgroud)
insert into t(id, id1)values(1, 100)
insert into t(id, id1)values(1, 101)
insert into t(id, id1)values(2, 103)
insert into t(id, id1)values(1, 104)
insert into t(id, id1)values(3, 105)
insert into t(id, id1)values(1, 106)
insert into t(id, id1)values(2, 107)
insert into t(id, id1)values(3, 108)
Run Code Online (Sandbox Code Playgroud)
SQL查询- select * from t Where id = 107 -用途Example_Index
SQL查询- select * from t Where …
performance index sql-server-2005 sql-server-2008 sql-server query-performance