Question about indexes

  • if you have and index built on field 1 and field 2 in that order but when you join to the table that has the index, you join by referencing field 2 and then field 1, are you still using the index?

     

    thanks,

    Barry

  • Assuming that the join is sargable then yes (not converting column data in the join).  The optimiser is smart enough to figure the best path.

  • Interesting question actually. I never considered this.

     

    When you say converting the data you are speaking of something like a cast, or convert; correct?

     

    I have had  cases, many times when I join tables, or perhaps it is just my WHERE clause and I get an index SCAN rather than SEEK.  In some cases I have corrected this either by changing the JOIN (is: perhaps adding WITH (FIRTROWFAST) ) or some other means. Even in those cases I am unclear (most of the time) as to why or how I corrected the issue.  I just know that I can try a select set of commands / hints and get a SEEK out of a scan.

     

    I must say Indexes (while I feel that I understand them) are my weak point. That said; I guess  do not understand them to the degree that I THINK that I do.

    <hr noshade size=1 width=250 color=#BBC8E5> Regards,Jeffery Williams http://www.linkedin.com/in/jwilliamsoh

  • jut to add ...

    when you build your query in Query Analyser, you can check out the execution plan. (exstimated /executed)

    (Ctrl+L or Query/display exstimated  execution plan)

    (Ctrl+K or Query/Show execution plan)

    This option is very educational.

    With SQL2005 this is even better

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • functions on columns that participate in the join can make the optimizer decide not to use an index (not sargeable)

    like convert(varchar(10),mydate1,102)=convert(varchar(10),mydate1,102)

     

  • Or even something as subtile as having a varchar column joining to an nvarchar column can cause implicit_converts which can cause scans.  This can be a big headache to find once the app is deployed .

  • I'm curious about the WITH (FIRTROWFAST). I searched help in SQL Server 2005 and google didn't get any hits. Is there a typo? WHat is this?

  • implicit_converts will indeed messup things.

    rule 1 for any RDBMS : tell the system what you know !

    SQL2005 can generate nice xml-plans which show you implict conversions !

    <

    ScalarOperator ScalarString="[DMaterialManagement].[dbo].[T_Inspection].[SequenceNr]=CONVERT_IMPLICIT(numeric(18,0),[@xyzParam2],0) AND [DMaterialManagement].[dbo].[T_Inspection].[DaughterNr]=CONVERT_IMPLICIT(numeric(18,0),[@xyzParam3],0) AND CONVERT_IMPLICIT(nchar(8),[DMaterialManagement].[dbo].[T_Inspection].[CoilNumberMainframe],0)=[@xyzParam1]">

    Whilst implict conversions are to be avoided, the conversion of a column to match a predicate will at least generate an indexscan (if the column is indexed) !

    And the impact may be huge !!!

     

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • should  be FASTFIRSTROW.

    search for "table hints" in BOL

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thank you all for replying.  I really appreciate it.

     

    Barry

  • Sorry, yes it is.

     

    I should have typed FASTFIRSTROW

     

    <hr noshade size=1 width=250 color=#BBC8E5> Regards,Jeffery Williams http://www.linkedin.com/in/jwilliamsoh

  • It depends on the type of JOIN but it is covered in detail by Peter Gultuzan in the book below.  Hope this helps.

    http://www.amazon.com/SQL-Performance-Tuning/dp/0201791692

    Kind regards,

    Gift Peddie

    Kind regards,
    Gift Peddie

Viewing 12 posts - 1 through 11 (of 11 total)

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