45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:select子句默认排序方式介绍

select子句默认排序方式介绍

2016-08-31 11:54:15 来源:www.45fan.com 【

select子句默认排序方式介绍

/* create by 4752234120070116
数据库分页选择显示的方式有很多种;其中比较常用的一种是使用select top 子句顺序得到结果的子集;
在这个过程中需要借助orderby对结果进行排序;当然也可以在表上建立索引后对执行默认排序规则。
但使用默认排序规则在某些情况下可能会出现问题;例如,在一个表上有一个clustered的primarykey的时候,
其select子句默认的返回方式往往是根据primary key的排序结果;但如果同时在该表上还有一个其他的索引
而并且该索引包含了其他大部分或者全部字段;则select的默认排序结果可能会根据另外的那个索引;而不是我们想象中 的按照其物理存储顺序排序。

*/
if object_id(N'T1') is not null
drop table t1

create table t1(
id int not null identity(1,1),
F1 varchar(20),
F2 varchar(20),
F3 varchar(20)
)
go


alter table t1
add constraint pf_t1_id primary key clustered (id) on [primary]

create index ix_t1_f1 on t1(f1,f2,f3) on [primary]

--插入测试数据
insert into t1 values(1,1,6)
insert into t1 values(2,4,5)
insert into t1 values(7,3,4)
insert into t1 values(4,21,3)
insert into t1 values(5,3,2)
insert into t1 values(6,5,1)


insert into t1
select top 10 abs(checksum(newid()) % 1000) as F1,
abs(checksum(newid()) % 1000) as F2,
abs(checksum(newid()) % 1000) as F3
from syscolumns


--测试结果

--查看默认排序结果
select * from t1

--返回的结果集为
id f1 f2 f3
------------------------------
1116
16110999866
2245
12290376261
7375722854
44213
5532
6651
1463050994
11693633358
3734
8772844484
13892807989
9907233671
15922798333
10935807420

--查看默认排序结果
select * from t1 order by ID

--返回的结果集为
id f1 f2 f3
------------------------------
1116
2245
3734
44213
5532
6651
7375722854
8772844484
9907233671
10935807420
11693633358
12290376261
13892807989
1463050994
15922798333
16110999866

 

本文地址:http://www.45fan.com/a/question/70178.html
Tags: 默认 排序 Select
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部