Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8jGi-0004UF-Se for pgsql-sql@arkaria.postgresql.org; Wed, 18 Apr 2018 09:20:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f8jGh-0000Cf-F4 for pgsql-sql@arkaria.postgresql.org; Wed, 18 Apr 2018 09:20:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8jGh-0000CV-8M for pgsql-sql@lists.postgresql.org; Wed, 18 Apr 2018 09:20:47 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8jGe-0001s6-0q for pgsql-sql@postgresql.org; Wed, 18 Apr 2018 09:20:46 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1f8jGZ-0007m2-Va for pgsql-sql@postgresql.org; Wed, 18 Apr 2018 10:20:40 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: postgres list Subject: summary view design / performance Date: Wed, 18 Apr 2018 10:20:39 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201804181020.39745.gary.stainburn@ringways.co.uk> X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 123838 [Apr 18 2018] X-KLMS-AntiSpam-Version: 5.8.1.0 X-KLMS-AntiSpam-Envelope-From: gary.stainburn@ringways.co.uk X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Info: LuaCore: 128 128 d7aa645f57ce6a6ceb7dd78eb333137e5e6b654c, {msgid_created_by_recepient}, ringways.co.uk:7.1.1;eddie.ringways.co.uk:7.1.1;127.0.0.199:7.1.2;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1, DmarcAF: none X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, 2018/04/16 12:23:15 X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.721, bases: 2018/04/18 00:48:00 #8433893 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Score: -50.8 (--------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: Morning all, I'm after people's opinions redarding performance / system load in the following select / view. Also, if there is a better technique I'd love to hear it. I have the following table, which is an audit and message log for amendments to the service_jobs table. [...] Content analysis details: (-50.8 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.1 SCORE_RCPTS Adding score for each recipient -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE No description available. X-forward-disable: yes List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Morning all, I'm after people's opinions redarding performance / system load in the following select / view. Also, if there is a better technique I'd love to hear it. I have the following table, which is an audit and message log for amendments to the service_jobs table. Table "public.service_jobs_log" Column | Type | Modifiers ---------------+-----------------------------+------------------------------------------------------------------- sj_seq | integer | not null default nextval('service_jobs_log_sj_seq_seq'::regclass) sj_id | integer | not null sj_u_id | integer | not null sj_text | text | not null sj_timestamp | timestamp without time zone | default now() sjl_id | integer | not null default 10 sjl_answer_to | integer | Indexes: "service_jobs_log_pkey" PRIMARY KEY, btree (sj_seq) "service_jobs_log_sj_id_index" btree (sj_id) "service_jobs_log_sj_timestamp_index" btree (sj_timestamp) "service_jobs_log_sjl_id_index" btree (sjl_id) Foreign-key constraints: "service_jobs_log_sj_id_fkey" FOREIGN KEY (sj_id) REFERENCES service_jobs(sj_id) "service_jobs_log_sj_u_id_fkey" FOREIGN KEY (sj_u_id) REFERENCES users(u_id) "service_jobs_log_sjl_answer_to_fkey" FOREIGN KEY (sjl_answer_to) REFERENCES service_jobs_log(sj_seq) "service_jobs_log_sjl_id_fkey" FOREIGN KEY (sjl_id) REFERENCES service_jobs_log_types(sjl_id) goole=# select * from service_jobs_log_types order by sjl_id; sjl_id | sjl_desc | sjl_show_chat | sjl_colour --------+----------+---------------+------------ 10 | Activity | f | 20 | Comment | f | 30 | Question | f | 40 | Answer | f | (4 rows) The view I want is: j_id count comment_count highest_comment_seq question_count highest_question_seq answer_count highest_answer_seq I have two solutions. Firstly having multiple sub-selects select sjl.sj_id, sjl.count, coalesce(cc.count,0) as comment_count, cc.max as highest_comment_seq, coalesce(qc.count,0) as question_count, qc.max as highest_question_seq, coalesce(ac.count,0) as answer_count, ac.max as highest_answer_seq from (select sj_id, count(sj_id) from service_jobs_log group by sj_id) sjl left outer join (select sj_id, count(sj_id), max(sj_seq) from service_jobs_log where sjl_id=20 group by sj_id) cc on sjl.sj_id = cc.sj_id left outer join (select sj_id, count(sj_id), max(sj_seq) from service_jobs_log where sjl_id=30 group by sj_id) qc on sjl.sj_id = qc.sj_id left outer join (select sj_id, count(sj_id), max(sj_seq) from service_jobs_log where sjl_id=40 group by sj_id) ac on sjl.sj_id = ac.sj_id ; Secondly, having one query with lots of case statements select sj_id, count(sj_id), count(comments) as comment_count, max(comment_seq) as highest_comment_seq, count(questions) as question_count, max(question_seq) as highest_question_seq, count(answers) as answer_count, max(answer_seq) as highest_answer_seq from ( select sj_id, case when sjl_id = 20 then 1 else NULL end as comments, case when sjl_id = 20 then sj_seq else NULL end as comment_seq, case when sjl_id = 30 then 1 else NULL end as questions, case when sjl_id = 30 then sj_seq else NULL end as question_seq, case when sjl_id = 40 then 1 else NULL end as answers, case when sjl_id = 40 then sj_seq else NULL end as answer_seq from service_jobs_log) foo group by sj_id; In the production environment the view will be called with a list of required sj_id's, e.g. select * from service_job_log_summary where sj_id in (123,124,145..........)