Note that there are some explanatory texts on larger screens.

plurals
  1. POPostgreSQL query doesn't use index
    primarykey
    data
    text
    <p>I have a very simple db schema, which has a multi column b-tree index on following columns: </p> <pre><code>PersonId, Amount, Commission </code></pre> <p>Now, if I try to select the table with following query:</p> <pre><code>explain select * from "Order" where "PersonId" = 2 AND "Commission" &gt; 3 </code></pre> <p>Pg is scanning the index and the query is very fast, but if I try the following query:</p> <pre><code>explain select * from "Order" where "PersonId" &gt; 2 AND "Commission" &gt; 3 </code></pre> <p>It does a sequential scan, even when the index is present. Even this query</p> <pre><code>explain select * from "Order" where "Commission" &gt; 3 </code></pre> <p>does a sequential scan. Anyone care to explain why? :-)</p> <p>Thank you very much.</p> <p><strong>UPDATE</strong></p> <p>The table contains 100 million rows. I have created it just to test PostgreSQL performance against MS SQL. The table is already VACUUMED. I'm runnning Core I5 2500k quad core cpu and 8 GB of ram.</p> <p>Here's the result of explain analyze for this query:</p> <pre><code>explain ANALYZE select * from "Order" where "Commission" BETWEEN 3000000 AND 3000010 LIMIT 20 Limit (cost=0.00..2218328.00 rows=1 width=24) (actual time=28043.249..28043.249 rows=0 loops=1) -&gt; Seq Scan on "Order" (cost=0.00..2218328.00 rows=1 width=24) (actual time=28043.247..28043.247 rows=0 loops=1) Filter: (("Commission" &gt;= 3000000::numeric) AND ("Commission" &lt;= 3000010::numeric)) Total runtime: 28043.278 ms </code></pre>
    singulars
    1. This table or related slice is empty.
    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