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 Thursday, May 6, 2010 1:46 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 28, 2010 7:35 AM
Points: 253, Visits: 40
Good question.
Post #916712
Posted Thursday, May 6, 2010 2:23 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
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.
Post #916721
Posted Thursday, May 6, 2010 9:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:55 PM
Points: 11,297, Visits: 13,083
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

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 #917116
Posted Thursday, May 6, 2010 9:34 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:41 PM
Points: 1,293, Visits: 1,430
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/
Post #917134
Posted Thursday, May 6, 2010 9:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 8, 2014 3:06 PM
Points: 109, Visits: 600
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)
Post #917165
Posted Thursday, May 6, 2010 10:15 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 21,733, Visits: 15,424
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #917187
Posted Monday, May 10, 2010 4:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, September 1, 2014 6:55 AM
Points: 2,366, Visits: 1,845
Hi

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


"Keep Trying"
Post #918894
Posted Wednesday, May 12, 2010 10:14 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, November 12, 2013 10:53 PM
Points: 290, 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#:Page#: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
Post #920977
Posted Wednesday, March 21, 2012 12:25 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:33 PM
Points: 2,948, Visits: 2,965
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
Post #1270423
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse