Re: Planner should use index on a LIKE 'foo%' query

From: Dimitri Fontaine <dfontaine(at)hi-media(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Cc: Moritz Onken <onken(at)houseofdesign(dot)de>
Subject: Re: Planner should use index on a LIKE 'foo%' query
Date: 2008-06-30 10:20:40
Message-ID: 200806301220.43392.dfontaine@hi-media.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi,

Le samedi 28 juin 2008, Moritz Onken a écrit :
> select count(*)
> from result
> where exists
> (select * from item where item.url LIKE result.url || '%' limit 1);
>
> which basically returns the number of items which exist in table
> result and match a URL in table item by its prefix.

It seems you could benefit from the prefix project, which support indexing
your case of prefix searches. Your query would then be:
SELECT count(*) FROM result r JOIN item i ON r.url @> i.url;

The result.url column would have to made of type prefix_range, which casts
automatically to text when needed.

Find out more about the prefix projects at those urls:
http://pgfoundry.org/projects/prefix
http://prefix.projects.postgresql.org/README.html

Regards,
--
dim

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message John Beaver 2008-06-30 10:59:00 Re: sequence scan problem
Previous Message Matthew Wakeling 2008-06-30 10:19:47 Re: Planner should use index on a LIKE 'foo%' query