Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z5xMr-0003H6-IH for pgsql-performance@arkaria.postgresql.org; Fri, 19 Jun 2015 14:34:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z5xMq-0002P7-EV for pgsql-performance@arkaria.postgresql.org; Fri, 19 Jun 2015 14:34:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z5xMn-0002NZ-TZ for pgsql-performance@postgresql.org; Fri, 19 Jun 2015 14:34:02 +0000 Received: from resqmta-ch2-01v.sys.comcast.net ([2001:558:fe21:29:69:252:207:33]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z5xMi-0007cO-Ln for pgsql-performance@postgresql.org; Fri, 19 Jun 2015 14:34:00 +0000 Received: from resomta-ch2-02v.sys.comcast.net ([69.252.207.98]) by resqmta-ch2-01v.sys.comcast.net with comcast id iEXu1q00427uzMh01EZqpE; Fri, 19 Jun 2015 14:33:50 +0000 Received: from [10.0.0.151] ([173.166.19.109]) by resomta-ch2-02v.sys.comcast.net with comcast id iEZp1q00L2MCEZH01EZplt; Fri, 19 Jun 2015 14:33:50 +0000 Message-ID: <55842859.4060102@intuvisiontech.com> Date: Fri, 19 Jun 2015 10:34:01 -0400 From: Ian Pushee User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Slow query (planner insisting on using 'external merge' sort type) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20140121; t=1434724430; bh=ZiHnTe3F+4CDjZL6dLd+i61F3isPwVJnksvLso2sNBk=; h=Received:Received:Message-ID:Date:From:MIME-Version:To:Subject: Content-Type; b=dIMNBwPwCU+VqTQrbGaETRdzTzA9xlwZsQKRKdOtFg8/xS2b2L4X4gdD17/bbTglU MUp5+E/rpziIlXeFNTkAYgljXeiiVGQBAT2T/mXZauXYxzPDzzzc8fc2e/9j9G2NlS p9zusNzuYsqn0k9MQmCsT6xxZTxuqKD+UpwATTCJEVDilzGX5eIn5LTHPdn7L7ujFZ 2QO0xSV5699VhGofbVCgbkleHIlLQRCz1kNeZh9ih1KAiwJjR4RvWQJglovZX1Z22h osGZQOAq698QK8WnC7IvGKZT3v8RQLAp20PQwiRCXIEzsxx8N2LOjHkzrB74ZD7QKj qxzRWww11ZjGw== X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Hi Folks, This is my first time posting here, so hopefully I manage to convey all the information needed. We have a simple query that just started giving us problems in production when the number of rows gets too large (>100k). The issue seems to be that the planner wants to sort the rows using a sequential scan, rather than the index provided specifically for this query. This isn't a problem with low numbers of rows, but eventually the query outgrows work_mem and uses the disk, slowing does the query greatly. I know the common answer is to increase work_mem... but since this tables growth is unpredictable, that isn't a viable strategy. I've tried increasing shared_buffers and effective_cache_size, but that doesn't appear to effect the plan chosen here. Setting random_page_cost=1.0 works, but I'm hoping for a more general solution that doesn't require setting that locally each time I run the query. I guess my real question is wether or not there is any way to get the planner to take into account the fact that it's going to need to do an 'external merge', and that it is going to take a LONG time? Table and Index Schemas: CREATE TABLE events ( id serial NOT NULL, name character varying(64), eventspy_id integer NOT NULL, camera_id integer NOT NULL, start_time timestamp without time zone NOT NULL, millisecond smallint NOT NULL, uid smallint NOT NULL, update_time timestamp without time zone NOT NULL DEFAULT now(), length integer NOT NULL, objects text NOT NULL, priority smallint NOT NULL, type character varying(45) NOT NULL DEFAULT 'alarm'::character varying, status event_status NOT NULL DEFAULT 'new'::event_status, confidence smallint NOT NULL DEFAULT 100::smallint, CONSTRAINT events_pkey PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); CREATE INDEX events_confidnce ON events USING btree (confidence); CREATE INDEX events_summary ON events USING btree (name COLLATE pg_catalog."default", eventspy_id, camera_id, type COLLATE pg_catalog."default", status); Query: SELECT name, type, eventspy_id, camera_id, status, COUNT(id), MAX(update_time), MIN(start_time), MAX(start_time) FROM events WHERE confidence>=0 GROUP BY name, eventspy_id, camera_id, type, status; Explain Analyze outputs (links as requested): Default plan: http://explain.depesz.com/s/ib3k Forced index (random_page_cost=1.0): http://explain.depesz.com/s/lYaP Software/Hardware: PGSql 9.2.1, Windows 8.1, 8GB RAM All pgsql settings are at their defaults. Thanks for any help you can provide, -Ian Pushee -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance