Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 6E69B64FFAE for ; Mon, 17 Nov 2008 12:26:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 93223-04 for ; Mon, 17 Nov 2008 12:26:30 -0400 (AST) X-Greylist: delayed 00:33:09.149077 by SQLgrey-1.7.6 Received: from ulysses.lattica.com (lattica.com [204.14.19.114]) by mail.postgresql.org (Postfix) with ESMTP id 7EF9B64FF47 for ; Mon, 17 Nov 2008 12:26:30 -0400 (AST) Received: from localhost (ulysses.lattica.com [127.0.0.1]) by ulysses.lattica.com (Postfix) with ESMTP id 0641A7383DD for ; Mon, 17 Nov 2008 10:53:22 -0500 (EST) X-Virus-Scanned: amavisd-new at lattica.com Received: from ulysses.lattica.com ([127.0.0.1]) by localhost (ulysses.lattica.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xKIcMDqbVcBo for ; Mon, 17 Nov 2008 10:53:17 -0500 (EST) Received: from [192.168.3.11] (CPE00010242152b-CM0011aec8bfb4.cpe.net.cable.rogers.com [99.253.168.241]) by ulysses.lattica.com (Postfix) with ESMTP id 828547380A7 for ; Mon, 17 Nov 2008 10:53:17 -0500 (EST) Subject: Bad performance on simple query From: Dimi Paun To: pgsql-performance Content-Type: text/plain Organization: Lattica, Inc. Date: Mon, 17 Nov 2008 10:53:17 -0500 Message-Id: <1226937197.5760.89.camel@dimi.lattica.com> Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 (2.12.3-5.fc8) Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/145 X-Sequence-Number: 31876 Hi folks, I have a simple table that keeps track of a user's access history. It has a a few fields, but the important ones are: - ownerId: the user's ID, a int8 - accessTS: the timestamp of the record The table right now is small, only 1942 records. The user I test with (10015) has only 89 entries. What I want is to get the last 5 accesses of a user: SELECT * FROM triphistory WHERE ownerId = 10015 ORDER BY accessTS DESC LIMIT 5 If I create a composite index *and* analyze: create index IDX_TRIP_HISTORY_OWNER_ACCESS_TS on tripHistory (ownerId, accessTS); ANALYZE triphistory; It takes 0.091s (!): perpedes_db=# EXPLAIN ANALYZE SELECT * FROM triphistory WHERE ownerId = 10015 ORDER BY accessTS DESC LIMIT 5; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.00..7.99 rows=5 width=106) (actual time=0.024..0.042 rows=5 loops=1) -> Index Scan Backward using idx_trip_history_owner_access_ts on triphistory (cost=0.00..142.20 rows=89 width=106) (actual time=0.021..0.034 rows=5 loops=1) Index Cond: (ownerid = 10015) Total runtime: 0.091 ms (4 rows) BTW, this is after several runs of the query, shouldn't all this stuff be in memory? This is not a fast machine, but this seems rather excessive, no? -- Dimi Paun Lattica, Inc.