Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««23456»»»

SELECT TOP Expand / Collapse
Author
Message
Posted Wednesday, December 22, 2010 4:08 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 25, 2014 11:05 PM
Points: 338, Visits: 463
Yes, the answer is "Ann". But when you say most of the time we would get “Ann” since the clustered index is created on “first name”.
Why can’t we say it is always return the “Ann”?
I did execute the query more than 10 times, all the time the result was “Ann”.
Just trying to understand, in which scenario SQL Server return different result than “Ann”?


Joe
Post #1038161
Posted Wednesday, December 22, 2010 5:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 11:42 AM
Points: 13,295, Visits: 11,086
dfine (12/22/2010)
Yes, the answer is "Ann". But when you say most of the time we would get “Ann” since the clustered index is created on “first name”.
Why can’t we say it is always return the “Ann”?
I did execute the query more than 10 times, all the time the result was “Ann”.
Just trying to understand, in which scenario SQL Server return different result than “Ann”?


For this small result set with simple key it will probably always return 'Ann'.
But when you are talking about very large tables with a clustered index, the output might not be predictable.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1038186
Posted Wednesday, December 22, 2010 4:55 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, October 17, 2014 1:59 PM
Points: 262, Visits: 918
Ninja's_RGR'us (12/21/2010)

So which was better performance wise? maxdop 1 or 0?

Yes I know the results are wrong without the order by!


well for ad-hoc examination it was much better to leave off the order-by and remember that the results had sub-sections in nondeterministic order.

otherwise the answer is the typical "it depends" - on tradeoff between sorting a huge flat resultset and the time it takes to select each part with multiple CPU.
Post #1038557
Posted Thursday, December 23, 2010 3:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:18 PM
Points: 6,048, Visits: 8,330
I got the question right, assuming that the submitter was unaware of the fact that there is no guarantee for this behaviour. I was then surprised to see in the explanation that he is in fact aware of this, and still marked Ann as the correct answer. I don't get this - basically, the explanation says that the correct answer is "undetermined", but that answer is not given.

A weak question.


ronmoses (12/21/2010)
For educational purposes, I would genuinely appreciate it if one of the folks who take issue with the "most of the time" factor could illustrate a scenario in which that script returns different results.

Thanks!
Ron

Read this blog post I wrote a few years ago.
For this specific scenario, without any changes to the code, the data, etc, I don't think we can make this script return different results. But Microsoft can. Since they didn't document or otherwise guarantee this behaviour, future versions of SQL Server, or even service packs and hotfixes, may change this behaviour. Remember what happened to all those views that abused the pre-SQL2005 "TOP 100 PERCENT ... ORDER BY" behaviour? And what about GROUP BY without ORDER BY in SQL 6.5 and older?



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1038681
Posted Monday, December 27, 2010 7:53 PM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:15 AM
Points: 1,447, Visits: 1,059
mtassin (12/21/2010)

Always use the top with an ORDER BY clause!


I got the question correct, but this is the last sentance of the answer's explanation.

Why?

Why should I always use TOP with an order by clause? What benefit do I get with say TOP 100% when I decide i want all the records to come back, just ordered?


I don't think anyone gave a direct answer to this (although someone else pointed out the same ambiguous wording.) The author meant you should always use top with an order by clause. In other words, don't use top without an order by clause. The author was not saying you should always use top when you are using an order by clause.
Post #1039616
Posted Tuesday, December 28, 2010 10:56 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
Hugo Kornelis (12/23/2010)
I got the question right, assuming that the submitter was unaware of the fact that there is no guarantee for this behaviour. I was then surprised to see in the explanation that he is in fact aware of this, and still marked Ann as the correct answer. I don't get this - basically, the explanation says that the correct answer is "undetermined", but that answer is not given.

A weak question.


I have exactly the same opinion. Ann was the only reasonable option available, and I was expecting to have to mention that it wouldn't be guaranteed, but then he does it himself.
Post #1039890
Posted Tuesday, December 28, 2010 1:51 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:13 AM
Points: 9,926, Visits: 11,188
mtassin (12/21/2010)
These days I begin to wonder if the optimizer doesn't just return them in clustered index order when the ORDER BY clause is omitted (and a clustered index is present), and Microsoft lists in BOL that the order is arbitrary so that if they need to change the optimizer for some reason with a service pack, they can say "We told you it was arbitrary".

No, the optimizer (and storage engine) can choose whatever access path seems most efficient. For example:

CREATE  TABLE dbo.Example
(
row_id INTEGER IDENTITY NOT NULL PRIMARY KEY CLUSTERED,
data AS CONVERT(VARCHAR(11), row_id) PERSISTED NOT NULL UNIQUE
);
GO
INSERT dbo.Example
DEFAULT VALUES;
GO 10
SELECT E.row_id, E.data
FROM dbo.Example E;
GO
DROP TABLE dbo.Example;

Even on a simple scan of the clustered index, there are a number of ways to show that ordering isn't guaranteed. Using parallelism is the most popular one, but this works too:

CREATE  TABLE dbo.Example
(
data BIGINT NOT NULL,
padding CHAR(5000) NOT NULL DEFAULT ''
);
GO
CREATE CLUSTERED INDEX c
ON dbo.Example (data)
WITH (ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF);
GO
INSERT dbo.Example (data) VALUES (CHECKSUM(NEWID()));
GO 64
-- Clustered index scan, but not in clustered index order!
SELECT TOP (10)
data
FROM dbo.Example
GO
DROP TABLE dbo.Example;





Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1039988
Posted Wednesday, December 29, 2010 5:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:01 AM
Points: 5,364, Visits: 8,954
Just to point out that this code:
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE name like '#Customer%')
DROP TABLE #Customer

is NOT the proper way to test for the existence of a temporary table (create the temporary table in a different connection, and you'll get an error when you run this code!)

The proper way to test for the existence of a temporary table is:

IF OBJECT_ID('tempdb..#Customer') IS NOT NULL



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1040209
Posted Wednesday, December 29, 2010 5:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:01 AM
Points: 5,364, Visits: 8,954
ronmoses (12/21/2010)
For educational purposes, I would genuinely appreciate it if one of the folks who take issue with the "most of the time" factor could illustrate a scenario in which that script returns different results.

Thanks!
Ron


Does this suffice?
IF OBJECT_ID('tempdb..#Test') IS NOT NULL DROP TABLE #Test;
CREATE TABLE #Test (RowID INT PRIMARY KEY CLUSTERED);
;WITH
TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),
MILLIONS (N) AS (SELECT 1 FROM THOUSANDS t1 CROSS JOIN THOUSANDS t2),
TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM MILLIONS)
INSERT INTO #Test
SELECT N FROM TALLY;

SELECT TOP (5) * FROM #Test WHERE RowID > 5000;
SELECT TOP (5) * FROM #Test WHERE RowID > 7000;
SELECT TOP (5) * FROM #Test WHERE RowID > 9000;

SELECT * FROM #Test;
IF OBJECT_ID('tempdb..#Test') IS NOT NULL DROP TABLE #Test;

On my system, the last select starts with 328417.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1040216
Posted Wednesday, December 29, 2010 6:27 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 6:33 AM
Points: 2,917, Visits: 2,532
WayneS
On my system, the last select starts with 328417.

Sorry Wayne,

On my system I get:
SELECT TOP (5) * FROM #Test WHERE RowID > 5000; - 5001
SELECT TOP (5) * FROM #Test WHERE RowID > 7000; - 7001
SELECT TOP (5) * FROM #Test WHERE RowID > 9000; - 9001
SELECT * FROM #Test; - 1


Steve Jimmo
Sr DBA
“If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
Post #1040228
« Prev Topic | Next Topic »

Add to briefcase «««23456»»»

Permissions Expand / Collapse