Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Conditional Index Usage

Conditional Index Usage

Scheduled Pinned Locked Moved Database
databasequestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gnjunge
    wrote on last edited by
    #1

    Hi, I have a stored procedure that is basically as follows: SELECT ColumnA, ColumnB, ColumnC FROM TableA WHERE (@ValueA IS NULL OR ColumnA = @ValueA) AND (@ValueB IS NULL OR ColumnB = @ValueB) AND (@ValueC IS NULL OR ColumnC = @ValueC) So each of the input values can either have a value or is null. If it has a value it has to be included in the filter, otherwise there will not be a filter on that column. Let's say that I have 3 indexes: 1) ColumnA, 2) ColumnA and ColumnB, 3) ColumnA and ColumnC Now on the first run of this sproc, if let's say columnA and ColumnB have values, it will use index 2. On the next runs it will continue using index 2, even if now ColumnA and ColumnC have values (thus actually it should have used index 3). I know that one can tell SQL what index to use using WITH(INDEX XXXX) but can the use of an index be conditioned, or do i have to write 3 different stored procedures?

    D G 2 Replies Last reply
    0
    • G gnjunge

      Hi, I have a stored procedure that is basically as follows: SELECT ColumnA, ColumnB, ColumnC FROM TableA WHERE (@ValueA IS NULL OR ColumnA = @ValueA) AND (@ValueB IS NULL OR ColumnB = @ValueB) AND (@ValueC IS NULL OR ColumnC = @ValueC) So each of the input values can either have a value or is null. If it has a value it has to be included in the filter, otherwise there will not be a filter on that column. Let's say that I have 3 indexes: 1) ColumnA, 2) ColumnA and ColumnB, 3) ColumnA and ColumnC Now on the first run of this sproc, if let's say columnA and ColumnB have values, it will use index 2. On the next runs it will continue using index 2, even if now ColumnA and ColumnC have values (thus actually it should have used index 3). I know that one can tell SQL what index to use using WITH(INDEX XXXX) but can the use of an index be conditioned, or do i have to write 3 different stored procedures?

      D Offline
      D Offline
      Dave B
      wrote on last edited by
      #2

      Probably best to use dynamic sql but read this first http://www.sommarskog.se/dynamic\_sql.html

      G 1 Reply Last reply
      0
      • D Dave B

        Probably best to use dynamic sql but read this first http://www.sommarskog.se/dynamic\_sql.html

        G Offline
        G Offline
        gnjunge
        wrote on last edited by
        #3

        The truth is I don't want to go there. I rather write an extra sproc.

        1 Reply Last reply
        0
        • G gnjunge

          Hi, I have a stored procedure that is basically as follows: SELECT ColumnA, ColumnB, ColumnC FROM TableA WHERE (@ValueA IS NULL OR ColumnA = @ValueA) AND (@ValueB IS NULL OR ColumnB = @ValueB) AND (@ValueC IS NULL OR ColumnC = @ValueC) So each of the input values can either have a value or is null. If it has a value it has to be included in the filter, otherwise there will not be a filter on that column. Let's say that I have 3 indexes: 1) ColumnA, 2) ColumnA and ColumnB, 3) ColumnA and ColumnC Now on the first run of this sproc, if let's say columnA and ColumnB have values, it will use index 2. On the next runs it will continue using index 2, even if now ColumnA and ColumnC have values (thus actually it should have used index 3). I know that one can tell SQL what index to use using WITH(INDEX XXXX) but can the use of an index be conditioned, or do i have to write 3 different stored procedures?

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          What I have been taught and understand about SQL Server is this: Let it decide on what indexes to use on the basis that 'SQL Server knows best'. Sometimes SQL Server will decide to do a table scan; as in some cases this is faster than what appears to be the correct index. There will be others out there who know a lot more than me about this. So basically set your indexes up and trust SQL to pick the most appropriate use of indexes (after all the database engine is designed precisely for this reason). I was taught not to use the

          gnjunge wrote:

          WITH(INDEX XXXX)

          for this reason. Regards Guy

          You always pass failure on the way to success.
          G 1 Reply Last reply
          0
          • G GuyThiebaut

            What I have been taught and understand about SQL Server is this: Let it decide on what indexes to use on the basis that 'SQL Server knows best'. Sometimes SQL Server will decide to do a table scan; as in some cases this is faster than what appears to be the correct index. There will be others out there who know a lot more than me about this. So basically set your indexes up and trust SQL to pick the most appropriate use of indexes (after all the database engine is designed precisely for this reason). I was taught not to use the

            gnjunge wrote:

            WITH(INDEX XXXX)

            for this reason. Regards Guy

            You always pass failure on the way to success.
            G Offline
            G Offline
            gnjunge
            wrote on last edited by
            #5

            You are partially right that SQL chooses the right index. But since this is a "conditional" where clause, the first time the sproc runs, SQL chooses the index that is right for that condition. But when the condition changes SQL stays with that same index. (which slows down the sproc) When I cause the sproc to recompile, and use as first run the second condition it chooses the second index as its index, and stays with that index, even when the condition changes. (thus running slow when the first condition happens).

            G 1 Reply Last reply
            0
            • G gnjunge

              You are partially right that SQL chooses the right index. But since this is a "conditional" where clause, the first time the sproc runs, SQL chooses the index that is right for that condition. But when the condition changes SQL stays with that same index. (which slows down the sproc) When I cause the sproc to recompile, and use as first run the second condition it chooses the second index as its index, and stays with that index, even when the condition changes. (thus running slow when the first condition happens).

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              You can recompile the stored procedure on each run Clickety.

              You always pass failure on the way to success.
              G 1 Reply Last reply
              0
              • G GuyThiebaut

                You can recompile the stored procedure on each run Clickety.

                You always pass failure on the way to success.
                G Offline
                G Offline
                gnjunge
                wrote on last edited by
                #7

                Good solution , cause it says that in SQL 2005 I can do statement-level recompilation which fits my needs. Thanks.

                D G 2 Replies Last reply
                0
                • G gnjunge

                  Good solution , cause it says that in SQL 2005 I can do statement-level recompilation which fits my needs. Thanks.

                  D Offline
                  D Offline
                  Dave B
                  wrote on last edited by
                  #8

                  I wouldnt get into the habit of doing that though. On a complex statement it will take a fair bit of time to analyze the query and build the plan. If you use sp_executesql then SQL Server will cache the plan associated with that statement.

                  G 2 Replies Last reply
                  0
                  • G gnjunge

                    Good solution , cause it says that in SQL 2005 I can do statement-level recompilation which fits my needs. Thanks.

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #9

                    You're welcome. :) Regards Guy

                    You always pass failure on the way to success.
                    1 Reply Last reply
                    0
                    • D Dave B

                      I wouldnt get into the habit of doing that though. On a complex statement it will take a fair bit of time to analyze the query and build the plan. If you use sp_executesql then SQL Server will cache the plan associated with that statement.

                      G Offline
                      G Offline
                      gnjunge
                      wrote on last edited by
                      #10

                      Thanks for your comment. I will test both solutions , and check which one is faster.

                      1 Reply Last reply
                      0
                      • D Dave B

                        I wouldnt get into the habit of doing that though. On a complex statement it will take a fair bit of time to analyze the query and build the plan. If you use sp_executesql then SQL Server will cache the plan associated with that statement.

                        G Offline
                        G Offline
                        gnjunge
                        wrote on last edited by
                        #11

                        Just a follow up: I used the sp_executesql method, and it works excellent.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups