麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

你知道多少關于sql select top N?

2019-11-17 03:56:10
字體:
來源:轉載
供稿:網友
背景:
sql select top N 語句是一個非常重要的語句, 在實現分頁查詢中是不可或缺的. 由于分頁查詢通常涉及含有大量記錄的表, 并且是為大量用戶分享的任務,因此,對其進行優化是很有意義的。

實現sql top N 的功能有幾種變種:

1. set rowcount @n; select ... order by somefields

2. select top (@n) .... order by somefields

3. select top (xx) ....   order by somefields

        -- 其中 xx是一個常數, 比如10

在上述的查詢中引用的somefields, 如果涉及的表在其上有索引是一種情況, 沒有索引又是一種情況。
有索引的話,即使表含有很多記錄,也不會對性能造成太大問題。
沒有索引的情況也是會有實際需求的,比如實時的找出銷售最好的前100個產品。在沒有索引時的查找Top N, 如果不進行細致的優化,會對性能造成很大的影響,甚至會使得整個系統癱瘓。

如果要對top n進行優化,那么了解sql server 是如何處理上述的top n 的幾種變形就是很有必要的. 下面的文章是我在MS的論壇上發的, 我自己懶得翻譯成中文了,和大家共享一下吧。



原文(是我在http://social.msdn.microsoft.com/Forums/en/transactsql/thread/944ad896-b34c-4dea-af55-cfbae79251f6上的一個回貼)



Question:

--fast
1. select top 100 * from test where c1 < 30000 order by c2

--slow
2. select top 101 * from test where c1 < 30000 order by c2



1. is more than  two times faster than 2.



Why?



What a coinccident! I am on the same issue just at the time.
I was considering implementing an algorithm like this:
First populate the N rows to a table variable (with index on the sort column), then iterate through all left rows, adding one row to the table variable if bigger than min of the table, else discard it.  This could be either done in sql or clr aggregate function.
Then I thought maybe MS had already done it in the Top N stuff, so started to run a test against it.



CREATE TABLE [dbo].[NUM]
([n] int NOT NULL, s varchar(128) NULL, PRIMARY KEY CLUSTERED([n] ASC))
go
-- populate data
set nocount on
declare @n int, @i int
set @n=1000000
set @i = 0
while @n>0 begin
if @i = 0 begin tran
insert into dbo.NUM
select @n, convert(varchar,@n + @i * 2)
set @n=@n-1
set @i = (@i + 1) % 1000
if @i = 0 commit
end
GO
-- test 1
select  top ( XX ) cast(s as int), n from dbo.num
order by cast(s as int) desc
go
-- test 2
set rowcount XX
select cast(s as int), n from dbo.num
order by cast(s as int) desc
for test 1, duration < 1s, for any XX <= 100, and the duration is about 12s for any XX >100

for test 2, the duration is fixed at 4s for XX: 10  - 100,000.

The show-plan shows test 1 uses Top N sort op, while the test 2 uses Sort  op.
Ok I dont care about the sort op. The only thing I care is if MS has correctly implemented the Ton N Sort.
MSDN stated about "Top N sort":  
"Top N Sort is similar to the Sort iterator, except that only the first N rows are needed, and not the entire result set. For small values of N, the SQL Server query execution engine attempts to perform the entire sort Operation in memory. For large values of N, the query execution engine resorts to the more generic method of sorting to which N is not a parameter."

As you can see, this statement sound like the algorithm I was intending to write myself. But the later part mentioned a "more generic method of sorting to which N is not a parameter", that exlains why no matter how XX changes for test1 after going beyong 100, the duration is always the same.  Test 2 is also insensitive to N.
So MS seems used 3 algorithm, in which two of them are used for "top N", one is for "set rowcount".

I do not think whether to perform it in memory or not will cause such a big difference. It's mainly due to that only one (the fastest one) uses the algorithm of just keeping the top N rows and then evict low ranking items when they fall below the N window.

I am using a sql 2005.

I also tested the "select top (@n)" variation. The result shows that "select top (@n)" is similar to "set rowcount...".
The reason I tested the "select top (@n)" variation is that I was wondering if We could use plan-force to force it use the faster "Top N Sort". However it seems that "select top (@n)" is quite different from "select top (xx)" where xx is a constant,  but similar to  "set rowcount; ...". Guess it will not work,  so I will not try to test if plan-force can do the job.

Just curious why MS choose not to use the "Top N Sort" algorithm always, instead to choose this so complex arrangement (i.e. some with "Top N Sort", some with the "Sort then Top").   I think, "Top N Sort" should always be used




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天天夜天天操 | 黄网站免费入口 | 欧美人与牲禽动交精品一区 | 在线播放免费视频 | 免费一级特黄欧美大片勹久久网 | 玖玖视频精品 | 精品99在线视频 | 成人一级片毛片 | 中国av中文字幕 | 亚洲va久久久噜噜噜久牛牛影视 | 一级做a爱片性色毛片 | 国产成人在线网站 | 男女羞羞视频 | 午夜视 | 在线小视频国产 | 一区播放| 精品国产视频一区二区三区 | 亚洲码无人客一区二区三区 | 成人综合一区二区 | 日本在线一区二区 | 亚洲精品久久久久久久久久久 | 九九热精品视频在线 | 久久福利国产 | 第四色成人网 | 毛片午夜 | 噜噜噜躁狠狠躁狠狠精品视频 | 欧美成人黄色小视频 | 成人在线观看免费爱爱 | 日本免费不卡一区二区 | 99精品国产一区二区三区 | 美女喷水网站 | 一级大片一级一大片 | 欧美成人a | 亚洲成人综合网站 | 中文在线观看www | 羞羞色在线观看 | 日韩精品久久久久久久电影99爱 | 国产三级精品最新在线 | 91网站在线观看视频 | 国产午夜免费视频 | 日本高清一级片 |