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 ««123»»

Analysing a T-SQL Query Expand / Collapse
Author
Message
Posted Wednesday, May 5, 2010 11:12 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 30, 2014 8:46 AM
Points: 25, Visits: 174
I think the Answer is ( 0 ) Bz the field QOTDID don't exist, there no Update.
Post #916294
Posted Wednesday, May 5, 2010 11:59 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:01 PM
Points: 1,665, Visits: 338
An unusual situation occurred for me. The first time I ran the SELECT statement the plan was a Clustered Index Scan. Subsequent runs were the expected NC Index Seek & Key Lookup. It took me a moment to realize the cause - it was due to having the following database settings set:
Auto Create Statistics - On
Auto Update Statistics - On
Auto Update Statistics Asynchronously - On

Testing with the various settings yields these results:
If you do not Auto Create stats you only get a clustered index scan.
If you Auto Create but do not Auto Update you also only get a clustered index scan.
If you Auto Update stats (synchronously) you get the results in the answer (NC Index Seek & Key Lookup).
And finally if you Auto Update Statistics Asynchronously the first run is clustered index scan with subsequent runs NC Index Seek & Key Lookup.

Post #916328
Posted Wednesday, May 5, 2010 12:08 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
Got lucky and got the points, but mainly I learnt something.

Good QotD.

Thanks,


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #916336
Posted Wednesday, May 5, 2010 12:46 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, August 24, 2013 10:11 AM
Points: 83, Visits: 60
DAMN -- got it wrong! Know why? Because I over-thought the answer: I though that since the counter (@i) started at 0 and incremented to 499, there would never be a record with QOTDID = 500.

Unfortunately, I forgot that the QOTDID field started at 1, so the very last record gets an id of 500.

Damn, Sam!


Thanks,
- Joseph Marsh
Post #916364
Posted Wednesday, May 5, 2010 4:50 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
Great question, gets you to actually thinking about what SQL Server is doing in the background.
Post #916569
Posted Wednesday, May 5, 2010 7:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 9:09 AM
Points: 10,342, Visits: 13,352
I got it wrong because I didn't read the script. I read the text where it says you created a non-clustered index on LastName and the query used EmailAddress. IF the index had been on LastName there would have been only a clustered index scan and no join. Yes, with the index on EmailAddress I would have expected 1 join because of a key/bookmark lookup.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #916613
Posted Wednesday, May 5, 2010 10:17 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:09 PM
Points: 1,940, Visits: 1,173
Hi,I checked this query by using estimated execution plan(graphical). there is one inner join between one index seek on email address and another index seek on Qotdid .but i done wrong.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #916645
Posted Wednesday, May 5, 2010 10:25 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 2, 2014 11:03 PM
Points: 2,104, Visits: 371
CirquedeSQLeil (5/5/2010)
This is a good question. Thanks. One other method one can use to demonstrate this is by evaluating the actual execution plan.


yep.. I also used the execution plan to get the answer
Post #916646
Posted Wednesday, May 5, 2010 10:27 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 2, 2014 11:03 PM
Points: 2,104, Visits: 371
thanks... got to learn something new today... wasn't aware of Profile stats
Post #916647
Posted Thursday, May 6, 2010 12:30 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Sunday, July 13, 2014 8:35 AM
Points: 3,939, Visits: 1,087
Hmmm, I get "Clustered Index Scan" as both PhysicalOp and LogicalOp in
SQL 2008 with Auto Statistics "false".
With Auto Statistics "true" I get one inner join.
I have to check why Auto Statistics was "false" on that test database...


Lars Broberg
Elbe-Data AB
Post #916686
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse