title: How to reduce "sort" cost (t-SQL)
body: <p>I have a T-SQL query. Using <em>Display Estimated Execution Plan</em> in Microsoft SQL Server Management Studio (SSMS), I find the last of the <code>sort</code> operations to consume 83% of the totalt cost. </p>
  
  <p>The weird thing is that when I remove the <code>WHERE</code> and <code>ORDER BY</code>, it only increases the <code>sort</code> cost. So my question is: What else can contribute to cost in this link?</p>
  
  <p><strong>Query</strong></p>
  
  <pre><code>SELECT                                                                  
  o.boardordrenr      as 'SenDX Board Run', 
  bo.boardordrerunnr  as 'RMED Board Run',                                                       
  o.ordrerunnr        as 'Dispensing Run',                                                                             
  o.ordreidnr         as 'Board ID', 
  o.boardnr           as 'Board No.',
  ba.raekkenr         as 'Row',       
  ba.arraynr          as 'Array',
  s0.sensor           as 'Sensor',
  ks.diameter         as 'Cavity Diameter',
  ks.kavitet_dybde    as 'Cavity Depth',
  ms1.tykkelse        as 'CA Thickness',
  ms1.membrane_Stdev  as 'CA Std. Dev.',
  s1.void_procent     as 'CA Void Fraction',
  s1.skannet_dt       as 'CA Scan Date',
  ms2.tykkelse        as 'OM Thickness',
  ms2.membrane_Stdev  as 'OM Std. Dev.',
  s2.void_procent     as 'OM Void Fraction',
  s2.skannet_dt       as 'OM Scan Date',
  ft_sc_par.vaerdi    as 'Sens'
  
  
  FROM 
  boardordre bo                                                                        
  INNER JOIN ordre o          ON bo.boardordrenr = o.boardordrenr AND DATEDIFF(MONTH, o.oprettet_dt, GETDATE()) &lt; 3 AND o.ordrevarenr = 932029                                                 
  INNER JOIN boardarray ba    ON ba.ordrenr = o.ordrenr    
  LEFT JOIN sensorkassette sc ON sc.boardarraynr_bund = ba.boardarraynr
  
  LEFT JOIN skanning s0           ON s0.boardarraynr = ba.boardarraynr AND s0.overflade_index = 0 AND s0.sensor in ('Glu','Lac')  -- Cavity
  LEFT JOIN skanning s1           ON s1.boardarraynr = ba.boardarraynr AND s1.overflade_index = 1 AND s1.sensor = s0.sensor       -- CA Membrane
  LEFT JOIN skanning s2           ON s2.boardarraynr = ba.boardarraynr AND s2.overflade_index = 2 AND s2.sensor = s0.sensor       -- Outer Membrane
  
  LEFT JOIN kavitetskanning ks    ON ks.skanningnr  = s0.skanningnr    
  LEFT JOIN membranskanning ms1   ON ms1.skanningnr = s1.skanningnr  
  LEFT JOIN membranskanning ms2   ON ms2.skanningnr = s2.skanningnr AND (ms2.membrannavn = 'YM_UDV' OR ms2.membrannavn = 'YM')
  
  LEFT JOIN funktionstest_sc ft_sc            ON ft_sc.sensorkassette_chipnr = sc.sensorkassette_chipnr
  LEFT JOIN funktionstest_sc_param ft_sc_par  ON ft_sc_par.funktionstest_sc_nr = ft_sc.funktionstest_sc_nr AND ft_sc_par.sensor = s0.sensor AND ft_sc_par.parameter = 'Sens'
  </code></pre>
  
  <p><strong>Execution Plan (Link)</strong></p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/5957600/sql_execution_plan_actual.sqlplan" rel="nofollow">https://dl.dropboxusercontent.com/u/5957600/sql_execution_plan_actual.sqlplan</a></p>