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 D9D0252991 for ; Wed, 8 Jun 2005 11:38:44 -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 44377-06 for ; Wed, 8 Jun 2005 14:38:38 +0000 (GMT) Received: from imsm058dat.netvigator.com (imsm058.netvigator.com [218.102.48.211]) by svr1.postgresql.org (Postfix) with ESMTP id D862852881 for ; Wed, 8 Jun 2005 11:38:35 -0300 (ADT) Received: from n2.netvigator.com ([218.102.224.145]) by imsm058dat.netvigator.com (InterMail vM.6.01.04.01 201-2131-118-101-20041129) with ESMTP id <20050608143839.QSJP3558.imsm058dat.netvigator.com@n2.netvigator.com>; Wed, 8 Jun 2005 22:38:39 +0800 Message-Id: <6.2.1.2.0.20050608221510.02c4e368@localhost> X-Mailer: QUALCOMM Windows Eudora Version 6.2.1.2 Date: Wed, 08 Jun 2005 22:25:16 +0800 To: George Essig From: K C Lau Subject: Re: SELECT DISTINCT Performance Issue Cc: pgsql-performance@postgresql.org In-Reply-To: <6744b38505060806342e6a07d@mail.gmail.com> References: <6.2.1.2.0.20050602190756.05ab2c70@localhost> <6744b38505060806342e6a07d@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.51 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_POST, FROM_ENDS_IN_NUMS X-Spam-Level: * X-Archive-Number: 200506/169 X-Sequence-Number: 12806 Both keys are text fields. Does it make any difference if PlayerID were integer? BTW, I think the real performance problem is when we use SELECT ... ORDER BY PlayerID DESC, AtDate DESC LIMIT 1 in a VIEW. Please see my subsequent email http://archives.postgresql.org/pgsql-performance/2005-06/msg00110.php on this show-stopper problem for which I still have no clue how to get around. Suggestions are much appreciated. Thanks and regards, KC. At 21:34 05/06/08, George Essig wrote: >On 6/2/05, K C Lau wrote: >... > > > > select DISTINCT ON (PlayerID) PlayerID,AtDate from Player where > > PlayerID='22220' order by PlayerID desc, AtDate desc; > > The Player table has primary key (PlayerID, AtDate) representing data over > > time and the query gets the latest data for a player. > > > > >... > > esdt=> explain analyze select DISTINCT ON (PlayerID) PlayerID,AtDate from > > Player > > where PlayerID='22220' order by PlayerID desc, AtDate desc; > > Unique (cost=0.00..2507.66 rows=1 width=23) (actual time=0.000..187.000 > > rows=1 loops=1) > > -> Index Scan Backward using pk_player on player (cost=0.00..2505.55 > > rows=8 > > 43 width=23) (actual time=0.000..187.000 rows=1227 loops=1) > > Index Cond: ((playerid)::text = '22220'::text) > > Total runtime: 187.000 ms > > > >Is PlayerID an integer datatype or a text datatype. It seems like >PlayerID should be an integer data type, but postgres treats PlayerID >as a text data type. This is because the value '22220' is quoted in >your query. Also, the explain analyze output shows "Index Cond: >((playerid)::text = '22220'::text". > >George Essig > >---------------------------(end of broadcast)--------------------------- >TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match