简体   繁体   English

Sql Server 2008视图中的ORDER BY

[英]ORDER BY in a Sql Server 2008 view

we have a view in our database which has an ORDER BY in it. 我们的数据库中有一个ORDER BY的视图。 Now, I realize views generally don't order, because different people may use it for different things, and want it differently ordered. 现在,我意识到观点通常不会排序,因为不同的人可能会将它用于不同的事情,并希望它有所不同。 This view however is used for a VERY SPECIFIC use-case which demands a certain order. 然而,该视图用于需要特定订单的非常特定的用例。 (It is team standings for a soccer league.) (这是足球联赛的球队排名。)

The database is Sql Server 2008 Express, v.10.0.1763.0 on a Windows Server 2003 R2 box. 该数据库是Windows Server 2003 R2上的Sql Server 2008 Express,v.10.0.1763.0。

The view is defined as such: 视图定义如下:

CREATE VIEW season.CurrentStandingsOrdered
AS
    SELECT TOP 100 PERCENT *, season.GetRanking(TEAMID) RANKING   
    FROM season.CurrentStandings 
    ORDER BY 
        GENDER, TEAMYEAR, CODE, POINTS DESC, 
        FORFEITS, GOALS_AGAINST, GOALS_FOR DESC, 
        DIFFERENTIAL, RANKING

It returns: 它返回:

GENDER, TEAMYEAR, CODE, TEAMID, CLUB, NAME,  
WINS, LOSSES, TIES, GOALS_FOR, GOALS_AGAINST,  
DIFFERENTIAL, POINTS, FORFEITS, RANKING

Now, when I run a SELECT against the view, it orders the results by GENDER, TEAMYEAR, CODE, TEAMID . 现在,当我对视图运行SELECT时 ,它按GENDER,TEAMYEAR,CODE,TEAMID命令结果。 Notice that it is ordering by TEAMID instead of POINTS as the order by clause specifies. 请注意,它是按TEAMID而不是POINTS排序,因为order by子句指定。

However, if I copy the SQL statement and run it exactly as is in a new query window, it orders correctly as specified by the ORDER BY clause. 但是,如果我复制SQL语句并完全按照新查询窗口中的方式运行它,它将按照ORDER BY子句的指定正确排序

SQL Server 2005 ignores TOP 100 PERCENT by design. SQL Server 2005在设计上忽略了TOP 100 PERCENT。

Try TOP 2000000000 instead. 请尝试使用TOP 2000000000。

Now, I'll try and find a reference... I was at a seminar presented by Itzak Ben-Gan who mentioned it 现在,我会尝试找一个参考资料......我参加了由Itzak Ben-Gan提出的研讨会

Found some... 发现一些......

Kimberly L. Tripp Kimberly L. Tripp

"TOP 100 Percent ORDER BY Considered Harmful" “前100%的秩序被认为是有害的”

In this particular case, the optimizer recognizes that TOP 100 PERCENT qualifies all rows and does not need to be computed at all. 在这种特殊情况下,优化器会识别TOP 100 PERCENT限定所有行,并且根本不需要计算。

The order of rows returned by a view with an ORDER BY clause is never guaranteed. 从不保证具有ORDER BY子句的视图返回的行的顺序。 If you need a specific row order, you must specify where you select from the view. 如果需要特定的行顺序,则必须指定从视图中选择的位置。

See this the note at the top of this Book On-Line entry. 请参阅本书在线条目顶部的注释。

Just use : 只需使用:

" Top (99) Percent " 顶级(99)百分比

or 要么

"Top (a number 1000s times more than your data rows like 24682468123)" it works! “Top(比你的数据行多了1000多倍,比如24682468123)”它有效! just try it. 就试一试吧。

In SQL server 2008, ORDER BY is ignored in views that use TOP 100 PERCENT. 在SQL Server 2008中,在使用TOP 100 PERCENT的视图中忽略ORDER BY。 In prior versions of SQL server, ORDER BY was only allowed if TOP 100 PERCENT was used, but a perfect order was never guaranteed. 在SQL Server的早期版本中,只有在使用TOP 100 PERCENT时才允许ORDER BY,但从未保证完美的订单。 However, many assumed a perfect order was guaranteed. 然而,许多人认为完美的订单是有保证的。 I infer that Microsoft does not want to mislead programmers and DBAs into believing there is a guaranteed order using this technique. 我推断微软不想误导程序员和DBA相信使用这种技术有保证的订单。

An excellent comparative demonstration of this inaccuracy, can be found here... 这里可以找到这种不准确性的优秀对比示例......

http://blog.sqlauthority.com/2009/11/24/sql-server-interesting-observation-top-100-percent-and-order-by http://blog.sqlauthority.com/2009/11/24/sql-server-interesting-observation-top-100-percent-and-order-by

Oops, I just noticed that this was already answered. 哎呀,我刚才注意到这已经回答了。 But checking out the comparative demonstration is worth a look anyway. 但无论如何,查看比较示范值得一看。

Microsoft has fixed this. 微软解决了这个问题。 You have patch your sql server 你有补丁你的SQL服务器

http://support.microsoft.com/kb/926292 http://support.microsoft.com/kb/926292

I found an alternative solution. 我找到了另一种解决方案。

My initial plan was to create a 'sort_order' column that would prevent users from having to perform a complex sort. 我最初的计划是创建一个“sort_order”列,以防止用户执行复杂的排序。

I used a windowed function ROW_NUMBER. 我使用了一个窗口函数ROW_NUMBER。 In the ORDER BY clause, I specified the default sort order that I needed (just as it would have been in the ORDER BY of a SELECT statement). 在ORDER BY子句中,我指定了我需要的默认排序顺序(就像它在SELECT语句的ORDER BY中一样)。

I get several positive outcomes: 我得到了几个积极的结果:

  1. By default, the data is getting returned in the default sort order I originally intended (this is probably due to the windowed function having to sort the data prior to assigning the sort_order value) 默认情况下,数据以我最初预期的默认排序顺序返回(这可能是由于窗口函数必须在分配sort_order值之前对数据进行排序)

  2. Other users can sort the data in alternative ways if they choose to 其他用户可以选择以其他方式对数据进行排序

  3. The sort_order column is there for a very specific sort need, making it easier for users to sort the data should whatever tool they use rearranges the rowset. sort_order列用于非常特定的排序需求,如果用户使用重新排列行集的任何工具,用户可以更轻松地对数据进行排序。

Note: In my specific application, users are accessing the view via Excel 2010, and by default the data is presented to the user as I had hoped without further sorting needed. 注意:在我的特定应用程序中,用户通过Excel 2010访问视图,默认情况下,数据按照我的希望呈现给用户,无需进一步排序。

Hope this helps those with a similar problem. 希望这有助于那些有类似问题的人。

Cheers, Ryan 干杯,瑞恩

run a profiler trace on your database and see the query that's actually being run when you query your view. 在数据库上运行探查器跟踪,并查看查询视图时实际运行的查询。

You also might want to consider using a stored procedure to return the data from your view, ordered correctly for your specific use case. 您还可以考虑使用存储过程从视图中返回数据,并针对您的特定用例正确排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM