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 to properly store the current value of int variable into a token list?

My large rocket is still flipping over

Make me a minimum magic sum

In Futurama, how many beings has Leela slept with?

Why would one crossvalidate the random state number?

Counting the Number of Real Roots of A Polynomial

Sci-fi/fantasy book - ships on steel runners skating across ice sheets

All superlinear runtime algorithms are asymptotically equivalent to convex function?

Enabling a minor mode in all but some buffers

Is any special diet an effective treatment of autism?

Where are the "shires" in the UK?

How to pass hash as password to ssh server

Understanding ties

Why does sound not move through a wall?

Which US defense organization would respond to an invasion like this?

Is 'contemporary' ambiguous and if so is there a better word?

GitLab account hacked and repo wiped

Has the Hulk always been able to talk?

It isn’t that you must stop now

Is there a proof that the set of real numbers can exactly represent distances?

Does running exec do anything?

about academic proof-reading, what to do in this situation?

How can I evaluate this integral

Clarification of algebra in moment generating functions



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
    Apr 26 at 14:19

















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
    Apr 26 at 14:19













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 Apr 26 at 13:39









Philᵀᴹ

26k65692




26k65692










asked Apr 26 at 13:35









Gabriele D'OnufrioGabriele D'Onufrio

669




669












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

    – Denis Rubashkin
    Apr 26 at 14:19

















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

    – Denis Rubashkin
    Apr 26 at 14:19
















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

– Denis Rubashkin
Apr 26 at 14:19





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

– Denis Rubashkin
Apr 26 at 14:19










2 Answers
2






active

oldest

votes


















9














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






























    1














    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









      9














      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



























        9














        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

























          9












          9








          9







          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 Apr 26 at 16:40









          Martin SmithMartin Smith

          65.3k10177263




          65.3k10177263























              1














              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



























                1














                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

























                  1












                  1








                  1







                  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 Apr 26 at 17:19









                  Laughing VergilLaughing Vergil

                  963212




                  963212



























                      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

                      Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                      Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                      What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company