From: Gaetano Mendola <mendola@bigfoot.com>
To: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: Wrong index choosen?
Date: Fri, 23 Jul 2004 10:26:52 +0200
Message-ID: <4100CBCC.5050308@bigfoot.com> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: mendola@bigfoot.com, pgsql-hackers@postgresql.org
Subject: Re: Wrong index choosen?
In-Reply-To: <4100CBCC.5050308@bigfoot.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox