Note that there are some explanatory texts on larger screens.

plurals
  1. POSlow SQL Query on indexed columns, multi-column number
    primarykey
    data
    text
    <p>I have a table that is comprised of 6 numbers as the primary key</p> <pre><code>CREATE TABLE table1 ( num1 decimal, num2 int, num3 int, num4 bigint, num5 bigint, num6 bigint, PRIMARY KEY (num1, num2, num3, num4, num5, num6)) </code></pre> <p>I need to access the table in sorted order and often times I have a need to query the table to find the next N large numbers in order and their associated data.</p> <p>So the query I wrote was something like this</p> <pre><code>SELECT * FROM table1 WHERE num1 &gt;? OR ( (num1 == ? AND num2 &gt; ?) OR ( (num1 == ? AND num2 == ? AND num3 &gt; ?) OR ( (num1 == ? AND num2 == ? AND num3 == ? AND num4 &gt; ? OR ( (num1 == ? AND num2 == ? AND num3 == ? AND num4 == ? AND num5 &gt; ?) OR ( (num1 == ? AND num2 == ? AND num3 == ? AND num4 == ? AND num5 == ? AND num6 &gt; ?)))))) ORDER BY num1, num2, num3, num4, num5, num6 LIMIT ?; </code></pre> <p>This was the best way I could see to find the next largest key, and this does query in the order of the index however....query takes a few seconds, which is something that I'm not to fond of.</p> <p>Is there any way to improve the performance? This takes a few seconds to execute on a table of 10million rows and I need it to execute more on the order of 100ms.</p> <p>Query Plan:</p> <pre><code>"SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1&gt;?) (~250000 rows)" "SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1=? AND num2&gt;?) (~2 rows)" "SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1=? AND num2=? AND num3&gt;?) (~2 rows)" "SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1=? AND num2=? AND num3=? AND num4&gt;?) (~2 rows)" "SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1=? AND num2=? AND num3=? AND num4=? AND num5&gt;?) (~1 rows)" "SEARCH TABLE table1 USING INDEX sqlite_autoindex_table1_1 (num1=? AND num2=? AND num3=? AND num4=? AND num5=? AND num6&gt;?) (~1 rows)" "USE TEMP B-TREE FOR ORDER BY" </code></pre> <p>Edit:</p> <p>Why is this not possible? I literally want to get things in the INDEXED ORDER, the same order generated by the <code>ORDER BY</code> keyword?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload