SELECT 没有 ORDER 不按聚集索引排序
SELECT without ORDER not Sorting on Clustered Index
对此有点困惑。我的印象是,如果没有给出 ORDER BY,SELECT 应该默认为 Clustered Index 进行排序。
但是如果我 运行 下面,它会使用非聚集索引。
DROP TABLE TEST_TABLE
if NOT EXISTS(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TEST_TABLE')
BEGIN
CREATE TABLE TEST_TABLE (
[ID] int NOT NULL IDENTITY(1,1),
[col1] int NOT NULL,
[col2] int NOT NULL
CONSTRAINT [PK_TEST_TABLE] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
END
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TEST_TABLE]') AND name = N'TEST_TABLE_cols')
DROP INDEX [TEST_TABLE_cols] ON [dbo].[TEST_TABLE]
CREATE UNIQUE NONCLUSTERED INDEX [TEST_TABLE_cols] ON [dbo].[TEST_TABLE]
( [col1] ASC,
[col2] ASC
)WITH (FILLFACTOR = 60) ON [PRIMARY];
GO
--GO
EXEC ('insert into test_table select 1, 0')
EXEC ('insert into test_table select 2, 0')
EXEC ('insert into test_table select 10, 0')
EXEC ('insert into test_table select 9, 0')
EXEC ('insert into test_table select 8, 0')
EXEC ('insert into test_table select 6, 0')
EXEC ('insert into test_table select 7, 0')
EXEC ('insert into test_table select 5, 0')
EXEC ('insert into test_table select 3, 0')
EXEC ('insert into test_table select 4, 0')
select * from test_table
结果如下...
ID col1 col2
----------- ----------- -----------
1 1 0
2 2 0
9 3 0
10 4 0
8 5 0
6 6 0
7 7 0
5 8 0
4 9 0
3 10 0
如果有人能解释一下,我们将不胜感激!
I was under the impression that if no ORDER BY is given, a SELECT should default to the Clustered Index for sorting.
虽然在实践中可能会出现这种情况(如果行是按顺序简单地从存储中读取的,那就是你得到的),但在任何地方都没有这样的保证。查询或查询计划中的小改动很容易改变事情。
如果不指定顺序,则结果无顺序。
我无法解释您为什么会看到这个,但我知道除非按照以下 link:
指定 order by 子句,否则无法保证任何顺序
对此有点困惑。我的印象是,如果没有给出 ORDER BY,SELECT 应该默认为 Clustered Index 进行排序。
但是如果我 运行 下面,它会使用非聚集索引。
DROP TABLE TEST_TABLE
if NOT EXISTS(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TEST_TABLE')
BEGIN
CREATE TABLE TEST_TABLE (
[ID] int NOT NULL IDENTITY(1,1),
[col1] int NOT NULL,
[col2] int NOT NULL
CONSTRAINT [PK_TEST_TABLE] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
END
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TEST_TABLE]') AND name = N'TEST_TABLE_cols')
DROP INDEX [TEST_TABLE_cols] ON [dbo].[TEST_TABLE]
CREATE UNIQUE NONCLUSTERED INDEX [TEST_TABLE_cols] ON [dbo].[TEST_TABLE]
( [col1] ASC,
[col2] ASC
)WITH (FILLFACTOR = 60) ON [PRIMARY];
GO
--GO
EXEC ('insert into test_table select 1, 0')
EXEC ('insert into test_table select 2, 0')
EXEC ('insert into test_table select 10, 0')
EXEC ('insert into test_table select 9, 0')
EXEC ('insert into test_table select 8, 0')
EXEC ('insert into test_table select 6, 0')
EXEC ('insert into test_table select 7, 0')
EXEC ('insert into test_table select 5, 0')
EXEC ('insert into test_table select 3, 0')
EXEC ('insert into test_table select 4, 0')
select * from test_table
结果如下...
ID col1 col2
----------- ----------- -----------
1 1 0
2 2 0
9 3 0
10 4 0
8 5 0
6 6 0
7 7 0
5 8 0
4 9 0
3 10 0
如果有人能解释一下,我们将不胜感激!
I was under the impression that if no ORDER BY is given, a SELECT should default to the Clustered Index for sorting.
虽然在实践中可能会出现这种情况(如果行是按顺序简单地从存储中读取的,那就是你得到的),但在任何地方都没有这样的保证。查询或查询计划中的小改动很容易改变事情。
如果不指定顺序,则结果无顺序。
我无法解释您为什么会看到这个,但我知道除非按照以下 link:
指定 order by 子句,否则无法保证任何顺序