/ Hathaway Weblog / PostgreSQL and 'not x'

Shane :: Free Software :: October 31, 2005 # PostgreSQL and 'not x'

SQL syntax nuances make a big difference. I have a table defined this way (details omitted for brevity):

create table stripe (
  stripeid     int not null primary key,
  all_healthy  boolean not null default true,
  enough_online  boolean not null default true,
  in_batch   boolean not null default false
);

I also have an index on this table:

create index stripe_schedule_helper on stripe (all_healthy, enough_online, in_batch);

The following query takes a long time because it performs a sequential scan on the whole table.

select stripeid from stripe
where not all_healthy and enough_online and not in_batch

The following query returns the same results but takes a tiny fraction of the time of the above query, because it uses the index:

select stripeid from stripe
where all_healthy = false and enough_online and in_batch = false

So, it turns out that the query optimizer perceives a major difference between the conditions "not x" and "x = false", even when x is a non-null boolean. I used 'explain analyze' to discover this. It's rather surprising, but at least there's a workaround.

Comments

Shane Hathaway (October 31, 2005 10:12)

Addendum: the query should also have enough_online = true to take the most advantage of indexes. I guess the optimizer just isn't aware of the simplifications involving booleans.

Bryan (November 13, 2005 20:35)

Hey my last name is Hathaway what a weird world. I was looking up pictures from Google for Huck Finn and i found a great picture. Random Face

Chas (November 23, 2005 19:13)

I dunno, Shane, all this computer talk gives me a headache. I better go take a tylenol...

Shane Hathaway (November 28, 2005 11:02)

Well, I like to talk about computers, Chas. ;-)

No further comments may be added.

Click below to fill in the scripture reference.
Your browser is not able to display the scripture fill-in program. To see it, enable Javascript or use Mozilla 1.0 or better.
And when ye shall receive these things, I would exhort you that ye would ask God, the Eternal Father, in the name of Christ, if these things are not true; and if ye shall ask with a sincere heart, with real intent, having faith in Christ, he will manifest the truth of it unto you, by the power of the Holy Ghost. And by the power of the Holy Ghost ye may know the truth of all things.

Church: lds scriptures provident games pearls kzion shiblon film chancellor gateway cumorah byutv happiness nephi
Zope: freezope org com zen labs newbies zettai warnes
Python: home pyzine daily icanprogram
Genealogy: cyndi
Weblogs: jeffrey paul jon joel another-shane guido barry jeremy windley chrism zac
News: quakes lwn dc weather deseret zeitgeist softwarelivre
Zaurus: software developer
Tech: tango spintronics thin
Semantic: aaron sean
Reference: css rdf html4 javascript geckodom iecss emacs phrases acronyms
Reverse: advogato slashdot
Misc: gimp-savvy directory soda jokes shouldexist pdphoto