can anyone help me with this awful query plan?Is it possible to optimize this query? Or any recommendations to speed it up?SHOWPLAN does not display a warning but “Include Execution Plan” does for the same queryINDEX SKIP SCAN performance on Index where first columns only contain 1 valueShould I use a subquery to help SQL Server find the correct planOdd Stream Aggregate behaviourCan I make this multiple join query faster?SQL Server 2012: An older Index that used to help a SP overnightSSIS OLE DB Source Editor Data Access Mode: “SQL command” vs “Table or view”Replace Subquery with JOIN - MYSQLStrange query plan when using OR in JOIN clause - Constant scan for every row in table

How exactly does Hawking radiation decrease the mass of black holes?

555 timer FM transmitter

Two field separators (colon and space) in awk

Is the claim "Employers won't employ people with no 'social media presence'" realistic?

What does ゆーか mean?

Check if a string is entirely made of the same substring

Elements other than carbon that can form many different compounds by bonding to themselves?

How much cash can I safely carry into the USA and avoid civil forfeiture?

What is the smallest unit of eos?

What makes accurate emulation of old systems a difficult task?

Relationship between strut and baselineskip

Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?

Can SQL Server create collisions in system generated constraint names?

How can I practically buy stocks?

Multiple options vs single option UI

On The Origin of Dissonant Chords

Dynamic SOQL query relationship with field visibility for Users

Function pointer with named arguments?

Which big number is bigger?

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

a sore throat vs a strep throat vs strep throat

Re-entry to Germany after vacation using blue card

"The cow" OR "a cow" OR "cows" in this context

Can I grease a crank spindle/bracket without disassembling the crank set?



can anyone help me with this awful query plan?


Is it possible to optimize this query? Or any recommendations to speed it up?SHOWPLAN does not display a warning but “Include Execution Plan” does for the same queryINDEX SKIP SCAN performance on Index where first columns only contain 1 valueShould I use a subquery to help SQL Server find the correct planOdd Stream Aggregate behaviourCan I make this multiple join query faster?SQL Server 2012: An older Index that used to help a SP overnightSSIS OLE DB Source Editor Data Access Mode: “SQL command” vs “Table or view”Replace Subquery with JOIN - MYSQLStrange query plan when using OR in JOIN clause - Constant scan for every row in table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















The query:



 SELECT Object1.Column1, Object2.Column2 AS Column3, Object2.Column4 AS Column5, 
Object3.Column6, Object3.Column7,Object1.Column8, Object1.Column9,
Object1.Column10, Object1.Column11, Object1.Column12, Object1.Column13,
Object1.Column14, Object1.Column15 as Column15, Object1.Column16,
Object4.Column4 AS Column17, Object4.Column2 AS Column18, Object1.Column19,
Object1.Column20, Object1.Column21, Object1.Column22, Object1.Column23,
Object1.Column24, Object1.Column25, Object1.Column26, Object5.Column4,
Object1.Column27, Object1.Column28, Object1.Column29, Object3.Column30,
Object3.Column1 as Column31, Object3.Column32 as Column33, Object1.Column34
as Column34, ? AS Column35 , Object3.Column36 as Column37
FROM Object6 AS Object1
INNER JOIN Object7 AS Object3 ON Object1.Column38 = Object3.Column1
INNER JOIN Object8 AS Object2 ON Object3.Column30 = Object2.Column1
LEFT JOIN Object9 AS Object4 ON Object1.Column16 = Object4.Column2
LEFT JOIN Object10 AS Object5 ON Object1.Column9 = Object5.Column2
WHERE Object2.Column1 <> ? AND Object1.Column8 = ?
AND ( coalesce(Column16,?)= ? )
AND EXISTS (
SELECT ?
FROM Object11
WHERE Column39 = ?
AND Column30 = Object3.Column30)
ORDER BY Column7 desc
OFFSET ? ROWS FETCH FIRST ? ROWS ONLY


here is the query plan



I know that I should maybe add an index on this:



Database1.Schema1.Object7.Column30, Database1.Schema1.Object7.Column36, Database1.Schema1.Object7.Column6, Database1.Schema1.Object7.Column32



but one of this columns is a varchar 4000 and it can't be created cause of the big dimension of the field.



I noticed that it takes 25 second only if the rows returned are fewer than the fetch first number










share|improve this question
























  • Hi, try to add OPTION(FORCE ORDER) to your query

    – Denis Rubashkin
    11 hours ago

















7















The query:



 SELECT Object1.Column1, Object2.Column2 AS Column3, Object2.Column4 AS Column5, 
Object3.Column6, Object3.Column7,Object1.Column8, Object1.Column9,
Object1.Column10, Object1.Column11, Object1.Column12, Object1.Column13,
Object1.Column14, Object1.Column15 as Column15, Object1.Column16,
Object4.Column4 AS Column17, Object4.Column2 AS Column18, Object1.Column19,
Object1.Column20, Object1.Column21, Object1.Column22, Object1.Column23,
Object1.Column24, Object1.Column25, Object1.Column26, Object5.Column4,
Object1.Column27, Object1.Column28, Object1.Column29, Object3.Column30,
Object3.Column1 as Column31, Object3.Column32 as Column33, Object1.Column34
as Column34, ? AS Column35 , Object3.Column36 as Column37
FROM Object6 AS Object1
INNER JOIN Object7 AS Object3 ON Object1.Column38 = Object3.Column1
INNER JOIN Object8 AS Object2 ON Object3.Column30 = Object2.Column1
LEFT JOIN Object9 AS Object4 ON Object1.Column16 = Object4.Column2
LEFT JOIN Object10 AS Object5 ON Object1.Column9 = Object5.Column2
WHERE Object2.Column1 <> ? AND Object1.Column8 = ?
AND ( coalesce(Column16,?)= ? )
AND EXISTS (
SELECT ?
FROM Object11
WHERE Column39 = ?
AND Column30 = Object3.Column30)
ORDER BY Column7 desc
OFFSET ? ROWS FETCH FIRST ? ROWS ONLY


here is the query plan



I know that I should maybe add an index on this:



Database1.Schema1.Object7.Column30, Database1.Schema1.Object7.Column36, Database1.Schema1.Object7.Column6, Database1.Schema1.Object7.Column32



but one of this columns is a varchar 4000 and it can't be created cause of the big dimension of the field.



I noticed that it takes 25 second only if the rows returned are fewer than the fetch first number










share|improve this question
























  • Hi, try to add OPTION(FORCE ORDER) to your query

    – Denis Rubashkin
    11 hours ago













7












7








7


1






The query:



 SELECT Object1.Column1, Object2.Column2 AS Column3, Object2.Column4 AS Column5, 
Object3.Column6, Object3.Column7,Object1.Column8, Object1.Column9,
Object1.Column10, Object1.Column11, Object1.Column12, Object1.Column13,
Object1.Column14, Object1.Column15 as Column15, Object1.Column16,
Object4.Column4 AS Column17, Object4.Column2 AS Column18, Object1.Column19,
Object1.Column20, Object1.Column21, Object1.Column22, Object1.Column23,
Object1.Column24, Object1.Column25, Object1.Column26, Object5.Column4,
Object1.Column27, Object1.Column28, Object1.Column29, Object3.Column30,
Object3.Column1 as Column31, Object3.Column32 as Column33, Object1.Column34
as Column34, ? AS Column35 , Object3.Column36 as Column37
FROM Object6 AS Object1
INNER JOIN Object7 AS Object3 ON Object1.Column38 = Object3.Column1
INNER JOIN Object8 AS Object2 ON Object3.Column30 = Object2.Column1
LEFT JOIN Object9 AS Object4 ON Object1.Column16 = Object4.Column2
LEFT JOIN Object10 AS Object5 ON Object1.Column9 = Object5.Column2
WHERE Object2.Column1 <> ? AND Object1.Column8 = ?
AND ( coalesce(Column16,?)= ? )
AND EXISTS (
SELECT ?
FROM Object11
WHERE Column39 = ?
AND Column30 = Object3.Column30)
ORDER BY Column7 desc
OFFSET ? ROWS FETCH FIRST ? ROWS ONLY


here is the query plan



I know that I should maybe add an index on this:



Database1.Schema1.Object7.Column30, Database1.Schema1.Object7.Column36, Database1.Schema1.Object7.Column6, Database1.Schema1.Object7.Column32



but one of this columns is a varchar 4000 and it can't be created cause of the big dimension of the field.



I noticed that it takes 25 second only if the rows returned are fewer than the fetch first number










share|improve this question
















The query:



 SELECT Object1.Column1, Object2.Column2 AS Column3, Object2.Column4 AS Column5, 
Object3.Column6, Object3.Column7,Object1.Column8, Object1.Column9,
Object1.Column10, Object1.Column11, Object1.Column12, Object1.Column13,
Object1.Column14, Object1.Column15 as Column15, Object1.Column16,
Object4.Column4 AS Column17, Object4.Column2 AS Column18, Object1.Column19,
Object1.Column20, Object1.Column21, Object1.Column22, Object1.Column23,
Object1.Column24, Object1.Column25, Object1.Column26, Object5.Column4,
Object1.Column27, Object1.Column28, Object1.Column29, Object3.Column30,
Object3.Column1 as Column31, Object3.Column32 as Column33, Object1.Column34
as Column34, ? AS Column35 , Object3.Column36 as Column37
FROM Object6 AS Object1
INNER JOIN Object7 AS Object3 ON Object1.Column38 = Object3.Column1
INNER JOIN Object8 AS Object2 ON Object3.Column30 = Object2.Column1
LEFT JOIN Object9 AS Object4 ON Object1.Column16 = Object4.Column2
LEFT JOIN Object10 AS Object5 ON Object1.Column9 = Object5.Column2
WHERE Object2.Column1 <> ? AND Object1.Column8 = ?
AND ( coalesce(Column16,?)= ? )
AND EXISTS (
SELECT ?
FROM Object11
WHERE Column39 = ?
AND Column30 = Object3.Column30)
ORDER BY Column7 desc
OFFSET ? ROWS FETCH FIRST ? ROWS ONLY


here is the query plan



I know that I should maybe add an index on this:



Database1.Schema1.Object7.Column30, Database1.Schema1.Object7.Column36, Database1.Schema1.Object7.Column6, Database1.Schema1.Object7.Column32



but one of this columns is a varchar 4000 and it can't be created cause of the big dimension of the field.



I noticed that it takes 25 second only if the rows returned are fewer than the fetch first number







sql-server query-performance execution-plan sql-server-2017






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago









Philᵀᴹ

26k65691




26k65691










asked 11 hours ago









Gabriele D'OnufrioGabriele D'Onufrio

649




649












  • Hi, try to add OPTION(FORCE ORDER) to your query

    – Denis Rubashkin
    11 hours ago

















  • Hi, try to add OPTION(FORCE ORDER) to your query

    – Denis Rubashkin
    11 hours ago
















Hi, try to add OPTION(FORCE ORDER) to your query

– Denis Rubashkin
11 hours ago





Hi, try to add OPTION(FORCE ORDER) to your query

– Denis Rubashkin
11 hours ago










2 Answers
2






active

oldest

votes


















8














enter image description here



The execution plan accesses Object7 first using a non covering index in Column7 order. It then does some key lookups on that table and nested loops joins against the other tables with the final joined resulting arriving at the TOP operator still ordered by Column7.



Once this has received enough rows to satisfy the OFFSET ... FETCH it can stop requesting any more rows from downstream operators. SQL Server estimates that it will only need to read 2419 rows from the initial index on Object7.Column7 before this point is arrived at.



This estimate is not at all correct. In fact it ends up reading the entirety of Object7 and likely runs out of rows before the OFFSET ... FETCH is satisfied.



The semi join on Object11 reduces the rowcount by almost half but the killer is the join on Object6 and predicate on the same table. Together these reduce the 9,753,116 rows coming out of the semijoin to 2.



You could try spending some time looking at statistics on the tables involved to try and get the cardinality estimates from these joins to be more accurate or alternatively you could add OPTION (USE HINT ('DISABLE_OPTIMIZER_ROWGOAL') ) so the plan is costed without the assumption that it can stop early due to the OFFSET ... FETCH - this will certainly give you a different plan.






share|improve this answer






























    0














    If you can add an index on Object11, Column39 + Column30, and an index on Object7, Column30, with other fields from Object7 in the INCLUDE portion of the CREATE INDEX statement for Object 7, you should have a large increase in performance. This is the vast majority of the resource expenditure involved in this query.



    Based on the plan's XML, these would appear to be close to optimal indexes for this query:



    CREATE INDEX Idx_Object11_Column39_Column30
    ON Object11(Column39_Column30)

    CREATE INDEX Idx_Object7_Column30_Column1_Includes
    ON Object7 (Column30, Column1)
    INCLUDE (Column7, Column36, Column6, Column2)





    share|improve this answer























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



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f236785%2fcan-anyone-help-me-with-this-awful-query-plan%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      enter image description here



      The execution plan accesses Object7 first using a non covering index in Column7 order. It then does some key lookups on that table and nested loops joins against the other tables with the final joined resulting arriving at the TOP operator still ordered by Column7.



      Once this has received enough rows to satisfy the OFFSET ... FETCH it can stop requesting any more rows from downstream operators. SQL Server estimates that it will only need to read 2419 rows from the initial index on Object7.Column7 before this point is arrived at.



      This estimate is not at all correct. In fact it ends up reading the entirety of Object7 and likely runs out of rows before the OFFSET ... FETCH is satisfied.



      The semi join on Object11 reduces the rowcount by almost half but the killer is the join on Object6 and predicate on the same table. Together these reduce the 9,753,116 rows coming out of the semijoin to 2.



      You could try spending some time looking at statistics on the tables involved to try and get the cardinality estimates from these joins to be more accurate or alternatively you could add OPTION (USE HINT ('DISABLE_OPTIMIZER_ROWGOAL') ) so the plan is costed without the assumption that it can stop early due to the OFFSET ... FETCH - this will certainly give you a different plan.






      share|improve this answer



























        8














        enter image description here



        The execution plan accesses Object7 first using a non covering index in Column7 order. It then does some key lookups on that table and nested loops joins against the other tables with the final joined resulting arriving at the TOP operator still ordered by Column7.



        Once this has received enough rows to satisfy the OFFSET ... FETCH it can stop requesting any more rows from downstream operators. SQL Server estimates that it will only need to read 2419 rows from the initial index on Object7.Column7 before this point is arrived at.



        This estimate is not at all correct. In fact it ends up reading the entirety of Object7 and likely runs out of rows before the OFFSET ... FETCH is satisfied.



        The semi join on Object11 reduces the rowcount by almost half but the killer is the join on Object6 and predicate on the same table. Together these reduce the 9,753,116 rows coming out of the semijoin to 2.



        You could try spending some time looking at statistics on the tables involved to try and get the cardinality estimates from these joins to be more accurate or alternatively you could add OPTION (USE HINT ('DISABLE_OPTIMIZER_ROWGOAL') ) so the plan is costed without the assumption that it can stop early due to the OFFSET ... FETCH - this will certainly give you a different plan.






        share|improve this answer

























          8












          8








          8







          enter image description here



          The execution plan accesses Object7 first using a non covering index in Column7 order. It then does some key lookups on that table and nested loops joins against the other tables with the final joined resulting arriving at the TOP operator still ordered by Column7.



          Once this has received enough rows to satisfy the OFFSET ... FETCH it can stop requesting any more rows from downstream operators. SQL Server estimates that it will only need to read 2419 rows from the initial index on Object7.Column7 before this point is arrived at.



          This estimate is not at all correct. In fact it ends up reading the entirety of Object7 and likely runs out of rows before the OFFSET ... FETCH is satisfied.



          The semi join on Object11 reduces the rowcount by almost half but the killer is the join on Object6 and predicate on the same table. Together these reduce the 9,753,116 rows coming out of the semijoin to 2.



          You could try spending some time looking at statistics on the tables involved to try and get the cardinality estimates from these joins to be more accurate or alternatively you could add OPTION (USE HINT ('DISABLE_OPTIMIZER_ROWGOAL') ) so the plan is costed without the assumption that it can stop early due to the OFFSET ... FETCH - this will certainly give you a different plan.






          share|improve this answer













          enter image description here



          The execution plan accesses Object7 first using a non covering index in Column7 order. It then does some key lookups on that table and nested loops joins against the other tables with the final joined resulting arriving at the TOP operator still ordered by Column7.



          Once this has received enough rows to satisfy the OFFSET ... FETCH it can stop requesting any more rows from downstream operators. SQL Server estimates that it will only need to read 2419 rows from the initial index on Object7.Column7 before this point is arrived at.



          This estimate is not at all correct. In fact it ends up reading the entirety of Object7 and likely runs out of rows before the OFFSET ... FETCH is satisfied.



          The semi join on Object11 reduces the rowcount by almost half but the killer is the join on Object6 and predicate on the same table. Together these reduce the 9,753,116 rows coming out of the semijoin to 2.



          You could try spending some time looking at statistics on the tables involved to try and get the cardinality estimates from these joins to be more accurate or alternatively you could add OPTION (USE HINT ('DISABLE_OPTIMIZER_ROWGOAL') ) so the plan is costed without the assumption that it can stop early due to the OFFSET ... FETCH - this will certainly give you a different plan.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 8 hours ago









          Martin SmithMartin Smith

          64.9k10175261




          64.9k10175261























              0














              If you can add an index on Object11, Column39 + Column30, and an index on Object7, Column30, with other fields from Object7 in the INCLUDE portion of the CREATE INDEX statement for Object 7, you should have a large increase in performance. This is the vast majority of the resource expenditure involved in this query.



              Based on the plan's XML, these would appear to be close to optimal indexes for this query:



              CREATE INDEX Idx_Object11_Column39_Column30
              ON Object11(Column39_Column30)

              CREATE INDEX Idx_Object7_Column30_Column1_Includes
              ON Object7 (Column30, Column1)
              INCLUDE (Column7, Column36, Column6, Column2)





              share|improve this answer



























                0














                If you can add an index on Object11, Column39 + Column30, and an index on Object7, Column30, with other fields from Object7 in the INCLUDE portion of the CREATE INDEX statement for Object 7, you should have a large increase in performance. This is the vast majority of the resource expenditure involved in this query.



                Based on the plan's XML, these would appear to be close to optimal indexes for this query:



                CREATE INDEX Idx_Object11_Column39_Column30
                ON Object11(Column39_Column30)

                CREATE INDEX Idx_Object7_Column30_Column1_Includes
                ON Object7 (Column30, Column1)
                INCLUDE (Column7, Column36, Column6, Column2)





                share|improve this answer

























                  0












                  0








                  0







                  If you can add an index on Object11, Column39 + Column30, and an index on Object7, Column30, with other fields from Object7 in the INCLUDE portion of the CREATE INDEX statement for Object 7, you should have a large increase in performance. This is the vast majority of the resource expenditure involved in this query.



                  Based on the plan's XML, these would appear to be close to optimal indexes for this query:



                  CREATE INDEX Idx_Object11_Column39_Column30
                  ON Object11(Column39_Column30)

                  CREATE INDEX Idx_Object7_Column30_Column1_Includes
                  ON Object7 (Column30, Column1)
                  INCLUDE (Column7, Column36, Column6, Column2)





                  share|improve this answer













                  If you can add an index on Object11, Column39 + Column30, and an index on Object7, Column30, with other fields from Object7 in the INCLUDE portion of the CREATE INDEX statement for Object 7, you should have a large increase in performance. This is the vast majority of the resource expenditure involved in this query.



                  Based on the plan's XML, these would appear to be close to optimal indexes for this query:



                  CREATE INDEX Idx_Object11_Column39_Column30
                  ON Object11(Column39_Column30)

                  CREATE INDEX Idx_Object7_Column30_Column1_Includes
                  ON Object7 (Column30, Column1)
                  INCLUDE (Column7, Column36, Column6, Column2)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 7 hours ago









                  Laughing VergilLaughing Vergil

                  943212




                  943212



























                      draft saved

                      draft discarded
















































                      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%2f236785%2fcan-anyone-help-me-with-this-awful-query-plan%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

                      Nidaros erkebispedøme

                      Birsay

                      Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?