X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 18F08D1B1D5 for ; Mon, 5 Jul 2004 08:44:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 52664-07 for ; Mon, 5 Jul 2004 11:44:21 +0000 (GMT) Received: from lamb.mcmillan.net.nz (218-101-12-175.paradise.net.nz [218.101.12.175]) by svr1.postgresql.org (Postfix) with ESMTP id E84B4D1B1E0 for ; Mon, 5 Jul 2004 08:44:16 -0300 (ADT) Received: from lamb.mcmillan.net.nz (lamb.mcmillan.net.nz [127.0.0.1]) by lamb.mcmillan.net.nz (Postfix) with ESMTP id 9505FAD9859E; Mon, 5 Jul 2004 23:44:14 +1200 (NZST) Subject: Re: Seq scan vs. Index scan with different query From: Andrew McMillan To: eleven@ludojad.itpp.pl Cc: pgsql-performance@postgresql.org In-Reply-To: <20040705101515.GA12879@ludojad.itpp.pl> References: <20040705101515.GA12879@ludojad.itpp.pl> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-D7Gn1fqrapxF38GPn3pl" Date: Mon, 05 Jul 2004 23:44:13 +1200 Message-Id: <1089027854.6664.197.camel@lamb.mcmillan.net.nz> Mime-Version: 1.0 X-Mailer: Evolution 1.5.9.2 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200407/11 X-Sequence-Number: 7402 --=-D7Gn1fqrapxF38GPn3pl Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Mon, 2004-07-05 at 12:15 +0200, eleven@ludojad.itpp.pl wrote: > Hello, >=20 > Can anybody suggest any hint on this: >=20 > temp=3D> EXPLAIN SELECT DISTINCT "number" FROM "tablex" WHERE "Date" BETW= EEN '2004-06-28'::date AND '2004-07-04'::date AND "Time" BETWEEN '00:00:00'= ::time AND '18:01:00'::time; >=20 > Unique (cost=3D305669.92..306119.43 rows=3D89 width=3D8) > -> Sort (cost=3D305669.92..305894.67 rows=3D89903 width=3D8) > Sort Key: "number" > -> Index Scan using "DateTimeIndex" on "tablex" (cost=3D0.00..298272= .66 rows=3D89903 width=3D8) > Index Cond: (("Date" >=3D '2004-06-28'::date) AND ("Date" <=3D '2004-= 07-04'::date) AND ("Time" >=3D '00:00:00'::time without time zone) AND ("Ti= me" <=3D '18:01:00'::time without time zone)) >=20 >=20 > temp=3D> EXPLAIN SELECT DISTINCT "number" FROM "tablex" WHERE "Date" BETW= EEN '2004-06-28'::date AND '2004-07-04'::date AND "Time" BETWEEN '00:00:00'= ::time AND '19:01:00'::time; >=20 > Unique (cost=3D315252.77..315742.27 rows=3D97 width=3D8) > -> Sort (cost=3D315252.77..315497.52 rows=3D97900 width=3D8) > Sort Key: "number" > -> Seq Scan on "tablex" (cost=3D0.00..307137.34 rows=3D97900 width= =3D8) > Filter: (("Date" >=3D '2004-06-28'::date) AND ("Date" <=3D '2004-07-04= '::date) AND ("Time" >=3D '00:00:00'::time without time zone) AND ("Time" <= =3D '19:01:00'::time without time zone)) >=20 > Basically, the difference is in upper "Time" value (as you can see, it's > 18:01:00 in the first query and 19:01:00 in the other one).=20 > The question is - why does it use index in first case and=20 > it tries to do full sequential scan when the upper "Time" value > is different? >=20 > DateTimeIndex was created on both columns (Date/Time): > CREATE INDEX "DateTimeIndex" ON "tablex" USING btree ("Date", "Time"); PostgreSQL is always going to switch at some point, where the number of rows that have to be read from the table exceed some percentage of the total rows in the table. We can possibly be more helpful if you send EXPLAIN ANALYZE, rather than just EXPLAIN. A few things to be careful of: - Is this supposed to be a slice of midnight to 6pm, for each day between 28 June and 4 July? If you want a continuous period from Midnight 28 June -> 6pm 4 July you're better to have a single timestamp field. - It is unlikely that the , "Time" on your index is adding much to your selectivity, and it may be that you would be better off without it. - the DISTINCT can screw up your results, and it usually means that the SQL is not really the best it could be. A _real_ need for DISTINCT is quite rare in my experience, and from what I have seen it adds overhead and tends to encourage bad query plans when used unnecessarily. Hope this is some help. Regards, Andrew McMillan ------------------------------------------------------------------------- Andrew @ Catalyst .Net .NZ Ltd, PO Box 11-053, Manners St, Wellington WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St DDI: +64(4)803-2201 MOB: +64(272)DEBIAN OFFICE: +64(4)499-2267 Make things as simple as possible, but no simpler -- Einstein ------------------------------------------------------------------------- --=-D7Gn1fqrapxF38GPn3pl Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQBA6T8NjJA0f48GgBIRAuMEAJ9yMe8jagZvUB4gLyeolqpPtueeGQCgtzSG b2oxoi/xilX6DiQNvZAwfyE= =atnn -----END PGP SIGNATURE----- --=-D7Gn1fqrapxF38GPn3pl--