X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id B8DE1D1C4C8 for ; Mon, 28 Jul 2003 03:59:58 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 59010-06 for ; Mon, 28 Jul 2003 00:59:02 -0300 (ADT) Received: from ozimelb03.ozicom.com (unknown [203.185.247.100]) by svr1.postgresql.org (Postfix) with ESMTP id 6F4C3D1C4BB for ; Mon, 28 Jul 2003 00:59:54 -0300 (ADT) Subject: Using a compound primary key MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Mon, 28 Jul 2003 13:59:52 +1000 Message-ID: X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Using a compound primary key Thread-Index: AcNUvLKokaOFSR/fR+2YMH1AMOtvlQ== From: "David Witham" To: X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/410 X-Sequence-Number: 14524 Hi all, I have a table containing tariff information. It has a primary key (and the= refore unique index) of (tariff_type varchar(5),prefix varchar(12)) where t= ariff_type is a set of rates assigned to dialling prefixes. e.g. tariff_type prefix rate abc 44 $x abc 441 $y abc 61 $z def 44 $a def 441 $b def 61 $c and so on. For a known tariff_type, I need to find the rate that has the longest match= ing dialling prefix. In the data above, if I made a phone call to +44123456= 78 using tariff_type abc, then I would want to retrieve the record abc,441,= $y and not the record abc,44,$x. I do this currently by dividing up the pho= ne number and using this query: select * from tariff where tariff_type =3D 'UIA' and prefix in ('44','441','4412','44123','441234','4412345','44123456') order by prefix desc limit 1; The query doesn't use the primary key index as I might expect: Limit (cost=3D98.88..98.88 rows=3D1 width=3D31) -> Sort (cost=3D98.88..98.89 rows=3D7 width=3D31) Sort Key: prefix -> Seq Scan on tariff (cost=3D0.00..98.78 rows=3D7 width=3D31) Filter: ((tariff_type =3D 'UIA'::character varying) AND ((pr= efix =3D '44'::character varying) OR (prefix =3D '441'::character varying) = OR (prefix =3D '4412'::character varying) OR (prefix =3D '44123'::character= varying) OR (prefix =3D '441234'::character varying) OR (prefix =3D '44123= 45'::character varying) OR (prefix =3D '44123456'::character varying))) If I specify both parts of the key then it will, of course, use the index a= nd cost very little: select * from tariff where tariff_type =3D 'UIA' and prefix =3D '441' order by prefix desc limit 1;=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20 QUERY PLAN=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ---------------------------------------------------------------------------= ----------------------------------- Limit (cost=3D5.23..5.23 rows=3D1 width=3D31) -> Sort (cost=3D5.23..5.23 rows=3D1 width=3D31) Sort Key: prefix -> Index Scan using tariff_ix2 on tariff (cost=3D0.00..5.22 rows= =3D1 width=3D31) Index Cond: ((tariff_type =3D 'UIA'::character varying) AND = (prefix =3D '441'::character varying)) I have used this exact scenario using an Informix database and the query pl= anner is able to use the index: QUERY: ------ select * from tariff where tariff_type =3D 'SIL18' and (prefix =3D '44' or prefix =3D '441' or prefix =3D '4412' or prefix =3D '44123' or prefix =3D '441234' or prefix =3D '4412345' or prefix =3D '44123456' ) order by prefix desc Estimated Cost: 1 Estimated # of Rows Returned: 1 1) informix.tariff: INDEX PATH (1) Index Keys: tariff_type prefix (Key-First) Lower Index Filter: informix.old_tariff.tariff_type =3D 'SIL18'=20 Key-First Filters: (((((((informix.old_tariff.prefix =3D '44' OR i= nformix.ol d_tariff.prefix =3D '441' ) OR informix.old_tariff.prefix =3D '4412' ) OR i= nformix.old_ tariff.prefix =3D '44123' ) OR informix.old_tariff.prefix =3D '441234' ) OR= informix.ol d_tariff.prefix =3D '4412345' ) OR informix.old_tariff.prefix =3D '44123456= ' ) ) Is there a way I can achieve the same result using PostgreSQL 7.3.2? I can = add another index just on prefix and get a performance increase but it's st= ill not as cost-efficient as using the primary index. Would it be more cost= effective to do a bunch of individual queries for each length of prefix un= til I find one that matches? The average length of a prefix would probably = be around 3 digits and I would need to start at 8 digits and work back to c= over all possibilities. Thanks for any advice, David Witham