Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Analysing a T-SQL Query


Analysing a T-SQL Query

Author
Message
AmitJbp
AmitJbp
SSC Veteran
SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)

Group: General Forum Members
Points: 253 Visits: 40
Good question.
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
Jack Corbett (5/5/2010)
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.


Good thought except the code has this in it:

CREATE NONCLUSTERED INDEX [IX_EmailAddress] ON [dbo].[QOTD]
(
[EmailAddress] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


That does create a non clustered index on the EmailAddress column.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11018 Visits: 14858
cengland0 (5/6/2010)
Jack Corbett (5/5/2010)
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.


Good thought except the code has this in it:

CREATE NONCLUSTERED INDEX [IX_EmailAddress] ON [dbo].[QOTD]
(
[EmailAddress] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


That does create a non clustered index on the EmailAddress column.


Right, but I did say that I didn't read the script, which I state in my post. In reality the information provided without the code does provide enough information to answer the question, it's just that the information provided does not match what it is in the code. I am commenting so that the question can be corrected.

I usually try to answer the questions without running the code provided because it is usually too easy to get it right if you run the code. It is in this case as well, if you view an execution plan.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Tom Brown
Tom  Brown
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1342 Visits: 1466
Am I right in thinking there are a more efficient ways of creating the test data?
Such as:

WITH NORBAR AS (
SELECT N FROM dbo.Tally
WHERE N < 500 )
INSERT INTO QOTD (Title,EmailAddress,DateSubmitted,Age)
SELECT 'Mr','jsmith@email.com','24/03/2010',50
FROM NORBAR;



See Jeff's article on Tally table: http://www.sqlservercentral.com/articles/T-SQL/62867/
Phil Brammer
Phil Brammer
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 640
CirquedeSQLeil (5/5/2010)

That is likely true - haven't tried. I am just accustomed to asking for the actual execution plan that it seemed the safer route for this too.;-)


Except to get the actual execution plan, the query has to finish. Which in this case is no big deal, but against a very large data set can be quite painful to wait while the same plan is likely generated as the estimated. (Row counts may be off, but the plan itself would likely the same)
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
Tom Brown (5/6/2010)
Am I right in thinking there are a more efficient ways of creating the test data?
Such as:

WITH NORBAR AS (
SELECT N FROM dbo.Tally
WHERE N < 500 )
INSERT INTO QOTD (Title,EmailAddress,DateSubmitted,Age)
SELECT 'Mr','jsmith@email.com','24/03/2010',50
FROM NORBAR;



See Jeff's article on Tally table: http://www.sqlservercentral.com/articles/T-SQL/62867/


Yes, that would be one method that is better at producing this test data.:-)



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

ChiragNS
ChiragNS
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2409 Visits: 1865
Hi

Can anyone explain this to me ? I have been away from SQL Server for some time... finding my way back. :-)

"Keep Trying"
m--S3qU3L
m--S3qU3L
SSC Veteran
SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

Group: General Forum Members
Points: 292 Visits: 714
[b]The correct answer is 1 INNER JOIN is used. When the database engine executes the SELECT query to return all rows that have an email address of 'jdoe@email.com' the following steps take place:

1. There is an Index Seek on the IX_EmailAddress non clustered index - this searches for the 'jdoe@email.com' email address. When the email address is found in the index, its QOTDID value will be used in STEP 2 - The value for EmailAddress is returned by this step.

2. Next there is a clustered index seek on the IX_QOTDID index looking for the QOTDID value which was found in STEP 1. - The value for Title, DateSubmitted and Age are returned by this step.

3. Then the output of the index seek (STEP 1) is joined with the output of the clustered index seek (STEP 2) and the data is returned via the SELECT statement - thus returning the row of data that contains an EmailAddress of 'jdoe@email.com' :





Is there really a JOIN happening while a select is applied with a column with Non-Clustered index from a table containing Clustered index?

I believe its traversing happening between the clustered and Non-Clustered indexes to find the actual row.

Let me explain it with using the concept of Nonclustered Index:
In a nonclustered index, the leaf level does not contain all the data. In addition to the key values, each index row in the leaf level (the lowest level of the tree) contains a bookmark that tells SQL Server where to find the data row corresponding to the key in the index.

A bookmark can take one of two forms. If the table has a clustered index, the bookmark is the clustered index key for the corresponding data row. If the table is a heap (in other words, it has no clustered index), the bookmark is a row identifier (RID), which is an actual row locator in the form File#Tongueage#:Slot#.


In this case, the bookmark (or pointer) contains the Clustered Index key. So after getting the clustered index key what I believe is it searches the row using this key (here it is QOTID column).

So I believe join is not happening.

Correct me if I'm wrong.

So please SHout :-)

John
Igor Micev
Igor Micev
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4166 Visits: 4849
Useful question, thank you
The following query with same structure as those in the question produces 0 joins

SELECT *
FROM QOTD
where qotdid = 1

because of "QOTD" which is clustered index


Igor Micev,
‌SQL Server developer at Seavus
www.seavus.com
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