Analysing a T-SQL Query

  • 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.

  • 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)

  • 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 🙂

  • thanks... got to learn something new today... wasn't aware of Profile stats

  • 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

  • Good question.

  • 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:

    [font="Courier New"]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

    [/font]

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

  • 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:

    [font="Courier New"]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

    [/font]

    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.

  • 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/

  • 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)

  • 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[/url]
    Learn Extended Events

  • Hi

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

    "Keep Trying"

  • 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

  • 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,My blog: www.igormicev.com

Viewing 14 posts - 16 through 28 (of 28 total)

You must be logged in to reply to this topic. Login to reply