`
yanjie_1206
  • 浏览: 7604 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

MS-SQL分页not in 方法改进之使用Max\Min

阅读更多

先看下表中共有多少条数据:

一百二十多万条,呵呵。

sql语句:

复制代码
declare @d datetime
set @d = getdate()
select top 10 ID,U_Name,U_Age,U_Sex,U_Address from Test1 where ID not in (select top 9990 ID from Test1 order by ID) order by ID 
select [not in方法升序分页执行花费时间(毫秒)]=datediff(ms,@d,getdate()) 

declare @s datetime
set @s = getdate()
select top 10 ID,U_Name,U_Age,U_Sex,U_Address from Test1 where ID >=(select max(ID) from (select top 9991 ID from test1 order by ID) as T) order by ID
select [Max方法升序分页执行花费时间(毫秒)]=datediff(ms,@s,getdate()) 

declare @t datetime
set @t = getdate()
select top 10 ID,U_Name,U_Age,U_Sex,U_Address from Test1 where ID not in (select top 9990 ID from Test1 order by ID desc) order by ID desc 
select [not in方法降序分页执行花费时间(毫秒)]=datediff(ms,@t,getdate()) 

declare @q datetime
set @q = getdate()
select top 10 ID,U_Name,U_Age,U_Sex,U_Address from Test1 where ID <=(select min(ID) from (select top 9991 ID from Test1 order by ID desc) as T) order by ID desc
select [Min方法降序分页执行花费时间(毫秒)]=datediff(ms,@q,getdate())
复制代码

分页效率一目了然!

两种方法的不同点之一就是计算第二个top中的值

not in 中计算公式为:

pgSize * (pgNo - 1)

max\min中计算公式为:

((pgNo-1)*pgSize+1)

pgNo为当前第几页,pgSize为每页几条数据。

分享到:
评论

相关推荐

    asp.net c#使用min-max的高效分页方法

    自己编写的min-max分页方法。 据说查询效率非常高~~

    经典SQL语句大全

    9、说明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’) 10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * ...

    sql经典语句一部分

    9、说明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’) 10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * ...

    数据库操作语句大全(sql)

    9、说明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’) 10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * ...

    sql 2000自定义分页存储过程

    set @SQL = 'select top ' + Cast(@PageSize as varchar) + ' ' + @Fields + ' from ' + @TableName + ' where ' + @OrderField + '&lt;(select min(' + @OrderField + ') from (select top ' + Cast(@PageSize*(@...

    LINQ_to_SQL语法及实例大全

    LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg 9 1.简单形式: 9 2.带条件形 式: 9 1.简单形式: 10 2.映射形式: 10 3.元素 : 11 1.简单形式: 11 2.映射形式: 11 3.元素: 11 1.简单形式: 12 2.映射形式: 12 3....

    2009达内SQL学习笔记

    NOT 与 IN 在一起使用时,NOT 是找出与条件列表不匹配的行。 IN 列表里有 NULL 时不处理,不影响结果;用 NOT IN 时,有 NULL 则出错,必须排除空值再运算。 in :选择列表的条件 使用IN操作符的优点: 在长的...

    精髓Oralcle讲课笔记

    ------------------------/组函数(共5个):将多个条件组合到一起最后只产生一个数据------min() max() avg() sum() count()----------------------------/ 51、select count(*) from emp; --求出表中一共有多少条...

    经典全面的SQL语句大全

    Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)  20、说明:列出数据库里所有的表名 select name from sysobjects where type='U' 21、说明:列出表里的所有的 ...

    超实用sql语句

    9、说明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’) 10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * ...

    SQL培训第一期

    count、sum、avg、max、min 1.8.2 case when 1.8.2.1 语法 select t.uuid, t.score, case when t.score &gt; 90 then '优秀' when t.score &gt; 60 then '及格' else '不及格' end from exam_user_exam t 1.8.3 ...

    深入浅出Mysql优化性能提升.txt

    22-Max Min非常规优化技巧.wmv 23-Count优化小技巧.wmv 24-巧用变量减少查询.wmv 25-MySQL主从集群配置.wmv 26-Binlog-Format的区别.wmv 27-主主复制.wmv 28-主主复制时的主键冲突解决.wmv 29-被动主主复制.wmv 30-...

    mysql基本语法,增删改查大全总结,以及常用的sql语句总结

    mysql语法大全总结,内含DDL,DML,DQL,DCL等语句,可作为工作学习的小字典,随时查看学习,查询所有数据库...- 聚合函数(count,max,min,avg,sum) - 分组查询(group by) - 排序查询(order by) - 分页查询(limit)

    索引摘要:分析数据库查询和架构,并提出索引和架构改进建议

    如果提供了SQL查询日志(通过--sql-log选项),则它: 检查这些查询是否使用了所有表,列和索引 报告字符集与utf不同的文本列 报告不使用索引的查询 报告使用文件排序,临时文件或全表扫描的查询 报告不是很干净的...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    ORACLE数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/S体系结构的数据库之一。  拉里•埃里森  就业前景 从就业与择业的...

    SQL语句大全.docx

    3.使用数据库(在我们的密令行中要先使用数据库 才能进行相应的建表 查询等操作) 4.创建一个数据库表 create table 表名 ( id 类型 约束 , 列名 类型 约束 , 列名 类型 约束 ) 5.查看数据库表 6.查看数据库表...

    C#程序开发范例宝典(第2版).part08

    实例081 DataGridView控件的分页功能 108 实例082 从DataGridView控件拖放数据至TreeView控件 113 实例083 在DataGridView控件中实现合并单元格 116 实例084 在DataGridView控件中显示图片 118 实例085 为...

Global site tag (gtag.js) - Google Analytics