Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 7DC372E339E for ; Wed, 28 Nov 2007 07:06:53 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 33213-07 for ; Wed, 28 Nov 2007 07:06:49 -0400 (AST) Received: from mail.resen.org (h55eb180c.c45-01-09.sta.perspektivbredband.net [85.235.24.12]) by postgresql.org (Postfix) with ESMTP id BDB382E339A for ; Wed, 28 Nov 2007 07:06:48 -0400 (AST) Received: from localhost (server [127.0.0.1]) by mail.resen.org (Postfix) with ESMTP id 8C773950AAF; Wed, 28 Nov 2007 02:08:53 +0100 (CET) Received: from mail.resen.org ([127.0.0.1]) by localhost (server [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 22819-06; Wed, 28 Nov 2007 02:08:40 +0100 (CET) Received: from [10.0.0.101] (unknown [10.0.0.101]) by mail.resen.org (Postfix) with ESMTP id 2CBCF950562; Wed, 28 Nov 2007 02:08:40 +0100 (CET) Message-ID: <474CBF98.6070508@amossen.dk> Date: Wed, 28 Nov 2007 02:08:40 +0100 From: tmp User-Agent: Thunderbird 2.0.0.6 (X11/20071022) MIME-Version: 1.0 To: Tom Lane Cc: pgsql-performance@postgresql.org Subject: Re: Query only slow on first run References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> <056501c83128$55d58a90$8e00a8c0@tridecap.com> <20071127233833.GA24031@uio.no> <15029.1196209554@sss.pgh.pa.us> In-Reply-To: <15029.1196209554@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at resen.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.464 tagged_above=0 required=5 tests=AWL=0.463, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200711/335 X-Sequence-Number: 27750 > The query's spending nearly all its time in the scan of "posts", and > I'm wondering why --- doesn't seem like it should take 6400msec to fetch > 646 rows, unless perhaps the data is just horribly misordered relative > to the index. Which may in fact be the case ... Yes, they probably are. I use the random_number column in order to receive a semi random sample subset from the large amount of rows. The technique is described in [1]. This subset is later used for some statistical investigation, but this is somewhat irrelevant here. In order to receive the sample fast, I have made an index on the random_number column. > what exactly is that > "random_number" column A random float that is initialized when the row is created and never modified afterwards. The physical row ordering will clearly not match the random_number ordering. However, other queries uses a row ordering by the primary key so I don't think it would make much sense to make the index on random_number a clustering index just in order to speed up this single query. > and why are you desirous of ordering by it? In order to simulate a random pick of K rows. See [1]. > For that matter, if it is what it sounds like, why is it sane to group > by it? You'll probably always get groups of one row ... For each random_number, another table (question_tags) holds zero or more rows satisfying a number of constraints. I need to count(*) the number of corresponding question_tag rows for each random_number. We have primarily two tables of interest here: questions (~100k rows) and posts (~400k rows). Each post refers to a question, but only the "posts" rows for which the corresponding "question.status = 1" are relevant. This reduces the number of relevant question rows to about 10k. Within the post rows corresponding to these 10k questions I would like to pick a random sample of size K. [1] http://archives.postgresql.org/pgsql-general/2007-10/msg01240.php