弘帝企业智能建站系统交流平台

 找回密码
 立即注册
查看: 1805|回复: 0

【SQL】查询200行效率比较

[复制链接]
发表于 2022-8-4 10:33:00 | 显示全部楼层 |阅读模式
declare @d datetime
set @d=getdate()

SELECT TOP 200 * From FreeHost.FreeHost_Product_Domain where id in (SELECT TOP 200 id From FreeHost.FreeHost_Product_Domain Order By endtime ASC) Order By endtime DESC
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
//416ms,30ms(两次执行)

set @d=getdate()
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY endtime asc) AS [ROW_NUMBER], * FROM FreeHost.FreeHost_Product_Domain) AS [t1] WHERE [t1].[ROW_NUMBER] BETWEEN 0 AND 200 order by endtime asc
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
//1116ms,986ms(两次执行)

很明显用了ROW_NUMBER效率低下很多,但ROW_NUMBER] BETWEEN 0 AND 200优势可以指定任意行之间的表数据获取,逻辑上更清晰。
declare @d datetime
set @d=getdate()
select * from FreeHost.FreeHost_Product_Domain where id in (SELECT TOP 190 id From FreeHost.FreeHost_Product_Domain where id in (SELECT TOP 190 id From FreeHost.FreeHost_Product_Domain Order By endtime ASC) Order By endtime DESC) order by endtime asc
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
//6,6,3
set @d=getdate()
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY endtime asc) AS [ROW_NUMBER], * FROM FreeHost.FreeHost_Product_Domain) AS [t1] WHERE [t1].[ROW_NUMBER] BETWEEN 0 AND 190 order by endtime asc
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
//3,3,6

以上效率差不多,但ROW_NUMBER多了一个排序,逻辑略清晰
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|弘帝企业智能建站系统 ( 皖ICP备07503252号 )

GMT+8, 2024-11-24 03:08 , Processed in 0.048628 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表