Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DF2AC632AFD for ; Thu, 19 Aug 2010 03:14:19 -0300 (ADT) 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 63391-05 for ; Thu, 19 Aug 2010 06:14:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f46.google.com (mail-fx0-f46.google.com [209.85.161.46]) by mail.postgresql.org (Postfix) with ESMTP id 9FAD7632A48 for ; Thu, 19 Aug 2010 03:14:11 -0300 (ADT) Received: by fxm13 with SMTP id 13so808304fxm.19 for ; Wed, 18 Aug 2010 23:14:10 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.119.198 with SMTP id a6mr8745829far.66.1282198450152; Wed, 18 Aug 2010 23:14:10 -0700 (PDT) Received: by 10.223.150.11 with HTTP; Wed, 18 Aug 2010 23:14:10 -0700 (PDT) X-Originating-IP: [66.215.95.51] Date: Wed, 18 Aug 2010 23:14:10 -0700 Message-ID: Subject: yet another q From: Samuel Gendler To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=001636c5b91e272460048e27174d X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.918 tagged_above=-10 required=5 tests=BAYES_00=-1.9, HTML_FONT_FACE_BAD=0.981, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201008/216 X-Sequence-Number: 40059 --001636c5b91e272460048e27174d Content-Type: text/plain; charset=ISO-8859-1 Please forgive the barrage of questions. I'm just learning how to tune things in postgres and I've still got a bit of learning curve to get over, apparently. I have done a lot of reading, though, I swear. I've got two identical queries except for a change of one condition which cuts the number of rows in half - which also has the effect of eliminating one partition from consideration (partitioned over time and I cut the time period in half). The query plans are considerably different as a result. The net result is that the fast query is 5x faster than the slow query. I'm curious if the alternate query plan is inherently faster or is it just a case of the algorithm scaling worse than linearly with the row count, which certainly wouldn't be surprising. The big win, for me, is that the sort uses vastly less memory. The slow plan requires work_mem to be 1500MB to even make it 5x worse. With a more reasonable work_mem (400MB), it drops to something like 15x worse because it has to sort on disk. fast plan: http://explain.depesz.com/s/iZ slow plan: http://explain.depesz.com/s/Dv2 query: EXPLAIN ANALYZE SELECT t_lookup.display_name as group, to_char(t_fact.time, 'DD/MM HH24:MI') as category, substring(t_lookup.display_name from 1 for 20) as label, round(sum(t_fact.total_ms)/sum(t_fact.count)) as value FROM portal.providers t_lookup, day_scale_radar_performance_fact t_fact WHERE t_fact.probe_type_num < 3 and t_lookup.provider_id = t_fact.provider_id and t_lookup.provider_owner_customer_id = t_fact.provider_owner_customer_id and t_fact.provider_owner_customer_id = 0 and t_fact.time between timezone('UTC', '2010-08-18 15:00:00') - interval '30 day' and timezone('UTC', '2010-08-18 15:00:00') GROUP BY t_fact.provider_owner_customer_id, t_fact.provider_id, t_lookup.display_name, t_fact.time ORDER BY t_fact.time table structure: Table "perf_reporting.abstract_radar_performance_fact" Column | Type | Modifiers ----------------------------+-----------------------------+----------- count | bigint | not null total_ms | bigint | not null time | timestamp without time zone | not null market_num | integer | not null country_num | integer | not null autosys_num | integer | not null provider_owner_zone_id | integer | not null provider_owner_customer_id | integer | not null provider_id | integer | not null probe_type_num | integer | not null with individual indexes on the everything from time to the bottom on the child tables and Table "portal.providers" Column | Type | Modifiers ----------------------------+-----------------------------+------------------------ btime | timestamp without time zone | not null default now() mtime | timestamp without time zone | not null default now() version | integer | not null default 1 provider_id | integer | not null provider_owner_zone_id | integer | not null provider_owner_customer_id | integer | not null provider_category_id | integer | not null name | character varying(255) | not null display_name | character varying(255) | not null with indexes on every column with name ending in '_id' --001636c5b91e272460048e27174d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Please forgive the barrage of questions. =A0I'm just learning how to tu= ne things in postgres and I've still got a bit of learning curve to get= over, apparently. =A0I have done a lot of reading, though, I swear.
I've got two identical queries except for a change of one condition whi= ch cuts the number of rows in half - which also has the effect of eliminati= ng one partition from consideration (partitioned over time and I cut the ti= me period in half). =A0The query plans are considerably different as a resu= lt. The net result is that the fast query is 5x faster than the slow query.= =A0I'm curious if the alternate query plan is inherently faster or is = it just a case of the algorithm scaling worse than linearly with the row co= unt, which certainly wouldn't be surprising. =A0The big win, for me, is= that the sort uses vastly less memory. =A0The slow plan requires work_mem = to be 1500MB to even make it 5x worse. =A0With a more reasonable work_mem (= 400MB), it drops to something like 15x worse because it has to sort on disk= .

fast plan: http://explain.de= pesz.com/s/iZ
slow plan: http://explain.depesz.com/s/Dv2

query:


EXPLAIN ANA= LYZE SELECT
=A0 =A0 =A0 =A0 =A0 =A0t_lookup.display_name as group,
=A0 =A0 =A0 = =A0 =A0 =A0to_char(t_fact.time, 'DD/MM HH24:MI') as category, =A0 = =A0 =A0 =A0 =A0
=A0 =A0 =A0 =A0 =A0 =A0substring(t_lookup.display_name = from 1 for 20) as label,
=A0 =A0 =A0 =A0 =A0 =A0round(sum(t_fact.total_= ms)/sum(t_fact.count)) as value
=A0 =A0 =A0 =A0FROM
=A0 =A0 =A0 =A0 =A0 =A0portal.providers t_lookup,<= br> =A0 =A0 =A0 =A0 =A0 =A0day_scale_radar_performance_fact t_fact
=A0 = =A0 =A0 =A0WHERE
=A0 =A0 =A0 =A0 =A0 =A0t_fact.probe_type_num < 3 =A0 =A0 =A0 =A0 =A0 =A0and t_lookup.provider_id =3D t_fact.provider_id =A0 =A0 =A0 =A0 =A0 =A0and t_lookup.provider_owner_customer_id =3D t_fact.= provider_owner_customer_id
=A0 =A0 =A0 =A0 =A0 =A0and t_fact.provider_o= wner_customer_id =3D 0
=A0 =A0 =A0 =A0 =A0 =A0and t_fact.time between t= imezone('UTC', '2010-08-18 15:00:00') - interval '30 da= y' and timezone('UTC', '2010-08-18 15:00:00')
=A0 =A0 =A0 =A0GROUP BY
=A0 =A0 =A0 =A0 =A0 =A0t_fact.provider_owner_c= ustomer_id, t_fact.provider_id,
=A0 =A0 =A0 =A0 =A0 =A0t_lookup.display= _name,
=A0 =A0 =A0 =A0 =A0 =A0t_fact.time
=A0 =A0 =A0 =A0ORDER BY <= br> =A0 =A0 =A0 =A0 =A0 =A0t_fact.time

table structure:

=A0 =A0 =A0 =A0Table "perf_reporting.abstract_radar_performance_f= act"
=A0 =A0 =A0 =A0 =A0 Column =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0 = =A0 =A0 =A0Type =A0 =A0 =A0 =A0 =A0 =A0 | Modifiers
----------------------------+-----------------------------+-----------
= count =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| bigint =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0| not null
total_ms =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 | bigint =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| not null<= br> time =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | timestamp without ti= me zone | not null
market_num =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 | not null
country_num =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not null
autosys= _num =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 | not null
provider_owner_zone_id =A0 =A0 | integer =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not null
provider_owner_customer_id | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 | not null
provider_id =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| integer =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not null
probe_type_num =A0 =A0 = =A0 =A0 =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not nul= l


with individual indexes on the everything from time to the bottom on th= e child tables

and


=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 Table "portal.providers"
=A0 =A0 =A0 =A0 =A0 Column =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0Ty= pe =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0 Modifiers =A0 =A0 =A0 =A0
-----= -----------------------+-----------------------------+---------------------= ---
btime =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| timestamp withou= t time zone | not null default now()
mtime =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| timestamp without time = zone | not null default now()
version =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0| integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not null default= 1
provider_id =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| integer =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 | not null
provider_owner_zone_id =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 | not null
provider_owner_customer_id | integer =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 | not null
provider_category_id =A0 =A0 =A0 | = integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | not null
name =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | character varying(255) =A0 =A0 =A0| = not null
display_name =A0 =A0 =A0 =A0 =A0 =A0 =A0 | character varying(255) =A0 =A0 = =A0| not null

with indexes on every column with name ending i= n '_id'

--001636c5b91e272460048e27174d--