Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 9F4561337B4F for ; Mon, 29 Nov 2010 08:30:52 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 56448-02 for ; Mon, 29 Nov 2010 12:30:45 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out.mail.amis.hr (out2.mail.amis.hr [188.129.0.5]) by mail.postgresql.org (Postfix) with ESMTP id 594C71335B36 for ; Mon, 29 Nov 2010 08:30:45 -0400 (AST) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out.mail.amis.hr (Postfix) with ESMTP id 65FA920371; Mon, 29 Nov 2010 13:30:45 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id oATCRER0013696; Mon, 29 Nov 2010 13:30:45 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id svab3g1a2-1; Mon, 29 Nov 2010 13:30:45 +0100 A-Original-Client-UID: 188.129.73.52 splivalo Received: from [192.168.10.1] (cpe-188-129-73-52.dynamic.amis.hr [188.129.73.52]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id D76E5273; Mon, 29 Nov 2010 13:30:44 +0100 (CET) Message-ID: <4CF39CF4.2050506@megafon.hr> Date: Mon, 29 Nov 2010 13:30:44 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 MIME-Version: 1.0 To: mark.kirkwood@catalyst.net.nz CC: pgsql-performance@postgresql.org Subject: Re: SELECT INTO large FKyed table is slow References: <4CF24103.8030209@megafon.hr> <4CF3522B.2050203@catalyst.net.nz> In-Reply-To: <4CF3522B.2050203@catalyst.net.nz> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2010-11-29_06:2010-11-29, 2010-11-29, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=1 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1011290040 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201011/397 X-Sequence-Number: 41421 On 11/29/2010 08:11 AM, Mark Kirkwood wrote: > On 29/11/10 00:46, Mario Splivalo wrote: >> >> This is the slow part: >> INSERT INTO drones_history (sample_id, drone_id, drone_log_notice, >> drone_temperature, drone_pressure) >> SELECT * FROM tmpUpdate; >> >> For 100 rows this takes around 2 seconds. For 1000 rows this takes >> around 40 seconds. For 5000 rows this takes around 5 minutes. >> For 50k rows this takes around 30 minutes! Now this is where I start >> lag because I get new CSV every 10 minutes or so. > > Have you created indexes on drones_history(sample_id) and > drones_history(drone_id)? That would probably help speed up your INSERT > quite a bit if you have not done so. Yes, since (sample_id, drone_id) is primary key, postgres created composite index on those columns. Are you suggesting I add two more indexes, one for drone_id and one for sample_id? > Also it would be worthwhile for you to post the output of: > > EXPLAIN ANALYZE INSERT INTO drones_history (sample_id, drone_id, > drone_log_notice, drone_temperature, drone_pressure) > SELECT * FROM tmpUpdate; > > to the list, so we can see what is taking the time. Is there a way to do so inside plpgsql function? I can recreate the whole process within psql and then post the explain analyze, it would just take me some time to do so. I'll post as soon as I'm done. Mario