|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, July 28, 2010 7:35 AM
Points: 253,
Visits: 40
|
|
|
|
|
|
Ten 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.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 17, 2013 12:22 PM
Points: 10,571,
Visits: 11,871
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:16 AM
Points: 1,196,
Visits: 1,319
|
|
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/
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 7:56 AM
Points: 109,
Visits: 564
|
|
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)
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 5:02 AM
Points: 2,365,
Visits: 1,825
|
|
Hi
Can anyone explain this to me ? I have been away from SQL Server for some time... finding my way back.
"Keep Trying"
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, November 19, 2012 11:30 PM
Points: 290,
Visits: 713
|
|
[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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 6:17 AM
Points: 1,879,
Visits: 1,452
|
|
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
|
|
|
|