Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D489C633126 for ; Wed, 21 Jul 2010 12:00:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 24803-07 for ; Wed, 21 Jul 2010 15:00:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.10]) by mail.postgresql.org (Postfix) with ESMTP id E3B1C632998 for ; Wed, 21 Jul 2010 12:00:02 -0300 (ADT) Received: from [192.168.1.3] (static-71-179-240-8.bltmmd.fios.verizon.net [71.179.240.8]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0M6gB2-1OwidD1w84-00wSnK; Wed, 21 Jul 2010 16:59:56 +0200 Message-ID: <4C470B69.6080304@2ndquadrant.com> Date: Wed, 21 Jul 2010 10:59:53 -0400 From: Greg Smith User-Agent: Thunderbird 2.0.0.24 (X11/20100317) MIME-Version: 1.0 To: Elias Ghanem CC: pgsql-performance@postgresql.org Subject: Re: Using more tha one index per table References: <4C46A23B.2090606@acteos.com> In-Reply-To: <4C46A23B.2090606@acteos.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:akLN+C585s4K8Sls2oACg1Q+Rsce/MSowwZ2Rv7mh7c wBSi7KnZOsnnOdGDY3Wq1O+k21cvmqEKbHflfqNi0YHYGK9wQi odB4ruZYfOv2NxQiybzt/59gJ8NmduyJhBfA+jLQNO0Gpiij6i GUYYkm5z8Nqb2g62foD0e6Q5jWHWyihH4eM2yImYRdr3mWw3LV quhvgku8ey/SvfrPkR9sHlYduRSL+Zx0pLkl6uV01s= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.001 tagged_above=-10 required=5 tests=BAYES_20=-0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201007/302 X-Sequence-Number: 39691 Elias Ghanem wrote: > > I red that in PG a query can not use more than one index per table: "a > query or data manipulation command can use at most one index per table". > You'll find that at the very end of http://www.postgresql.org/docs/7.4/static/indexes.html and http://www.postgresql.org/docs/8.0/static/indexes.html ; try http://www.postgresql.org/docs/8.1/static/indexes.html instead and you'll discover that text has been removed because it was no longer true as of this version. If you find yourself at a PostgreSQL documentation page, often the search engines link to an older version with outdated information just because those have had more time accumulate links to them. A useful trick to know is that if you replace the version number with "current", you'll get to the latest version most of the time (sometimes the name of the page is changed between versions, too, but this isn't that frequent). So for this example, http://www.postgresql.org/docs/current/static/indexes.html will take you to the documentation for 8.4, which is the latest released version. As for your example, you can't test optimizer behavior with trivial tables. The overhead of using the index isn't zero, and it will often be deemed excessive for a small example. So for this: *"Index Scan using idx_col_2 on test_index (cost=0.00..8.27 rows=1 width=16) (actual time=0.092..0.092 rows=0 loops=1)"* *" Index Cond: (col_2 = 30)"* *" Filter: (col_1 = 15)"* Once it uses the one index, it only expects one row to be returned, at which point it has no need to use a second index. Faster to just look at that row and use some CPU time to determine if it matches. Using the second index for that instead would require some disk access to look up things in it, which will take longer than running the filter. That's why the second one isn't used. -- Greg Smith 2ndQuadrant US Baltimore, MD PostgreSQL Training, Services and Support greg@2ndQuadrant.com www.2ndQuadrant.us