Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 16D2A633A22 for ; Fri, 1 Jan 2010 07:54:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 36717-03 for ; Fri, 1 Jan 2010 11:54:24 +0000 (UTC) X-Greylist: delayed 00:05:32.506408 by SQLgrey-1.7.6 Received: from shrek.krogh.cc (2605ds1-ynoe.0.fullrate.dk [90.184.12.24]) by mail.postgresql.org (Postfix) with ESMTP id 1127F632557 for ; Fri, 1 Jan 2010 07:54:24 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by shrek.krogh.cc (Postfix) with ESMTP id B134E1EE84C for ; Fri, 1 Jan 2010 12:48:48 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at shrek.krogh.cc Received: from shrek.krogh.cc ([127.0.0.1]) by localhost (shrek.krogh.cc [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KJ82-rpQUrAa for ; Fri, 1 Jan 2010 12:48:39 +0100 (CET) Received: from [90.184.13.137] (unknown [90.184.13.137]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: jesper@krogh.cc) by shrek.krogh.cc (Postfix) with ESMTPSA id B811E1EE7EF for ; Fri, 1 Jan 2010 12:48:39 +0100 (CET) Message-ID: <4B3DE11B.1010708@krogh.cc> Date: Fri, 01 Jan 2010 12:48:43 +0100 From: Jesper Krogh User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Message queue table - strange performance drop with changing limit size. Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.589 tagged_above=-10 required=5 tests=BAYES_00=-2.599, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/1 X-Sequence-Number: 36881 Hi. I have a "message queue" table, that contains in the order of 1-10m "messages". It is implemented using TheSchwartz: http://search.cpan.org/~bradfitz/TheSchwartz-1.07/lib/TheSchwartz.pm So when a "worker" picks the next job it goes into the "job" table an select the top X highest priority messages with the "funcid" that it can work on. The table looks like this: db=# \d workqueue.job Table "workqueue.job" Column | Type | Modifiers ---------------+----------+--------------------------------------------------------------- jobid | integer | not null default nextval('workqueue.job_jobid_seq'::regclass) funcid | integer | not null arg | bytea | uniqkey | text | insert_time | integer | run_after | integer | not null grabbed_until | integer | not null priority | smallint | coalesce | text | Indexes: "job_funcid_key" UNIQUE, btree (funcid, uniqkey) "funcid_coalesce_priority" btree (funcid, "coalesce", priority) "funcid_prority_idx2" btree (funcid, priority) "job_jobid_idx" btree (jobid) efam=# explain ANALYZe select jobid from workqueue.job where job.funcid in (3) order by priority asc limit 1000; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.00..2008.53 rows=1000 width=6) (actual time=0.077..765.169 rows=1000 loops=1) -> Index Scan using funcid_prority_idx2 on job (cost=0.00..7959150.95 rows=3962674 width=6) (actual time=0.074..763.664 rows=1000 loops=1) Index Cond: (funcid = 3) Total runtime: 766.104 ms (4 rows) efam=# explain ANALYZe select jobid from workqueue.job where job.funcid in (3) order by priority asc limit 50; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..100.43 rows=50 width=6) (actual time=0.037..505.765 rows=50 loops=1) -> Index Scan using funcid_prority_idx2 on job (cost=0.00..7959150.95 rows=3962674 width=6) (actual time=0.035..505.690 rows=50 loops=1) Index Cond: (funcid = 3) Total runtime: 505.959 ms (4 rows) efam=# explain ANALYZe select jobid from workqueue.job where job.funcid in (3) order by priority asc limit 10; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..20.09 rows=10 width=6) (actual time=0.056..0.653 rows=10 loops=1) -> Index Scan using funcid_prority_idx2 on job (cost=0.00..7959152.95 rows=3962674 width=6) (actual time=0.054..0.640 rows=10 loops=1) Index Cond: (funcid = 3) Total runtime: 0.687 ms (4 rows) So what I see is that "top 10" takes < 1ms, top 50 takes over 500 times more, and top 1000 only 1.5 times more than top 50. What can the reason be for the huge drop between limit 10 and limit 50 be? -- Jesper