建索引(尤其是主键)时请注意索引列顺序
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 ;
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 ;