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 E9D9053ABE for ; Sun, 8 May 2005 10:44:46 -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 79469-06 for ; Sun, 8 May 2005 13:44:38 +0000 (GMT) Received: from server07.icaen.uiowa.edu (server07.icaen.uiowa.edu [128.255.17.47]) by svr1.postgresql.org (Postfix) with ESMTP id AF26653A21 for ; Sun, 8 May 2005 10:44:36 -0300 (ADT) Received: from server11.icaen.uiowa.edu (server11.icaen.uiowa.edu [128.255.17.51]) by server07.icaen.uiowa.edu (8.13.3/8.12.9) with ESMTP id j48Dicph004331; (envelope-from ) Sun, 8 May 2005 08:44:38 -0500 (CDT) Received: from [192.168.1.11] (12-215-118-172.client.mchsi.com [12.215.118.172]) (authenticated user=jfmeinel) by server11.icaen.uiowa.edu (8.13.2/smtp-serv-1.7) with ESMTP id j48DiaW8024934 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256); (envelope-from ) Sun, 8 May 2005 08:44:37 -0500 (CDT) Message-ID: <427E17BC.7050407@arbash-meinel.com> Date: Sun, 08 May 2005 08:44:28 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2-1.3.2 (X11/20050324) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jeroen van Iddekinge Cc: pgsql-performance@postgresql.org Subject: Re: sequence scan on PK References: <427E2941.9070105@lycos.com> In-Reply-To: <427E2941.9070105@lycos.com> X-Enigmail-Version: 0.89.6.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigEC5314BD4A2BDB8114BFA6BB" X-Virus-Scanned: ClamAV version 0.83, clamav-milter version 0.83 on clamav.icaen.uiowa.edu X-Virus-Scanned: ClamAV version 0.83, clamav-milter version 0.83 on clamav.icaen.uiowa.edu X-Virus-Status: Clean X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/76 X-Sequence-Number: 12240 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigEC5314BD4A2BDB8114BFA6BB Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Jeroen van Iddekinge wrote: > Hi, > > > I understand that when a table contains only a few rows it is better to > do a sequence scan than an index scan. But is this also for a table with > 99 records? > ... > explain select * from tblFolders where id=90; > QUERY PLAN > ----------------------------------------------------------- > Seq Scan on tblfolders (cost=0.00..3.24 rows=1 width=50) > Filter: (id = 90) > > > (I have analyze table bit still a sequence scan). > > With how manys rows it is ok to do an index scan or sequence scan? How > is this calculated in pg? > > Regards > Jer It depends on how many pages need to be read. To do an index scan you need to read the index pages, and then you read the page where the actual row resides. Usually the comment is if you are selecting >5% of the rows, seqscan is faster than an index scan. If I'm reading your query correctly, it is estimating needing to read about 3 pages to get the row you are asking for. If you used an index, it probably would have to read at least that many pages, and they would not be a sequential read, so it should be slower. If you want to prove it, try: \timing EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; SET enable_seqscan TO OFF; EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; EXPLAIN ANALYZE SELECT * FROM tblFolders WHERE id=90; Run multiple times to make sure everything is cached, and take the fastest time. On your machine it might be true that the index scan is slightly faster than the seqscan in this exact circumstance. But I have the feeling the time is trivially different, and if you had say 70 rows it would favor seqscan. Probably somewhere at 150-200 rows it will switch on it's own. You could tweak with several settings to get it to do an index scan earlier, but these would probably break other queries. You don't need to tune for 100 rows, more like 100k or 100M. John =:-> --------------enigEC5314BD4A2BDB8114BFA6BB 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.1 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org iD8DBQFCfhe+JdeBCYSNAAMRAoEDAJ9OHPdMzjAd5KJXMMez+kuux8HlygCeOIBJ ZUOkCzlvqF2689jMlB1AMIE= =uWmL -----END PGP SIGNATURE----- --------------enigEC5314BD4A2BDB8114BFA6BB--