Wrong index choosen?

From: Gaetano Mendola <mendola(at)bigfoot(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Wrong index choosen?
Date: 2004-07-23 08:26:52
Message-ID: 4100CBCC.5050308@bigfoot.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

I hall
I have a query in this form:

empdb=# explain analyze select * from v_past_connections where id_user = 26195 and login_time > '2004-07-21';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_user_logs_login_time on user_logs (cost=0.00..14.10 rows=1 width=28) (actual time=66.890..198.998 rows=5 loops=1)
Index Cond: (login_time > '2004-07-21 00:00:00+02'::timestamp with time zone)
Filter: (id_user = 26195)
Total runtime: 199.083 ms
(4 rows)

as you see the index on the time stamp column is used

The table have indexes on both columns:

empdb=# explain analyze select * from v_past_connections where login_time > '2004-07-21';
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_user_logs_login_time on user_logs (cost=0.00..12.90 rows=481 width=28) (actual time=7.338..661.300 rows=22477 loops=1)
Index Cond: (login_time > '2004-07-21 00:00:00+02'::timestamp with time zone)
Total runtime: 676.472 ms
(3 rows)

empdb=# explain analyze select * from v_past_connections where id_user = 26195;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_user_user_logs on user_logs (cost=0.00..252.47 rows=320 width=28) (actual time=4.420..100.122 rows=221 loops=1)
Index Cond: (id_user = 26195)
Total runtime: 100.348 ms
(3 rows)

The rows filtered out with both condictions are two order of magnitude differents,
also the extimated rows are close to real numbers:

empdb=# select count(*) from v_past_connections where id_user = 26195;
count
-------
221
(1 row)

empdb=# select count(*) from v_past_connections where login_time > '2004-07-21';
count
-------
22441
(1 row)

why then the planner choose to do an index scan using the filter that retrieve a bigger ammount of rows ? A bug ?

Regards
Gaetano Mendola

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Suresh Tri 2004-07-23 09:21:51 PostgreSQL development
Previous Message Christopher Kings-Lynne 2004-07-23 08:02:57 Re: Fixing PKs and Uniques in tablespaces

Browse pgsql-performance by date

  From Date Subject
Next Message Rod Taylor 2004-07-23 11:43:07 Re: Performance over a LAN
Previous Message William Carney 2004-07-23 07:57:53 Re: Performance over a LAN