Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 743429FC368 for ; Fri, 18 May 2007 16:36:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 73988-08 for ; Fri, 18 May 2007 16:36:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id C1FBA9FC39E for ; Fri, 18 May 2007 16:36:23 -0300 (ADT) Received: from [10.10.1.37] ([10.10.1.37]) by exchange.g2switchworks.com with Microsoft SMTPSVC(6.0.3790.1830); Fri, 18 May 2007 14:36:22 -0500 Message-ID: <464E0036.3030804@g2switchworks.com> Date: Fri, 18 May 2007 14:36:22 -0500 From: Scott Marlowe User-Agent: Thunderbird 1.5.0.4 (X11/20060516) MIME-Version: 1.0 To: "Tyrrill, Ed" CC: pgsql-performance@postgresql.org Subject: Re: Slow queries on big table References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 18 May 2007 19:36:22.0509 (UTC) FILETIME=[D0BBE9D0:01C79983] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/340 X-Sequence-Number: 24730 Tyrrill, Ed wrote: > I have a two column table with over 160 million rows in it. As the size > of the table grows queries on this table get exponentially slower. I am > using version 8.1.5 32-bit on Red Hat Enterprise Linux 3. The hardware > is an Intel 3 Ghz Xeon with 4GB RAM, and 6 disks in a RAID 5 > configuration. For current testing I am running a single database > connection with no other applications running on the machine, and the > swap is not being used at all. > > Here is the table definition: > > mdsdb=# \d backup_location > Table "public.backup_location" > Column | Type | Modifiers > -----------+---------+----------- > record_id | bigint | not null > backup_id | integer | not null > Indexes: > "backup_location_pkey" PRIMARY KEY, btree (record_id, backup_id) > "backup_location_rid" btree (record_id) > Foreign-key constraints: > "backup_location_bfk" FOREIGN KEY (backup_id) REFERENCES > backups(backup_id) ON DELETE CASCADE > > Here is the table size: > > mdsdb=# select count(*) from backup_location; > count > ----------- > 162101296 > (1 row) > > And here is a simple query on this table that takes nearly 20 minutes to > return less then 3000 rows. I ran an analyze immediately before I ran > this query: > > mdsdb=# explain analyze select record_id from backup_location where > backup_id = 1070; > > QUERY PLAN > > ------------------------------------------------------------------------ > ------------------------------------------------------------------------ > ------------- > Index Scan using backup_location_pkey on backup_location > (cost=0.00..1475268.53 rows=412394 width=8) (actual > time=3318.057..1196723.915 rows=2752 loops=1) > Index Cond: (backup_id = 1070) > Total runtime: 1196725.617 ms > (3 rows) > I've got a few points. Firstly, is your data amenable to partitioning? If so that might be a big winner. Secondly, it might be more efficient for the planner to choose the backup_location_rid index than the combination primary key index. You can test this theory with this cool pg trick: begin; alter table backup_location drop constraint backup_location_pkey; explain analyze select .... rollback; to see if it's faster. > Obviously at this point the application is not usable. If possible we > would like to grow this table to the 3-5 billion row range, but I don't > know if that is realistic. > > Any guidance would be greatly appreciated. > Without knowing more about your usage patterns, it's hard to say. But partitioning seems like your best choice at the moment.