title: LEFT JOIN takes too long when SORTING results with ORDER BY
body: <p>this is my first question in stackoverflow, usually I'm used to searching the internet for an answer, but this time i could not find any answer for this question.</p>
  
  <p>Simply my problem is a query takes TOO long to execute while it's a simple join between 2 tables</p>
  
  <p>I'll post my query first then i will post more details about my system:</p>
  
  <pre><code>SELECT * FROM tbl_item 
  LEFT JOIN (SELECT * FROM tbl_item_details) AS tbl_item_details 
  ON tbl_item.item_id = tbl_item_details.item_details_item_id 
  WHERE item_active = 1 ORDER BY item_views DESC LIMIT 0,5
  </code></pre>
  
  <p>Here is my tables structure:</p>
  
  <pre><code>CREATE TABLE `tbl_item` (
  `item_id` int(11) NOT NULL AUTO_INCREMENT,
  `item_views` int(11) NOT NULL,
  `item_active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`item_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=821 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>tbl_item_details:</p>
  
  <pre><code>CREATE TABLE `tbl_item_details` (
  `item_details_id` int(11) NOT NULL AUTO_INCREMENT,
  `item_details_title` varchar(255) NOT NULL,
  `item_details_content` longtext NOT NULL,
  `item_details_item_id` int(11) NOT NULL,
  PRIMARY KEY (`item_details_id`),
  KEY `itm_dt_itm_id` (`item_details_item_id`),
  CONSTRAINT `tbl_item_details_ibfk_1` FOREIGN KEY (`itm_dt_itm_id`) REFERENCES `tbl_item` (`itm_id`) ON DELETE CASCADE ON UPDATE CASCADE
  ) ENGINE=InnoDB AUTO_INCREMENT=364 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>here is EXPLAIN query output:</p>
  
  <pre>
  id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
  1   PRIMARY     tbl_item    ALL     NULL    NULL    NULL    NULL    358     Using where; Using temporary; Using filesort
  1   PRIMARY       ALL     NULL    NULL    NULL    NULL    358      
  2   DERIVED     tbl_item_details    ALL     NULL    NULL    NULL    NULL    422      
  </pre>
  
  <p>each tables has ONLY 350 rows, and the big table (tbl_item_details) 1.5 MB so you see the tables are pretty small.</p>
  
  <p>BASICALLY, the above query takes almost 4 seconds to execute on the following system:</p>
  
  <p>CPU: Intel(R) Pentium(R) 4 CPU 3.20GHz (2 CPUs), ~3.2GHz
  RAM: 3 GB
  Mysql: 5.1.33 (included with wamp)</p>
  
  <p>BEFORE, anyone suggests a solution here is what I tried, what worked and what did not:</p>
  
  <p>things that worked  and the query took much less time (0.06 seconds):</p>
  
  <ul>
  <li>I tried removing ORDER BY I tried removing item_details_content from</li>
  <li>the select I tried using INNER JOIN instead and it worked I tried</li>
  <li>switching the tables in the join so the INNER table became OUTER and vice versa</li>
  </ul>
  
  <p>I CANNOT use INNER JOIN because there might be rows in tbl_item that does not have matches in tbl_item_details and i want these records</p>
  
  <p>things i tried which DID NOT work:</p>
  
  <ul>
  <li>I tried adding an index to item_views (DID NOT WORK)</li>
  <li>I tried removing the foreign keys constraints</li>
  <li>I tried witching the tables engine to MyIsam</li>
  </ul>
  
  <p>Obviously the problem happens when mysql is sorting the date and faces (relatively) large data in item_details_content, so if we get rid of one of the things (either sorting or the column item_details_content) it works just fine.</p>
  
  <p>but the thing is, this should not happen! cos the table has really very small data considering it's just 350 rows with total of 1.5 MB! mysql should be able to handle much more data than that.</p>
  
  <p>PLEASE, before suggesting drastic changes to the query structure, I'm afraid that is not possible, because I've been working on this framework for a while and the query is dynamically generated, and changes to the query might mean days of work BUT your suggestions are always welcome.</p>
  
  <p>P.S: I tried this query on a powerful server (core i7 and 8 GB RAM) and it took 0.3 seconds which is still too long for such a database</p>
  
  <p>Thanks a million</p>