Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4E5C264FFAE for ; Mon, 17 Nov 2008 12:53:56 -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 99149-03 for ; Mon, 17 Nov 2008 12:53:53 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.176]) by mail.postgresql.org (Postfix) with ESMTP id BC6EA64FDB7 for ; Mon, 17 Nov 2008 12:53:52 -0400 (AST) Received: by wa-out-1112.google.com with SMTP id j4so1296435wah.19 for ; Mon, 17 Nov 2008 08:53:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=20w7jqSvpwZGnBU7tePZYw19fQ6P98MWJYc7U/BIxzs=; b=UPO9XVs3zALZHuWhsYAchCgRlWT7E0eImezwqL7CYli+cXrpXj+0TNn79TEBp3lDk5 QOawCOW28TjpQ9YzyHeIOs/rq2yvG/jJzhwa/L9Okr51JtlGBmjBI6gzVMuoYyDFPVqH CZn+02wo1lrV6YoCS4Hdra5BzfxCxitlJyhdY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=cnSWmD35GiVPHzFJ5Y3biiD7uU4IxAqfzMLBqySvoJJ74DTuo9WNlOzeAyaZXxIGDj Tp3lmDSX1n+xbEHU92Vux4xXdr7DnRhNy0JVaQB05NA1xA2vnoM0/INDd4SY5JDnRq+n x6T4SyJUWJAHi+4UNe9ABdams3QYCywAG2QLI= Received: by 10.114.192.17 with SMTP id p17mr2612004waf.196.1226940831781; Mon, 17 Nov 2008 08:53:51 -0800 (PST) Received: by 10.114.137.14 with HTTP; Mon, 17 Nov 2008 08:53:51 -0800 (PST) Message-ID: Date: Mon, 17 Nov 2008 09:53:51 -0700 From: "Scott Marlowe" To: "Dimi Paun" Subject: Re: Bad performance on simple query Cc: pgsql-performance In-Reply-To: <1226937197.5760.89.camel@dimi.lattica.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1226937197.5760.89.camel@dimi.lattica.com> 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/147 X-Sequence-Number: 31878 On Mon, Nov 17, 2008 at 8:53 AM, Dimi Paun wrote: > 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? Are you saying it's excessive you need the compound query? Cause that's running in 91microseconds as pointed out by Alan.