How to optimize query that concatenates strings?

From: "badlydrawnbhoy" <badlydrawnbhoy(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: How to optimize query that concatenates strings?
Date: 2006-07-07 11:29:51
Message-ID: 1152271791.225523.297220@k73g2000cwa.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

I've got a database of URLs, and when inserting new data into it I want
to make sure that there are no functionally equivalent URLs already
present. For example, 'umist.ac.uk' is functionally the same as
'umist.ac.uk/'.

I find that searching for the latter form, using string concatentation
to append the trailing slash, is much slower than searching for a
simple string - the index on URL name isn't used to speed up the
search.

Here's an illustration

url=# explain select exists(select * from url where url = 'umist.ac.uk'
or url || '/' = 'umist.ac.uk') as present;
QUERY PLAN

-----------------------------------------------------------------------------------------------
Result (cost=47664.01..47664.02 rows=1 width=0)
InitPlan
-> Seq Scan on url (cost=0.00..47664.01 rows=6532 width=38)
Filter: ((url = 'umist.ac.uk'::text) OR ((url || '/'::text)
= 'umist.ac.uk'::text))
(4 rows)

url=# explain select exists(select * from url where url =
'umist.ac.uk') as present;
QUERY PLAN
----------------------------------------------------------------------------
Result (cost=5.97..5.98 rows=1 width=0)
InitPlan
-> Index Scan using url_idx on url (cost=0.00..5.97 rows=1
width=38)
Index Cond: (url = 'umist.ac.uk'::text)
(4 rows)

Is there any way I can force postgres to use the index when using the
string concatenation in the query?

Thanks in advance,

BBB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Roman Neuhauser 2006-07-07 11:30:35 Re: Why my cursor construction is so slow?
Previous Message Vittorio 2006-07-07 11:22:51 How to sample records