Expecting to save 4 seconds per query, I built a partial index on a
table, and was surprised that it did not work. Could someone explain
the difference between "foo=false" and "foo is false", for a boolean
type column?
stage=# create index eg_ve_reconciled_partial on eg_vehicle_event
(reconciled) where reconciled=false;
stage=# select pg_total_relation_size('eg_ve_reconciled_partial');
pg_total_relation_size
------------------------
8192
stage=# explain select count(*) from EG_VEHICLE_EVENT where reconciled is
false;
--------------------------------------------------------------------------
Aggregate (cost=33169.57..33169.58 rows=1 width=0)
-> Seq Scan on eg_vehicle_event (cost=0.00..33169.57
rows=1 width=0)
Filter: (reconciled IS FALSE)
stage=# explain select count(*) from EG_VEHICLE_EVENT where reconciled=false;
-------------------------------------------------------------------------------------------------------
Aggregate (cost=1.02..1.03 rows=1 width=0)
-> Index Scan using eg_ve_reconciled_partial on
eg_vehicle_event (cost=0.00..1.01 rows=1 width=0)
Index Cond: (reconciled = false)
The problem is that my test query above is fast, but the real query
from Hibernate is still dog slow. Here's the pg_log entry:
LOG: duration: 4260.575 ms statement: EXECUTE C_50292 [PREPARE:
select count(vehicleeve0_.VEHICLE_EVENT_ID) as x0_0_ from
EG_VEHICLE_EVENT vehicleeve0_ where (vehicleeve0_.CSO_ID=$1 ) and
(vehicleeve0_.RECONCILED=$2 )]
I tried building two indexes, one for "is false" one for "=false", but
the Hibernate query is still slow. Yet the hand-run version uses the
index easily:
stage=# explain analyze select count(vehicleeve0_.VEHICLE_EVENT_ID)
as x0_0_ from EG_VEHICLE_EVENT vehicleeve0_ where
(vehicleeve0_.CSO_ID=2 )and(vehicleeve0_.RECONCILED=false);
QUERY
PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=81.75..81.76 rows=1 width=4) (actual
time=56.153..56.154 rows=1 loops=1)
-> Index Scan using eg_ve_reconciled_partial on
eg_vehicle_event vehicleeve0_ (cost=0.00..60.05 rows=8679 width=4)
(actual time=0.126..44.548 rows=10345 loops=1)
Index Cond: (reconciled = false)
Filter: (cso_id = 2)
Total runtime: 64.825 ms
(5 rows)
--
----
Visit http://www.obviously.com/