Results 1 to 31 of 31

Thread: How I build my SQL strings

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: How I build my SQL strings

    Quote Originally Posted by szlamany
    Where CITYPE is NOT NULL

    Is CITYPE an INDEX? And it allows NULL's? I'm not so sure how NULL's are handled in INDEX SCANS - I'm not sure I have ever indexed a column that allows null's...
    CI_Type is not indexed.

    I did find this:
    how many rows in that table?
    In the table where all the data is actually held there are 528,643 rows.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How I build my SQL strings

    So a table scan is being done for that query.

    Is the PRIMARY KEY a CLUSTERED INDEX?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: How I build my SQL strings

    Quote Originally Posted by szlamany
    So a table scan is being done for that query.
    It doesn't say a table scan is being done. I hate computers sometimes. I mean what am I, psychic?

    Is the PRIMARY KEY a CLUSTERED INDEX?
    Yes.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How I build my SQL strings

    Quote Originally Posted by disruptivehair
    ...what am I, psychic?
    I am

    Actually, since you told me that the WHERE column was not an INDEX...

    but I can see that 87% of the time was spent on something appearing to be named "PPEKBaseData.PK..."

    that means you have a CLUSTERED PRIMARY KEY.

    So your DATA LEAVES are in physically stored in with your PRIMARY INDEX LEAVES. You have no DATA section - so the PRIMARY KEY section was used.

    It has no relationship with CI_Type so it was "table scanned"...

    I'm guessing based on the number of rows+size of row data returned that the LAZY spooler was used to store the working resultset on disk - not enough memory space...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How I build my SQL strings

    [edit]Change it to:

    Code:
    Select * From dbo.vwfSpecialIssued
    	Where PRIMARYKEYCOLUMN is in (Select PRIMARYKEYCOLUMN
    					From dbo.vwfSpecialIssued
    					Where CI_Type is not null)
    and tell me if performance changes

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: How I build my SQL strings

    Quote Originally Posted by szlamany
    [edit]Change it to:

    Code:
    Select * From dbo.vwfSpecialIssued
    	Where PRIMARYKEYCOLUMN is in (Select PRIMARYKEYCOLUMN
    					From dbo.vwfSpecialIssued
    					Where CI_Type is not null)
    and tell me if performance changes
    Nope, no change.

    This:
    Code:
    Select * From dbo.vwfeSpecialIssued
    	Where Pol_no in (Select Pol_no
    			From dbo.vwfeSpecialIssued
    			Where CI_Type is not null)
    and this:

    Code:
    SELECT * FROM dbo.vwFESpecialIssued
    WHERE CI_Type IS NOT NULL
    took exactly the same amount of time to run though their execution plans look different. The one you wrote contains a clustered index seek and an index scan as well as three clustered index scans; the simple one below just has three clustered index scans.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How I build my SQL strings

    You did see my post #19 - right?

    At any rate - I was trying to avoid the spool to disk by using the subquery to generate a "primary key list" - being smaller than SELECT *...

    So that the final resultset would be served by an index scan.

    But "IN (subquery)" is a stinky construct.

    What's the goal here anyway? There is no way to really avoid a table scan when the WHERE clause is on some un-keyed column of data. Do you have the option of temporarily adding an INDEX for ci_type to see the difference - or do you need DBA blessing for that

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width