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

From: "Steinar H(dot) Gunderson" <sgunderson(at)bigfoot(dot)com>
To: Moritz Onken <onken(at)houseofdesign(dot)de>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Planner should use index on a LIKE 'foo%' query
Date: 2008-06-28 19:19:31
Message-ID: 20080628191931.GB13013@uio.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Sat, Jun 28, 2008 at 06:24:42PM +0200, Moritz Onken wrote:
> SELECT distinct url from item where url like 'http://www.micro%' limit
> 10;

Here, the planner knows the pattern beforehand, and can see that it's a
simple prefix.
> select *
> from result
> where exists
> (select * from item where item.url LIKE result.url || '%' limit 1)
> limit 10;

Here it cannot (what if result.url was '%foo%'?).

Try using something like (item.url >= result.url && item.url <= result.url ||
'z'), substituting an appropriately high character for 'z'.

> The only explaination is that I don't use a constant when comparing the
> values. But actually it is a constant...

It's not a constant at planning time.

Also note that you'd usually want to use IN instead of a WHERE EXISTS.

/* Steinar */
--
Homepage: http://www.sesse.net/

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Ulrich 2008-06-28 22:07:32 Re: Subquery WHERE IN or WHERE EXISTS faster?
Previous Message Moritz Onken 2008-06-28 16:24:42 Planner should use index on a LIKE 'foo%' query