Re: Very slow query - Mailing list pgsql-novice

From Josh Berkus
Subject Re: Very slow query
Date
Msg-id 200207301403.11943.josh@agliodbs.com
Whole thread Raw
In response to Very slow query  ("Chad Thompson" <chad@weblinkservices.com>)
Responses Re: Very slow query  (Ron Johnson <ron.l.johnson@cox.net>)
List pgsql-novice
Chad,

> Its the substring function that slows things down so much.  Is there a
better way to compare these string values?
> substr(cr.phonenum, 1,6) is the same speed.  (and according to the docs,
basicly the same function)

Yeah.  You can index it:

CREATE FUNCTION phonenum_substr (
    VARCHAR ) RETURNS VARCHAR AS '
SELECT substr(cr.phonenum, 1, 6);
' LANGUAGE 'sql' WITH (ISCACHABLE, ISSTRICT);
-- this lets you index on the substring, as the straight substr function is
not indexable as of Postgres 7.2.1

Then:
CREATE INDEX idx_cr_phonenum_substr ON phonenum_substr(cr.phonenum);

This should speed things up.



--
-Josh Berkus
 Aglio Database Solutions
 San Francisco


pgsql-novice by date:

Previous
From: "Chad Thompson"
Date:
Subject: Very slow query
Next
From: Ron Johnson
Date:
Subject: Re: Very slow query