SQL查询效率:100w数据查询只需要1秒钟

作者:晨夕 时间:2008-12-09 14:36:00 

关于SQL查询效率,100w数据,查询只要1秒,与您分享:

机器情况:

p4: 2.4

内存: 1 G

os: windows 2003

数据库:

SQL Server 2000

目的: 查询性能测试,比较两种查询的性能


SQL查询效率 step by step

-- setp 1.

-- 建表

create table t_userinfo

(

userid int identity(1,1) primary key nonclustered,

nick varchar(50) not null default '',

classid int not null default 0,

writetime datetime not null default getdate()

)

go

-- 建索引

create clustered index ix_userinfo_classid on t_userinfo(classid)

go


-- step 2.


declare @i int

declare @k int

declare @nick varchar(10)

set @i = 1

while @i<1000000

begin

set @k = @i % 10

set @nick = convert(varchar,@i)

insert into t_userinfo(nick,classid,writetime) values(@nick,@k,getdate())

set @i = @i + 1

end

-- 耗时 08:27 ,需要耐心等待

-- step 3.

select top 20 userid,nick,classid,writetime from t_userinfo

where userid not in

(

select top 900000 userid from t_userinfo order by userid asc

)

-- 耗时 8 秒 ,够长的

-- step 4.

select a.userid,b.nick,b.classid,b.writetime from

(

select top 20 a.userid from

(

select top 900020 userid from t_userinfo order by userid asc

) a order by a.userid desc

) a inner join t_userinfo b on a.userid = b.userid

order by a.userid asc

-- 耗时 1 秒,太快了吧,不可以思议

-- step 5 where 查询

select top 20 userid,nick,classid,writetime from t_userinfo

where classid = 1 and userid not in

(

select top 90000 userid from t_userinfo

where classid = 1

order by userid asc

)

-- 耗时 2 秒


-- step 6 where 查询

select a.userid,b.nick,b.classid,b.writetime from

(

select top 20 a.userid from

(

select top 90000 userid from t_userinfo

where classid = 1

order by userid asc

) a order by a.userid desc

) a inner join t_userinfo b on a.userid = b.userid

order by a.userid asc

-- 查询分析器显示不到 1 秒.

查询效率分析:

子查询为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询。在这种情况下可以考虑用联接查询来取代。

如果要用子查询,那就用EXISTS替代IN、用NOT EXISTS替代NOT IN。因为EXISTS引入的子查询只是测试是否存在符合子查询中指定条件的行,效率较高。无论在哪种情况下,NOT IN都是最低效的。因为它对子查询中的表执行了一个全表遍历。

建立合理的索引,避免扫描多余数据,避免表扫描!

几百万条数据,照样几十毫秒完成查询。

标签:
0
投稿

猜你喜欢

  • MySQL 数值类型概述int smallint tinyint

    2010-11-02 11:46:00
  • 使用SQL语句快速获取SQL Server数据字典

    2009-01-08 16:31:00
  • 按键标示的设计体验

    2008-08-27 12:06:00
  • SQL Server数据库服务器高性能设置

    2010-11-25 16:00:00
  • IE7下动态创建Iframe时,去除边框的办法

    2009-01-19 13:56:00
  • 网页代码中键盘操作相关标签教程

    2010-03-18 15:56:00
  • 数据库连接字符串的常见问题和解决方法

    2008-11-28 15:16:00
  • 在IE8中继续使用滤镜及IE8的一些CSS扩展属性

    2009-02-21 11:18:00
  • 如何把ACCESS转成SQL数据库

    2007-08-11 13:51:00
  • 深入了解MySQL的数据类型以及建库策略

    2008-12-17 16:16:00
  • 在MySQL数据库中如何修改密码及访问限制

    2008-11-27 16:36:00
  • JavaScript程序编码规范[译]

    2009-07-20 17:54:00
  • 2009年情人节网站logo欣赏

    2009-02-15 12:13:00
  • 完全讲解 使用MSCS建立SQL Server集群

    2009-01-19 14:10:00
  • 讲解使用SQL Server升级顾问的详细步骤

    2009-01-04 14:14:00
  • ASP实现上传图片自动压缩图片大小

    2010-01-25 12:47:00
  • 基于Oracle的高性能动态SQL程序开发

    2010-07-20 13:01:00
  • 给在DreamWeaver编写CSS的人一些习惯建议

    2007-12-25 12:10:00
  • YUI3新特性学习

    2012-04-26 16:25:20
  • 关于设计规范

    2008-06-02 13:10:00
  • asp之家 网络编程 m.aspxhome.com