Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can usually optimize queries by:</p> <ul> <li><p>picking a <strong>good</strong> clustering key on your tables - good being <code>narrow, unique, static, ever-increasing</code>. <code>INT IDENTITY</code> is a classic good key - GUID's are a horribly bad example (since they lead to excessive index fragmentation - read Kim Tripp's <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/GUIDs-as-PRIMARY-KEYs-andor-the-clustering-key.aspx" rel="nofollow noreferrer">GUIDs as Primary and/or clustering key</a> for lots more details)</p></li> <li><p>making sure all foreign key columns in the child tables are <strong>indexed</strong> so that JOINs and lookups are performed faster</p></li> <li><p>selecting as little columns as you really need (you seem to be doing this just fine)</p></li> <li><p>trying to <em>cover the query</em>, e.g. create indices on the tables involved that have all the necessary columns - either directly as index columns, or as <em>included</em> columns (SQL Server 2008 and onwards)</p></li> <li><p>possibly adding additional indices to speed up range queries, and/or help with sorting/ordering</p></li> </ul> <p>Looking at your queries and table definitions:</p> <ul> <li><p>I don't seem to see any primary keys - add those!</p></li> <li><p>you would have to make sure to have foreign key indices on <code>pp.production_line</code> (assuming <code>t.production_line</code> is the primary key of the other table)</p></li> <li><p>you should see if you can find a good index to handle the range query on <code>t.UTC</code></p></li> <li><p>you should check if it makes sense to create an index on <code>production_plan2</code> to contain all the columns (<code>order_id, pp.prod_start, pp.prod_end</code>) </p></li> <li><p>you should check if it makes sense to create an index on <code>temperatures2</code> to contain all the columns (<code>UTC, temperature_1</code>) </p></li> </ul> <p><strong>Update:</strong> you can capture the actual execution plan by enabling that option from the SSMS toolbar:</p> <p><img src="https://i.stack.imgur.com/2EsYR.png" alt="enter image description here"></p> <p>or from the menu under <code>Query &gt; Include Actual Execution Plan</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload