Covering Index Changes Execution Plan but is not used












4















I have the following occasionally slow running query:



SELECT C.CustomerID 
FROM dbo.Customers C WITH (NOLOCK)
WHERE C.Forename = @Forename
AND C.Surname = @Surname
OPTION (RECOMPILE)


CustomerID is the Primary Key on the Customers table. The Customers table also has the following two non-clustered indexes:



CREATE NONCLUSTERED INDEX idx_Forename ON Customers (Forename ASC)
CREATE NONCLUSTERED INDEX idx_Surname ON Customers (Surname ASC)


When I run the query with both a surname and forename entered the query optimiser uses the index 'idx_Surname' as in the following execution plan:



Execution Plan without covering index



This query takes over two minutes to complete for this particular search and finds no results. For the values entered @Forename has no matches in the Customers table while @Surname matches 31,162 records. When I only search by the @surname the 31,162 records return in under a second with the following plan:



Execution Plan surname only search



In an attempt to optimise the query for searches containing both Forename and Surname I added the following covering index:



CREATE NONCLUSTERED INDEX idx_Surname_Covering ON dbo.Customers (Surname) INCLUDE (Forename)


The query with both Forename and Surname then returns in less than one second. However, the covering index is not used in the actual execution plan:



Execution Plan with covering index



So,




  1. Is the covering index required or is there a better way to improve the performance and

  2. Why does the additional covering index cause the change of index in the actual execution plan from idx_Forename to idx_Surname?


p.s. the query above is an isolated example, when in use either surname or forename or both may be searched for and the Customers table also includes other searchable columns with their own indexes. This detail was not considered relevant to the question so I have not included it.










share|improve this question







New contributor




Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    4















    I have the following occasionally slow running query:



    SELECT C.CustomerID 
    FROM dbo.Customers C WITH (NOLOCK)
    WHERE C.Forename = @Forename
    AND C.Surname = @Surname
    OPTION (RECOMPILE)


    CustomerID is the Primary Key on the Customers table. The Customers table also has the following two non-clustered indexes:



    CREATE NONCLUSTERED INDEX idx_Forename ON Customers (Forename ASC)
    CREATE NONCLUSTERED INDEX idx_Surname ON Customers (Surname ASC)


    When I run the query with both a surname and forename entered the query optimiser uses the index 'idx_Surname' as in the following execution plan:



    Execution Plan without covering index



    This query takes over two minutes to complete for this particular search and finds no results. For the values entered @Forename has no matches in the Customers table while @Surname matches 31,162 records. When I only search by the @surname the 31,162 records return in under a second with the following plan:



    Execution Plan surname only search



    In an attempt to optimise the query for searches containing both Forename and Surname I added the following covering index:



    CREATE NONCLUSTERED INDEX idx_Surname_Covering ON dbo.Customers (Surname) INCLUDE (Forename)


    The query with both Forename and Surname then returns in less than one second. However, the covering index is not used in the actual execution plan:



    Execution Plan with covering index



    So,




    1. Is the covering index required or is there a better way to improve the performance and

    2. Why does the additional covering index cause the change of index in the actual execution plan from idx_Forename to idx_Surname?


    p.s. the query above is an isolated example, when in use either surname or forename or both may be searched for and the Customers table also includes other searchable columns with their own indexes. This detail was not considered relevant to the question so I have not included it.










    share|improve this question







    New contributor




    Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      4












      4








      4








      I have the following occasionally slow running query:



      SELECT C.CustomerID 
      FROM dbo.Customers C WITH (NOLOCK)
      WHERE C.Forename = @Forename
      AND C.Surname = @Surname
      OPTION (RECOMPILE)


      CustomerID is the Primary Key on the Customers table. The Customers table also has the following two non-clustered indexes:



      CREATE NONCLUSTERED INDEX idx_Forename ON Customers (Forename ASC)
      CREATE NONCLUSTERED INDEX idx_Surname ON Customers (Surname ASC)


      When I run the query with both a surname and forename entered the query optimiser uses the index 'idx_Surname' as in the following execution plan:



      Execution Plan without covering index



      This query takes over two minutes to complete for this particular search and finds no results. For the values entered @Forename has no matches in the Customers table while @Surname matches 31,162 records. When I only search by the @surname the 31,162 records return in under a second with the following plan:



      Execution Plan surname only search



      In an attempt to optimise the query for searches containing both Forename and Surname I added the following covering index:



      CREATE NONCLUSTERED INDEX idx_Surname_Covering ON dbo.Customers (Surname) INCLUDE (Forename)


      The query with both Forename and Surname then returns in less than one second. However, the covering index is not used in the actual execution plan:



      Execution Plan with covering index



      So,




      1. Is the covering index required or is there a better way to improve the performance and

      2. Why does the additional covering index cause the change of index in the actual execution plan from idx_Forename to idx_Surname?


      p.s. the query above is an isolated example, when in use either surname or forename or both may be searched for and the Customers table also includes other searchable columns with their own indexes. This detail was not considered relevant to the question so I have not included it.










      share|improve this question







      New contributor




      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have the following occasionally slow running query:



      SELECT C.CustomerID 
      FROM dbo.Customers C WITH (NOLOCK)
      WHERE C.Forename = @Forename
      AND C.Surname = @Surname
      OPTION (RECOMPILE)


      CustomerID is the Primary Key on the Customers table. The Customers table also has the following two non-clustered indexes:



      CREATE NONCLUSTERED INDEX idx_Forename ON Customers (Forename ASC)
      CREATE NONCLUSTERED INDEX idx_Surname ON Customers (Surname ASC)


      When I run the query with both a surname and forename entered the query optimiser uses the index 'idx_Surname' as in the following execution plan:



      Execution Plan without covering index



      This query takes over two minutes to complete for this particular search and finds no results. For the values entered @Forename has no matches in the Customers table while @Surname matches 31,162 records. When I only search by the @surname the 31,162 records return in under a second with the following plan:



      Execution Plan surname only search



      In an attempt to optimise the query for searches containing both Forename and Surname I added the following covering index:



      CREATE NONCLUSTERED INDEX idx_Surname_Covering ON dbo.Customers (Surname) INCLUDE (Forename)


      The query with both Forename and Surname then returns in less than one second. However, the covering index is not used in the actual execution plan:



      Execution Plan with covering index



      So,




      1. Is the covering index required or is there a better way to improve the performance and

      2. Why does the additional covering index cause the change of index in the actual execution plan from idx_Forename to idx_Surname?


      p.s. the query above is an isolated example, when in use either surname or forename or both may be searched for and the Customers table also includes other searchable columns with their own indexes. This detail was not considered relevant to the question so I have not included it.







      sql-server index query-performance sql-server-2016 execution-plan






      share|improve this question







      New contributor




      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      FletchFletch

      704




      704




      New contributor




      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Fletch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          5















          1) Is the covering index required or is there a better way to improve the
          performance




          Best index



          The best index would be the most covering, selective index for the queries accessing the table.



          Take for example in your table, you have 50000 rows where the firstname = John , but only one where the last name = 'McClane', Should you create the index with John as the First key value or McClane?



          Answer:



          It depends... If you are always searching for John Mcclane, then its an open and shut case of indexing the lastname first. But what if there are also queries searching for Constanthin Smith? You could have over 5000 Smiths, but only five Constanthin's.



          As a result, it depends on your queries and what you are seeking on, how much they are executed, ....



          If your queries are going to always seek on both the firstname and lastname, then it is the simple case of picking the more selective one as the first key column.
          Keeping in mind that read performance improving should stay bigger than write performance declining.



          Ofcourse, nobody restricts you to create two indexes, one with (firstname,lastname )and one with (lastname,firstname).



          (Your update / insert / delete statements might).



          Not considering filtered indexes and whatnot, the best index for your example would be:



          CREATE NONCLUSTERED INDEX idx_Forename_Surname ON dbo.Customers (Forename,Surname)





          2) Why does the additional covering index cause the change of index in
          the actual execution plan from idx_Forename to idx_Surname?




          I don't think that this was just because of the index, but because of the statistics created as a result of the index creation.



          Even though these stats are the same as the one in idx_Surname, my guess is that they are with a bigger sample rate (100), as they are created with 'fullscan'.



          If an auto update stats occured on the statistics created by the index idx_Surname, they might have had a smaller sample rate, resulting in bad estimates (E.g. 1% Sample rate).



          You could try removing the idx_Surname_Covering index and its statisics and updating the stats on dbo.Customers with 100% sample rate (fullscan) to test this theory.



          UPDATE STATISTICS dbo.Customers WITH FULLSCAN


          Which hopefully changes your plan to use the better seek.



          If this is why your query changed, and updating the stats with fullscan on maintenance windows is not a viable option, you could change the sample rate






          share|improve this answer





















          • 1





            thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

            – Fletch
            yesterday






          • 1





            @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

            – Randi Vertongen
            yesterday






          • 1





            @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

            – KumarHarsh
            23 hours ago






          • 1





            @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

            – Fletch
            19 hours ago











          • @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

            – Fletch
            19 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Fletch is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228442%2fcovering-index-changes-execution-plan-but-is-not-used%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5















          1) Is the covering index required or is there a better way to improve the
          performance




          Best index



          The best index would be the most covering, selective index for the queries accessing the table.



          Take for example in your table, you have 50000 rows where the firstname = John , but only one where the last name = 'McClane', Should you create the index with John as the First key value or McClane?



          Answer:



          It depends... If you are always searching for John Mcclane, then its an open and shut case of indexing the lastname first. But what if there are also queries searching for Constanthin Smith? You could have over 5000 Smiths, but only five Constanthin's.



          As a result, it depends on your queries and what you are seeking on, how much they are executed, ....



          If your queries are going to always seek on both the firstname and lastname, then it is the simple case of picking the more selective one as the first key column.
          Keeping in mind that read performance improving should stay bigger than write performance declining.



          Ofcourse, nobody restricts you to create two indexes, one with (firstname,lastname )and one with (lastname,firstname).



          (Your update / insert / delete statements might).



          Not considering filtered indexes and whatnot, the best index for your example would be:



          CREATE NONCLUSTERED INDEX idx_Forename_Surname ON dbo.Customers (Forename,Surname)





          2) Why does the additional covering index cause the change of index in
          the actual execution plan from idx_Forename to idx_Surname?




          I don't think that this was just because of the index, but because of the statistics created as a result of the index creation.



          Even though these stats are the same as the one in idx_Surname, my guess is that they are with a bigger sample rate (100), as they are created with 'fullscan'.



          If an auto update stats occured on the statistics created by the index idx_Surname, they might have had a smaller sample rate, resulting in bad estimates (E.g. 1% Sample rate).



          You could try removing the idx_Surname_Covering index and its statisics and updating the stats on dbo.Customers with 100% sample rate (fullscan) to test this theory.



          UPDATE STATISTICS dbo.Customers WITH FULLSCAN


          Which hopefully changes your plan to use the better seek.



          If this is why your query changed, and updating the stats with fullscan on maintenance windows is not a viable option, you could change the sample rate






          share|improve this answer





















          • 1





            thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

            – Fletch
            yesterday






          • 1





            @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

            – Randi Vertongen
            yesterday






          • 1





            @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

            – KumarHarsh
            23 hours ago






          • 1





            @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

            – Fletch
            19 hours ago











          • @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

            – Fletch
            19 hours ago
















          5















          1) Is the covering index required or is there a better way to improve the
          performance




          Best index



          The best index would be the most covering, selective index for the queries accessing the table.



          Take for example in your table, you have 50000 rows where the firstname = John , but only one where the last name = 'McClane', Should you create the index with John as the First key value or McClane?



          Answer:



          It depends... If you are always searching for John Mcclane, then its an open and shut case of indexing the lastname first. But what if there are also queries searching for Constanthin Smith? You could have over 5000 Smiths, but only five Constanthin's.



          As a result, it depends on your queries and what you are seeking on, how much they are executed, ....



          If your queries are going to always seek on both the firstname and lastname, then it is the simple case of picking the more selective one as the first key column.
          Keeping in mind that read performance improving should stay bigger than write performance declining.



          Ofcourse, nobody restricts you to create two indexes, one with (firstname,lastname )and one with (lastname,firstname).



          (Your update / insert / delete statements might).



          Not considering filtered indexes and whatnot, the best index for your example would be:



          CREATE NONCLUSTERED INDEX idx_Forename_Surname ON dbo.Customers (Forename,Surname)





          2) Why does the additional covering index cause the change of index in
          the actual execution plan from idx_Forename to idx_Surname?




          I don't think that this was just because of the index, but because of the statistics created as a result of the index creation.



          Even though these stats are the same as the one in idx_Surname, my guess is that they are with a bigger sample rate (100), as they are created with 'fullscan'.



          If an auto update stats occured on the statistics created by the index idx_Surname, they might have had a smaller sample rate, resulting in bad estimates (E.g. 1% Sample rate).



          You could try removing the idx_Surname_Covering index and its statisics and updating the stats on dbo.Customers with 100% sample rate (fullscan) to test this theory.



          UPDATE STATISTICS dbo.Customers WITH FULLSCAN


          Which hopefully changes your plan to use the better seek.



          If this is why your query changed, and updating the stats with fullscan on maintenance windows is not a viable option, you could change the sample rate






          share|improve this answer





















          • 1





            thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

            – Fletch
            yesterday






          • 1





            @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

            – Randi Vertongen
            yesterday






          • 1





            @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

            – KumarHarsh
            23 hours ago






          • 1





            @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

            – Fletch
            19 hours ago











          • @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

            – Fletch
            19 hours ago














          5












          5








          5








          1) Is the covering index required or is there a better way to improve the
          performance




          Best index



          The best index would be the most covering, selective index for the queries accessing the table.



          Take for example in your table, you have 50000 rows where the firstname = John , but only one where the last name = 'McClane', Should you create the index with John as the First key value or McClane?



          Answer:



          It depends... If you are always searching for John Mcclane, then its an open and shut case of indexing the lastname first. But what if there are also queries searching for Constanthin Smith? You could have over 5000 Smiths, but only five Constanthin's.



          As a result, it depends on your queries and what you are seeking on, how much they are executed, ....



          If your queries are going to always seek on both the firstname and lastname, then it is the simple case of picking the more selective one as the first key column.
          Keeping in mind that read performance improving should stay bigger than write performance declining.



          Ofcourse, nobody restricts you to create two indexes, one with (firstname,lastname )and one with (lastname,firstname).



          (Your update / insert / delete statements might).



          Not considering filtered indexes and whatnot, the best index for your example would be:



          CREATE NONCLUSTERED INDEX idx_Forename_Surname ON dbo.Customers (Forename,Surname)





          2) Why does the additional covering index cause the change of index in
          the actual execution plan from idx_Forename to idx_Surname?




          I don't think that this was just because of the index, but because of the statistics created as a result of the index creation.



          Even though these stats are the same as the one in idx_Surname, my guess is that they are with a bigger sample rate (100), as they are created with 'fullscan'.



          If an auto update stats occured on the statistics created by the index idx_Surname, they might have had a smaller sample rate, resulting in bad estimates (E.g. 1% Sample rate).



          You could try removing the idx_Surname_Covering index and its statisics and updating the stats on dbo.Customers with 100% sample rate (fullscan) to test this theory.



          UPDATE STATISTICS dbo.Customers WITH FULLSCAN


          Which hopefully changes your plan to use the better seek.



          If this is why your query changed, and updating the stats with fullscan on maintenance windows is not a viable option, you could change the sample rate






          share|improve this answer
















          1) Is the covering index required or is there a better way to improve the
          performance




          Best index



          The best index would be the most covering, selective index for the queries accessing the table.



          Take for example in your table, you have 50000 rows where the firstname = John , but only one where the last name = 'McClane', Should you create the index with John as the First key value or McClane?



          Answer:



          It depends... If you are always searching for John Mcclane, then its an open and shut case of indexing the lastname first. But what if there are also queries searching for Constanthin Smith? You could have over 5000 Smiths, but only five Constanthin's.



          As a result, it depends on your queries and what you are seeking on, how much they are executed, ....



          If your queries are going to always seek on both the firstname and lastname, then it is the simple case of picking the more selective one as the first key column.
          Keeping in mind that read performance improving should stay bigger than write performance declining.



          Ofcourse, nobody restricts you to create two indexes, one with (firstname,lastname )and one with (lastname,firstname).



          (Your update / insert / delete statements might).



          Not considering filtered indexes and whatnot, the best index for your example would be:



          CREATE NONCLUSTERED INDEX idx_Forename_Surname ON dbo.Customers (Forename,Surname)





          2) Why does the additional covering index cause the change of index in
          the actual execution plan from idx_Forename to idx_Surname?




          I don't think that this was just because of the index, but because of the statistics created as a result of the index creation.



          Even though these stats are the same as the one in idx_Surname, my guess is that they are with a bigger sample rate (100), as they are created with 'fullscan'.



          If an auto update stats occured on the statistics created by the index idx_Surname, they might have had a smaller sample rate, resulting in bad estimates (E.g. 1% Sample rate).



          You could try removing the idx_Surname_Covering index and its statisics and updating the stats on dbo.Customers with 100% sample rate (fullscan) to test this theory.



          UPDATE STATISTICS dbo.Customers WITH FULLSCAN


          Which hopefully changes your plan to use the better seek.



          If this is why your query changed, and updating the stats with fullscan on maintenance windows is not a viable option, you could change the sample rate







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Randi VertongenRandi Vertongen

          2,201519




          2,201519








          • 1





            thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

            – Fletch
            yesterday






          • 1





            @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

            – Randi Vertongen
            yesterday






          • 1





            @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

            – KumarHarsh
            23 hours ago






          • 1





            @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

            – Fletch
            19 hours ago











          • @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

            – Fletch
            19 hours ago














          • 1





            thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

            – Fletch
            yesterday






          • 1





            @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

            – Randi Vertongen
            yesterday






          • 1





            @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

            – KumarHarsh
            23 hours ago






          • 1





            @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

            – Fletch
            19 hours ago











          • @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

            – Fletch
            19 hours ago








          1




          1





          thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

          – Fletch
          yesterday





          thanks for your answer. Interestingly dropping the index and updating the statistics had the same effect on the execution plan.

          – Fletch
          yesterday




          1




          1





          @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

          – Randi Vertongen
          yesterday





          @Fletch as in, that afterwards the better query plan was chosen? Then you should look into the stats on that table. The distribution of your data might be skewed, or your stats did not get updated for some reason.

          – Randi Vertongen
          yesterday




          1




          1





          @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

          – KumarHarsh
          23 hours ago





          @Fletch, It is very difficult to find most Selective column in column like Name.So 2 non clustered index can be created each covering the other columns.

          – KumarHarsh
          23 hours ago




          1




          1





          @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

          – Fletch
          19 hours ago





          @RandiVertongen yes as in the better query plan was chosen. This query was run on a database with obfuscated data in a development environment and so statistics aren't being maintained. I needed to understand why the query plan was changing and your answer helped me to see that it was because of the statistics.

          – Fletch
          19 hours ago













          @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

          – Fletch
          19 hours ago





          @KumarHarsh yes I agree neither column will always be more selective than the other so it may require two indexes as per Randi's answer. I have made some changes to the query without the additional indexes but if some of the searches are still slow then I will try adding both indexes. Thanks

          – Fletch
          19 hours ago










          Fletch is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Fletch is a new contributor. Be nice, and check out our Code of Conduct.













          Fletch is a new contributor. Be nice, and check out our Code of Conduct.












          Fletch is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228442%2fcovering-index-changes-execution-plan-but-is-not-used%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy