title: How can I optimize SQLite ORDER BY rowid?
body: <p>I want to query my sqlite db for all "level" values greater than 20, limit the result to 100 and order by rowid.</p>
  
  <p>When ordering by rowid, the query is much slower. The database contains ~3 million records 
  and the maximum value of level is 50. An index is created for level.</p>
  
  <p><em>This statement takes ~20ms:</em></p>
  
  <pre><code>SELECT * FROM log WHERE level &gt; 20 LIMIT 100
  </code></pre>
  
  <p><em>This statement takes ~100ms:</em></p>
  
  <pre><code>SELECT * FROM log WHERE level &gt; 20 ORDER BY rowid LIMIT 100
  </code></pre>
  
  <p><em>This statement takes ~1000ms (no rows with level value greater than 50 exists):</em></p>
  
  <pre><code>SELECT * FROM log WHERE level &gt; 50 ORDER BY rowid LIMIT 100
  </code></pre>
  
  <p>Is there a way to optimize this for faster ORDER BY query's?</p>
  
  <p>This is the index used:</p>
  
  <pre><code>CREATE INDEX level_idx ON table (level)
  </code></pre>