title: SQL optimization - slow query
body: <p>Given SQL takes 1.2s:</p>
  
  <pre><code>SELECT DISTINCT contracts.id, jt0.id, jt1.id, jt2.id, jt3.id FROM contracts
  LEFT JOIN accounts jt0 ON jt0.id = contracts.account_id AND jt0.deleted=0
  LEFT JOIN manufacturers jt1 ON jt1.id = contracts.manufacturer_id AND jt1.deleted=0
  LEFT JOIN products jt2 ON jt2.id = contracts.product_id AND jt2.deleted=0
  LEFT JOIN users jt3 ON jt3.id = contracts.assigned_user_id AND jt3.deleted=0
  WHERE contracts.deleted=0
  ORDER BY contracts.application_number ASC 
  LIMIT 0,21
  </code></pre>
  
  <p>here is what explain extended returns:</p>
  
  <pre><code>id  select_type table   type    possible_keys   key key_len ref rows
  1   SIMPLE  contracts   ref idx_contracts_deleted   idx_contracts_deleted   2   const   18968   100.00  Using where; Using temporary; Using filesort
  1   SIMPLE  jt0 eq_ref  PRIMARY,idx_accnt_id_del,idx_accnt_assigned_del PRIMARY 108 xxx.contracts.account_id    1   100.00  
  1   SIMPLE  jt1 eq_ref  PRIMARY,idx_manufacturers_id_deleted,idx_manufacturers_deleted  PRIMARY 108 xxx.contracts.manufacturer_id   1   100.00  
  1   SIMPLE  jt2 eq_ref  PRIMARY,idx_products_id_deleted,idx_products_deleted    PRIMARY 108 xxx.contracts.product_id    1   100.00  
  1   SIMPLE  jt3 eq_ref  PRIMARY,idx_users_id_del,idx_users_id_deleted,idx_users_deleted PRIMARY 108 xxx.contracts.assigned_user_id  1   100.00  
  </code></pre>
  
  <p>I need the distinct, I need all the joins to be left, I need order by and i need limit.<br />
  Can i optimize it somehow?</p>