机器情况: 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都是最低效的。因为它对子查询中的表执行了一个全表遍历。

建立合理的索引,避免扫描多余数据,避免表扫描! 几百万条数据,照样几十毫秒完成查询。

转载于:https://www.cnblogs.com/yuhanzhong/archive/2012/11/02/2751646.html

SQL查询效率:100万数据查询只需要1秒钟相关推荐

  1. mysql查询1万条数据要1秒钟_SQL查询效率:100万数据查询只需要1秒钟

    机器情况: p4: 2.4 内存: 1 G os: windows 2003 数据库: SQL Server 2000 目的: 查询性能测试,比较两种查询的性能 SQL查询效率 step by ste ...

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

    机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询性能测试,比较两种查询的性能 SQL查询效率 step by s ...

  3. mysql查询1万条数据要1秒钟_SQL查询效率:100w数据查询只需要1秒钟

    机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询性能测试,比较两种查询的性能 SQL查询效率 step by s ...

  4. 关于SQL查询效率,100w数据,查询只要1秒

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询 ...

  5. sql如何遍历几百万的表_关于SQL查询效率,100w数据,查询只要1秒

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询 ...

  6. PHP群发300万,mysql 300万数据查询500多秒如何优化

    mysql 300万数据查询500多秒怎么优化啊 本帖最后由 dz215136304 于 2013-08-15 11:33:52 编辑 linux下 mysql 300万数据查询500多秒怎么优化啊, ...

  7. 使用SQL编程创建100万条数据测试索引

    SQL编程创建100万条数据测试索引 CREATE TABLE `app_user`( `id` BIGINT (20) UNSIGNED NOT NULL AUTO_INCREMENT, `name ...

  8. 寻找热门查询,300万个查询字符串中统计最热门的10个查询

    寻找热门查询,300万个查询字符串中统计最热门的10个查询 #include #include #include struct QueryInfo{ string s; int times; Quer ...

  9. Oracle查询前100万条数据

    Oracle不支持select top语句,在Oracle中经常是用order by跟rownum select 列名1 ...列名n from ( select 列名1 ...列名n     fro ...

  10. mysql500万数据查询速度_是什么影响了数据库查询速度

    一.什么影响了数据库查询速度 1.1 影响数据库查询速度的四个因素 1.2 风险分析 QPS:Queries Per Second意思是"每秒查询率",是一台服务器每秒能够相应的查 ...

最新文章

  1. React router 的 Route 中 component 和 render 属性理解
  2. C语言:随笔8--结构体
  3. python3 面向对象(一)
  4. vc++给控件添加消息和函数
  5. TensorRT trtexec的用法
  6. DebugHook 与 ReportMemoryLeaksOnShutdown
  7. 为什么log4j的概念模型是错的--zlog的模型简介
  8. pythonidea_idea集成python插件
  9. linux练习手册,Linux操作习题集(1)
  10. idea 玩转 码云 -- idea安装码云插件
  11. VC++ 6.0 与VS2008 C++ DEBUG工具(Windows)介绍
  12. 感谢贫穷!707分考入北大,她的这篇文章看哭所有人…
  13. Python socket – network programming tutorial
  14. 【工具使用】Keil5软件使用-基础使用篇
  15. 人工智能培训:是学不好,还是教不好?3岁半的 BitTiger关闭
  16. 于仕琪老师libfacedetection最新开源代码使用测试配置
  17. MySQL基础(一)
  18. iOS:集成iAd广告
  19. 霍涛的HashMap由 数组 + 链表 组成
  20. 计算机专业应该拿的几个证书

热门文章

  1. C#、JS、HTML - 转义字符
  2. C++11nullptr
  3. 05. 从尾到头打印链表(C++版本)
  4. 注册微服务到Eureka Service
  5. c语言整数转浮点数_浮点数的秘密
  6. golang中base64编码_Rust 中的字符集编码 Rust 实践指南
  7. hdfs文件系统无法在网页上显示
  8. linux内存分段管理,Linux內存管理之分段機制
  9. redis数据结构-hash
  10. php mess,Mess.php