pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedDifference between "foo is false" and "foo=false"? Partial index on boolean.
4+ messages / 3 participants
[nested] [flat]
* Difference between "foo is false" and "foo=false"? Partial index on boolean.
@ 2007-09-04 04:43 Bryce Nesbitt <bryce1@obviously.com>
0 siblings, 1 reply; 4+ messages in thread
From: Bryce Nesbitt @ 2007-09-04 04:43 UTC (permalink / raw)
To: pgsql-sql
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
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?<br>
<tt><br>
stage=# create index eg_ve_reconciled_partial on eg_vehicle_event
(reconciled) where <b>reconciled=false;</b><br>
stage=# select pg_total_relation_size('eg_ve_reconciled_partial');<br>
pg_total_relation_size <br>
------------------------<br>
8192<br>
<br>
<br>
stage=# explain select count(*) from EG_VEHICLE_EVENT where reconciled <b>is
false;</b><br>
--------------------------------------------------------------------------<br>
Aggregate (cost=33169.57..33169.58 rows=1 width=0)<br>
-> <b>Seq Scan</b> on eg_vehicle_event (cost=0.00..33169.57
rows=1 width=0)<br>
Filter: (reconciled IS FALSE)<br>
<br>
<br>
<br>
stage=# explain select count(*) from EG_VEHICLE_EVENT where <b>reconciled=false;</b><br>
-------------------------------------------------------------------------------------------------------<br>
Aggregate (cost=1.02..1.03 rows=1 width=0)<br>
-> <b>Index Scan</b> using eg_ve_reconciled_partial on
eg_vehicle_event (cost=0.00..1.01 rows=1 width=0)<br>
Index Cond: (reconciled = false)<br>
</tt><br>
<br>
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:<br>
<br>
<tt>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_.<b>RECONCILED=$2</b> )]</tt><br>
<br>
<br>
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:<br>
<br>
<tt>stage=# explain analyze select count(vehicleeve0_.VEHICLE_EVENT_ID)
as x0_0_ from EG_VEHICLE_EVENT vehicleeve0_ where
(vehicleeve0_.CSO_ID=2 )and(vehicleeve0_.<b>RECONCILED=false</b>);<br>
QUERY
PLAN
<br>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------<br>
Aggregate (cost=81.75..81.76 rows=1 width=4) (actual
time=56.153..56.154 rows=1 loops=1)<br>
-> <b>Index Scan</b> 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)<br>
Index Cond: (reconciled = false)<br>
Filter: (cso_id = 2)<br>
Total runtime: 64.825 ms<br>
(5 rows)</tt><br>
<br>
<br>
<pre class="moz-signature" cols="100">--
----
Visit <a class="moz-txt-link-freetext" href="http://www.obviously.com/">http://www.obviously.com/</a;
</pre>
</body>
</html>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Difference between "foo is false" and "foo=false"? Partial index on boolean.
@ 2007-09-04 04:56 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bryce Nesbitt <bryce1@obviously.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2007-09-04 04:56 UTC (permalink / raw)
To: Bryce Nesbitt <bryce1@obviously.com>; +Cc: pgsql-sql
Bryce Nesbitt <bryce1@obviously.com> writes:
> Could someone explain
> the difference between "foo=false" and "foo is false", for a boolean
> type column?
They give different results for NULL --- specifically, NULL for the
former and FALSE for the latter. Don't blame me, it's in the spec...
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Difference between "foo is false" and "foo=false"? Partial index on boolean.
@ 2007-09-04 05:07 Bryce Nesbitt <bryce1@obviously.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 4+ messages in thread
From: Bryce Nesbitt @ 2007-09-04 05:07 UTC (permalink / raw)
To: pgsql-sql; +Cc: Tom Lane <tgl@sss.pgh.pa.us>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Tom Lane wrote:
<blockquote cite="mid:14040.1188881806@sss.pgh.pa.us" type="cite">
<pre wrap="">Bryce Nesbitt <a class="moz-txt-link-rfc2396E" href="mailto:bryce1@obviously.com"><bryce1@obviously.com></a> writes:
</pre>
<blockquote type="cite">
<pre wrap="">Could someone explain
the difference between "foo=false" and "foo is false", for a boolean
type column?
</pre>
</blockquote>
<pre wrap=""><!---->
They give different results for NULL --- specifically, NULL for the
former and FALSE for the latter. Don't blame me, it's in the spec...</pre>
</blockquote>
Thanks, and Got It. This particular column is:<br>
reconciled | boolean | not null<br>
On PostgreSQL 8.1.9.<br>
<br>
<br>
So given all that, why would the Hibernate query fail to use the
partial index? I eventually created three indexes, and only the
hideously large full index increases performance:<br>
<br>
<tt>Indexes:<br>
"eg_vehicle_event_pkey" PRIMARY KEY, btree (vehicle_event_id)<br>
"no_duplicate_events" UNIQUE, btree (cso_id, event_type,
"timestamp", fob_number, hardware_number)<br>
"eg_ve_reconciled_full" btree (reconciled)<br>
"eg_ve_reconciled_partial" btree (reconciled) WHERE reconciled =
false<br>
"eg_ve_reconciled_partial_is" btree (reconciled) WHERE reconciled
IS FALSE<br>
Foreign-key constraints:<br>
"fk_event_admin" FOREIGN KEY (admin_id) REFERENCES
eg_admin(admin_id)<br>
"fkd28396aacabde72e" FOREIGN KEY (vehicle_id) REFERENCES
eg_vehicle(vehicle_id)<br>
"fkd28396aaf61930e0" FOREIGN KEY (member_id) REFERENCES
eg_member(member_id)<br>
</tt><br>
<br>
Only the full index prevents a "false" scan from taking 4 seconds:<br>
<br>
<tt>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_.<b>RECONCILED=$2</b> )]</tt><br>
<br>
<br>
<pre class="moz-signature" cols="100">--
----
Visit <a class="moz-txt-link-freetext" href="http://www.obviously.com/">http://www.obviously.com/</a;
</pre>
</body>
</html>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Difference between "foo is false" and "foo=false"? Partial index on boolean.
@ 2007-09-04 08:43 Richard Huxton <dev@archonet.com>
parent: Bryce Nesbitt <bryce1@obviously.com>
0 siblings, 0 replies; 4+ messages in thread
From: Richard Huxton @ 2007-09-04 08:43 UTC (permalink / raw)
To: Bryce Nesbitt <bryce1@obviously.com>; +Cc: pgsql-sql@postgresql.org, Tom Lane <tgl@sss.pgh.pa.us>
Bryce Nesbitt wrote:
> Tom Lane wrote:
>> Bryce Nesbitt <bryce1@obviously.com> writes:
>>
>> They give different results for NULL --- specifically, NULL for the
>> former and FALSE for the latter. Don't blame me, it's in the spec...
> Thanks, and Got It. This particular column is:
> reconciled | boolean | not null
> On PostgreSQL 8.1.9.
> So given all that, why would the Hibernate query fail to use the partial
> index? I eventually created three indexes, and only the hideously large full
> index increases performance:
> Only the full index prevents a "false" scan from taking 4 seconds:
>
> 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* )]
It's a prepared query-plan, which means it can't plan to use the index
because the next EXECUTE might have reconciled=true.
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2007-09-04 08:43 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-09-04 04:43 Difference between "foo is false" and "foo=false"? Partial index on boolean. Bryce Nesbitt <bryce1@obviously.com>
2007-09-04 04:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2007-09-04 05:07 ` Bryce Nesbitt <bryce1@obviously.com>
2007-09-04 08:43 ` Richard Huxton <dev@archonet.com>
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