Received: from localhost (pluto.hub.org [200.46.204.4]) by postgresql.org (Postfix) with ESMTP id A58ED9FA491 for ; Sun, 5 Nov 2006 16:34:32 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.4]) (amavisd-new, port 10024) with ESMTP id 15641-10 for ; Sun, 5 Nov 2006 16:34:22 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 90D7A9F9ED7 for ; Sun, 5 Nov 2006 16:34:30 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from out1.smtp.messagingengine.com (out1.smtp.messagingengine.com [66.111.4.25]) by svr4.postgresql.org (Postfix) with ESMTP id C78A15AF887 for ; Sun, 5 Nov 2006 20:34:29 +0000 (GMT) Received: from db2.internal (db2.internal [10.202.2.12]) by frontend1.messagingengine.com (Postfix) with ESMTP id 3D3BBDC0F92; Sun, 5 Nov 2006 15:34:27 -0500 (EST) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by db2.internal (MEProxy); Sun, 05 Nov 2006 15:34:29 -0500 X-Sasl-enc: EcLz+Rcdq0fJW56ZiiJVQlkR2QVBFgHXEJryUJsPzG6Y 1162758868 Received: from [192.168.20.70] (207.47.10.130.static.nextweb.net [207.47.10.130]) by mail.messagingengine.com (Postfix) with ESMTP id 34CFEFB5C; Sun, 5 Nov 2006 15:34:28 -0500 (EST) Message-ID: <454E4AC7.9020201@stuartbishop.net> Date: Sun, 05 Nov 2006 12:34:15 -0800 From: Stuart Bishop User-Agent: Thunderbird 1.5.0.7 (X11/20060922) MIME-Version: 1.0 To: Stuart Bishop Cc: pgsql-performance@postgresql.org Subject: Re: Slow functional indexes? References: <45387666.204@canonical.com> In-Reply-To: <45387666.204@canonical.com> X-Enigmail-Version: 0.94.0.0 OpenPGP: id=BAC6374A; url=http://stuartbishop.net/stuartbishop.asc Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigC77A716D8670AEAC7C4728B1" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/54 X-Sequence-Number: 21824 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigC77A716D8670AEAC7C4728B1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Stuart Bishop wrote: > I would like to understand what causes some of my indexes to be slower = to > use than others with PostgreSQL 8.1. On a particular table, I have an i= nt4 > primary key, an indexed unique text 'name' column and a functional inde= x of > type text. The function (person_sort_key()) is declared IMMUTABLE and > RETURNS NULL ON NULL INPUT. >=20 > A simple query ordering by each of these columns generates nearly ident= ical > query plans, however runtime differences are significantly slower using= the > functional index. If I add a new column to the table containing the res= ult > of the function, index it and query ordering by this new column then th= e > runtime is nearly an order of magnitude faster than using the functiona= l > index (and again, query plans are nearly identical). > > (The following log is also at > http://rafb.net/paste/results/vKVuyi47.nln.html if that is more readabl= e) Here is a minimal test case that demonstrates the issue. Can anyone else reproduce these results? Of the four EXPLAIN ANALYZE SELECT statements at= the end, the one that orders by a user created IMMUTABLE stored procedure= is consistently slower than the other three variants. BEGIN; DROP TABLE TestCase; COMMIT; ABORT; BEGIN; CREATE TABLE TestCase (name text, alt_name text); CREATE OR REPLACE FUNCTION munge(s text) RETURNS text IMMUTABLE RETURNS NULL ON NULL INPUT LANGUAGE plpgsql AS $$ BEGIN RETURN lower(s); END; $$; -- Fill the table with random strings CREATE OR REPLACE FUNCTION fill_testcase(num_rows int) RETURNS boolean LANGUAGE plpgsql AS $$ DECLARE row_num int; char_num int; name text; BEGIN FOR row_num IN 1..num_rows LOOP name :=3D ''; FOR char_num IN 1..round(random() * 100) LOOP name :=3D name || chr(( round(random() * (ascii('z') - ascii('!'))) + ascii('!') )::int); END LOOP; INSERT INTO TestCase VALUES (name, lower(name)); IF row_num % 20000 =3D 0 THEN RAISE NOTICE '% of % rows inserted', row_num, num_rows; END IF; END LOOP; RETURN TRUE; END; $$; SELECT fill_testcase(500000); CREATE INDEX testcase__name__idx ON TestCase(name); CREATE INDEX testcase__lower__idx ON TestCase(lower(name)); CREATE INDEX testcase__munge__idx ON TestCase(munge(name)); CREATE INDEX testcase__alt_name__idx ON TestCase(alt_name); COMMIT; ANALYZE TestCase; EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY name; EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY lower(name); EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY munge(name); EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY alt_name; EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY name; EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY lower(name); EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY munge(name); EXPLAIN ANALYZE SELECT * FROM TestCase ORDER BY alt_name; --=20 Stuart Bishop http://www.stuartbishop.net/ --------------enigC77A716D8670AEAC7C4728B1 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFFTkrHAfqZj7rGN0oRAh7rAJ4ooO2tP8rFP4OV/Jj8oN+jZp40sQCeKu1W wh3EC3bIQOBYh2Z1SSxeAm8= =PlFc -----END PGP SIGNATURE----- --------------enigC77A716D8670AEAC7C4728B1--