SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Tablesample


Tablesample

Author
Message
okbangas
okbangas
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2641 Visits: 1387
Comments posted to this topic are about the item Tablesample



Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3314 Visits: 1599
Got into the trap...thought query may return 1 or 2....overlooked the primary key factor w00t

A good question though. Thanks for posting.

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Evgeny
 Evgeny
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1581 Visits: 785
Percentage is very low for that sample data, it will likely never return any row. Change the query like that to be fair:

create table #Test (ID int primary key);
insert into #Test values (2);
insert into #Test values (1);
select top 1 ID from #Test tablesample (50 percent)
drop table #Test
Raghavendra Mudugal
Raghavendra Mudugal
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3366 Visits: 2958
Awesome. :-) thank you for the interesting question

Got this wrong too, well I have seen this feature but never used. I need to work on this a lot now to make my understanding consistent.

(I was in the feeling that this might work like TOP [PERCENT] style, but seems like this feature is quite different OR the way it works is different)

Just a thought - "the query always returns no rows" can also be correct I guess. This is happening due to the limited rows and so no matter when you execute this query, it will not show any rows (with respect to the given data and the select statement) until the data is been increased with proper amount which is not going to happen. (I am wrong here is so many ways...:w00tSmile

ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Haining
Haining
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1764 Visits: 210
Interesting question.
Thanks for sharing! :-)
Raghavendra Mudugal
Raghavendra Mudugal
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3366 Visits: 2958
Evgeny Garaev (9/18/2012)
Percentage is very low for that sample data, it will likely never return any row. Change the query like that to be fair:

create table #Test (ID int primary key);
insert into #Test values (2);
insert into #Test values (1);
select top 1 ID from #Test tablesample (50 percent)
drop table #Test


Is this just me or somethings going wrong.....

If I execute the above statement as a whole I can see one record in the result, but executing only the select statement it is not giving any results... Unsure

EDIT:
I have answered myself.... the result is uncommon the resultset may vary like mentioned in the BOL. Blink

ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
okbangas
okbangas
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2641 Visits: 1387
Evgeny Garaev (9/18/2012)
Percentage is very low for that sample data, it will likely never return any row. Change the query like that to be fair:

create table #Test (ID int primary key);
insert into #Test values (2);
insert into #Test values (1);
select top 1 ID from #Test tablesample (50 percent)
drop table #Test

I could have used 50 percent, but the correct answers would be the same. Now the query will return a single row approximately 10% of the times the code is run.



Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18813 Visits: 12426
Nice question. I almost got it wrong, because I had first overlooked the TOP clause.

However, the official correct answer is still a bit questionable. The result of a TOP without ORDER BY is officially undocumented and undefined. It is true that all combinations of SQL Server version and hardware you and I and others have tested this on always produce the same execution plan, and hence the same result. But from that you cannot infer that this is guaranteed behaviour. For all we know, there may be a critical hotfix being pushed out through Windows Update right now that changes this behaviour.

I know this may seem nitpickish, as this would be an extremely unlikely change, but I think it's really important for everyone to know that they should never rely on undocumented behaviour, no matter how repeatable and how safe it appears. People have been bitten by trusting undocumented behaviour (*), and I'm afraid that this will continue.

(*) The two best examples of this that I recall are:
(1) The introduction of new queryplan operators for grouping, I think in SQL 7.0. Before that, sorting was the only option for the optimizer, and many people wrote their group by queries without order by clause. Big surprise for them when, after upgrade, the optimizer suddenly chose to do a hash group by!
(2) The optimizer change that caused "TOP 100 PERCENT" to be ignored (wasn't that in SQL 2005?), breaking code for people who had added that and an ORDER BY clause to their view, thinking that they now didn't need to have an ORDER BY in their queries anymore. And to my utter amazement, people who ahve been bitten by this still insist on "fixing" this with just a different undocumented "trick". And to my even utterer (is that a word?) amazement, Microsoft's own tool -the view designer in SSMS!- still generates this non-working clause, even in SQL Server 2012 (though it now does pop up a warning message when you try to save the view, a slight improvement over 2008R2).

</soapbox>


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Nils Gustav Stråbø
Nils Gustav Stråbø
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3643 Visits: 3575
Bummer. Missed the TOP 1.

Always. Read. The. Full. Question.
Raghavendra Mudugal
Raghavendra Mudugal
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3366 Visits: 2958
Hugo Kornelis (9/19/2012)
Nice question. I almost got it wrong, because I had first overlooked the TOP clause.

However, the official correct answer is still a bit questionable. The result of a TOP without ORDER BY is officially undocumented and undefined...


Hello Hugo,

(bit confused) Just trying to understand... "the official correct answer" with respect to the behavior of the TOP with TABLESAMPLE ? or the output (which have to be consistent) of the query?

The Person.Person table contains 19,972 rows. 
The following statement returns approximately 10 percent of the rows.
The number of rows returned usually changes every time that the statement is executed.


As the BOL states that the output usually changes, so to my understanding the TOP will work upon the resultset of the TABLESAMPLE returns just to limit the top 'n' rows to display on the final output as it uses the primary key constraint so ORDER BY might not be considered here?

Thank you for your contribution (there is stuff always to learn):-)

ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search