pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vitaliy Garnashevich <vgarnashevich@gmail.com>
To: Adrian Klaver <adrian.klaver@aklaver.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Long running INSERT+SELECT query
Date: Thu, 26 Apr 2018 21:08:01 +0300
Message-ID: <f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com> (raw)
In-Reply-To: <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com>
References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com>
	<a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com>
	<ce1a27f4-6085-6be4-8b9e-08598fd7e5eb@gmail.com>
	<39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com>


> Without the query we are flying blind, so suggestions will have a ? 

Here is one such query:

     INSERT INTO cmdb_sp_usage_history
       (created_by, updated_by, created_on, updated_on, mod_count,
       summary_on, quarter, product, used_from, "user",
       keystrokes, minutes_in_use, times_started, avg_keystrokes, 
max_keystrokes, spkg_operational)
     SELECT
        2, 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 1,
        CURRENT_TIMESTAMP, quarter.id, "spv"."product", 
"usage"."used_from", "usage"."user",
        coalesce(sum("usage"."keystrokes"), 0),
        coalesce(sum("usage"."minutes_in_use"), 0),
        coalesce(sum("usage"."times_started"), 0),
        coalesce(avg("usage"."keystrokes"), 0),
        coalesce(max("usage"."keystrokes"), 0),
        bool_or("cmdb_ci"."operational")
     FROM
       "cmdb_program_daily_usage" "usage"
     LEFT OUTER JOIN
       "cmdb_program_instance" "p" ON "p"."id" = "usage"."program_instance"
     LEFT OUTER JOIN
       "cmdb_ci_spkg" "s" ON "s"."id" = "p"."spkg"
     LEFT OUTER JOIN
       "cmdb_ci" "cmdb_ci" ON "s"."id" = "cmdb_ci"."id"
     LEFT OUTER JOIN
       "cmdb_software_product_version" "spv" ON "spv"."id" = "s"."software"
     WHERE ("usage"."minutes_in_use" > 0)
       AND ((NOT ("s"."software" IS NULL))
            AND ((NOT ("s"."os" = TRUE))
                 OR ("s"."os" IS NULL)))
       AND ("usage"."usage_date" >= quarter.start_date)
       AND ("usage"."usage_date" < quarter.end_date)
     GROUP BY "spv"."product", "usage"."used_from", "usage"."user"
     HAVING (coalesce(sum("usage"."keystrokes"), 0) > 0) OR 
(coalesce(sum("usage"."minutes_in_use"), 0) > 0) OR 
(coalesce(sum("usage"."times_started"), 0) > 0)
     ORDER BY "spv"."product", "usage"."used_from", "usage"."user";

Regards,
Vitaliy





view thread (16+ messages)  latest in thread

Message-ID: <f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com>
Permalink:  ../f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com/
Also on:    postgresql.org/message-id/f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: vgarnashevich@gmail.com, adrian.klaver@aklaver.com
  Subject: Re: Long running INSERT+SELECT query
  In-Reply-To: <f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com>
  References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com> <ce1a27f4-6085-6be4-8b9e-08598fd7e5eb@gmail.com> <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> <f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox