title: Query Optimization
body: <p>I have 2 queries.</p>
  
  <p>First: </p>
  
  <pre><code>SELECT * FROM `table` WHERE col='xyz' LIMIT 100
  //Time Taken: 0.0047s
  </code></pre>
  
  <p>Second:</p>
  
  <pre><code>SELECT * FROM `table` WHERE col='xyz' ORDER BY Id DESC LIMIT 100
  //Time Taken: 1.8208s
  </code></pre>
  
  <p>The second takes a much longer time. I know why that is, it is because first I have to select the whole table, then do the ordering, whereas the first query only returns the first 100 rows.</p>
  
  <p>Is there any way to ORDER BY using another method, like selecting the last 100 rows and then doing the order? Or am I doing the query wrong and it can be made faster?</p>
  
  <p>Note the Id is Autoincrement, so selecting the last rows will still return the correct data when it is ordered.</p>
  
  <pre><code>CREATE TABLE `table`(
      `Id` BIGINT NOT NULL AUTO_INCREMENT,
      `dateReg` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  
      PRIMARY KEY (`Id`)
  ) ENGINE=MyISAM
  </code></pre>