X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id E1EF511F6240 for ; Tue, 25 Apr 2006 12:05:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 30544-05 for ; Tue, 25 Apr 2006 12:05:07 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 3A1DC11F623C for ; Tue, 25 Apr 2006 12:05:04 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Tue, 25 Apr 2006 15:05:02 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 25 Apr 2006 10:05:02 -0500 Subject: Re: Query on postgresql 7.4.2 not using index From: Scott Marlowe To: arnaulist@andromeiberica.com Cc: chris smith , pgsql-performance@postgresql.org In-Reply-To: <444E28ED.3090900@andromeiberica.com> References: <444E240D.3050000@andromeiberica.com> <3c1395330604250634r6f215ceame157f3dd5f5f4fa3@mail.gmail.com> <444E28ED.3090900@andromeiberica.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1145977502.23538.201.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Tue, 25 Apr 2006 10:05:02 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200604/548 X-Sequence-Number: 18632 On Tue, 2006-04-25 at 08:49, Arnau wrote: > chris smith wrote: > > On 4/25/06, Arnau wrote: > > > >>Hi all, > >> > >> I have the following running on postgresql version 7.4.2: > >> > >>CREATE SEQUENCE agenda_user_group_id_seq > >>MINVALUE 1 > >>MAXVALUE 9223372036854775807 > >>CYCLE > >>INCREMENT 1 > >>START 1; > >> > >>CREATE TABLE AGENDA_USERS_GROUPS > >>( > >> AGENDA_USER_GROUP_ID INT8 > >> CONSTRAINT pk_agndusrgrp_usergroup PRIMARY KEY > >> DEFAULT NEXTVAL('agenda_user_group_id_seq'), > >> USER_ID NUMERIC(10) > >> CONSTRAINT fk_agenda_uid REFERENCES > >>AGENDA_USERS (USER_ID) > >> ON DELETE CASCADE > >> NOT NULL, > >> GROUP_ID NUMERIC(10) > >> CONSTRAINT fk_agenda_gid REFERENCES > >>AGENDA_GROUPS (GROUP_ID) > >> ON DELETE CASCADE > >> NOT NULL, > >> CREATION_DATE DATE > >> DEFAULT CURRENT_DATE, > >> CONSTRAINT un_agndusrgrp_usergroup > >>UNIQUE(USER_ID, GROUP_ID) > >>); > >> > >>CREATE INDEX i_agnusrsgrs_userid ON AGENDA_USERS_GROUPS ( USER_ID ); > >>CREATE INDEX i_agnusrsgrs_groupid ON AGENDA_USERS_GROUPS ( GROUP_ID ); SNIP > The same, the table has 2547556 entries: > > espsm_moviltelevision=# EXPLAIN ANALYZE SELECT agenda_user_group_id FROM > agenda_users_groups > espsm_moviltelevision-# WHERE group_id::int8 = 9; > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------- > Seq Scan on agenda_users_groups (cost=0.00..59477.34 rows=12738 > width=8) (actual time=3409.541..11818.794 rows=367026 loops=1) > Filter: ((group_id)::bigint = 9) > Total runtime: 13452.114 ms > (3 filas) > > espsm_moviltelevision=# EXPLAIN ANALYZE SELECT agenda_user_group_id FROM > agenda_users_groups > espsm_moviltelevision-# WHERE group_id = '9'; > QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------- > Seq Scan on agenda_users_groups (cost=0.00..53108.45 rows=339675 > width=8) (actual time=916.903..5763.830 rows=367026 loops=1) > Filter: (group_id = 9::numeric) > Total runtime: 7259.861 ms > (3 filas) > > espsm_moviltelevision=# select count(*) from agenda_users_groups ; > count > --------- > 2547556 OK, a few points. 1: 7.4.2 is WAY out of date for the 7.4 series. The 7.4 series, also it a bit out of date, and many issues in terms of performance have been enhanced in the 8.x series. You absolutely should update to the latest 7.4 series, as there are known data loss bugs and other issues in the 7.4.2 version. 2: An index scan isn't always faster. In this instance, it looks like the number of rows that match in the last version of your query is well over 10% of the rows. Assuming your average row takes up <10% or so of a block, which is pretty common, then you're going to have to hit almost every block anyway to get your data. So, an index scan is no win. 3: To test whether or not an index scan IS a win, you can use the enable_xxx settings to prove it to yourself: set enable_seqscan = off; explain analyze ; and compare. Note that the enable_seqscan = off thing is a sledge hammer, not a nudge, and generally should NOT be used in production. If an index scan is generally a win for you, but the database isn't using it, you might need to tune the database for your machine. note that you should NOT tune your database based on a single query. You'll need to reach a compromise on your settings that makes all your queries run reasonably fast without the planner making insane decisions. One of the better postgresql tuning docs out there is the one at: http://www.varlena.com/GeneralBits/Tidbits/perf.html . Good luck.