Thread: Slow query when the select list is big

Slow query when the select list is big

From
"Sterpu Victor"
Date:
I have a big query that takes about 7 seconds to run(time sending the data to the client is not counted).
Postgres uses 100% of 1 CPU when solving this query. I tried to run the query on a HDD and on a SSD with no difference. HDD show about 10% usage while the query runs.
 
The query has a big "select" list and no "group by" clause. If I delete all selects except one the query runs in under a second(it doesn't matter what field remains selected).
It seems that the query is slow because of the long select, can I do something to make the query faster?
 
Thank you.
 

Re: Slow query when the select list is big

From
Rob Imig
Date:
Can you share the full query and output of EXPLAIN ? Not much data here yet.
On Mon, May 9, 2016 at 6:58 AM Sterpu Victor <victor@caido.ro> wrote:
I have a big query that takes about 7 seconds to run(time sending the data to the client is not counted).
Postgres uses 100% of 1 CPU when solving this query. I tried to run the query on a HDD and on a SSD with no difference. HDD show about 10% usage while the query runs.
 
The query has a big "select" list and no "group by" clause. If I delete all selects except one the query runs in under a second(it doesn't matter what field remains selected).
It seems that the query is slow because of the long select, can I do something to make the query faster?
 
Thank you.
 

Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
Yes but it is very big.
I don't understand why the select list is influencing the CPU usage.
I was expecting that only the join and where clauses would influence CPU.
 
"Limit  (cost=190115.90..190115.93 rows=1 width=6391)"
"  ->  WindowAgg  (cost=190115.90..190115.93 rows=1 width=6391)"
"        ->  Sort  (cost=190115.90..190115.90 rows=1 width=6391)"
"              Sort Key: j1031101.validfrom DESC"
"              ->  Nested Loop Left Join  (cost=100.22..190115.89 rows=1 width=6391)"
"                    Join Filter: (j1033704.id = j1031074.id_stari_spitalizari)"
"                    ->  Nested Loop  (cost=100.22..190114.84 rows=1 width=6359)"
"                          Join Filter: (j1033407.iddepartment = j1034477.id)"
"                          ->  Nested Loop  (cost=77.25..190091.74 rows=1 width=6363)"
"                                ->  Nested Loop Left Join  (cost=76.95..190091.37 rows=1 width=6359)"
"                                      Join Filter: (j1033360.validfrom > j1033359.validfrom)"
"                                      Filter: (j1033360.id IS NULL)"
"                                      ->  Nested Loop Left Join  (cost=76.66..190091.00 rows=1 width=6367)"
"                                            Join Filter: (j1033387.id = j1033359.iddepartment)"
"                                            ->  Nested Loop Left Join  (cost=76.66..190088.83 rows=1 width=6351)"
"                                                  Filter: (((date(j1031101.validfrom) >= '2016-05-01'::date) AND (date(j1031101.validfrom) <= '2016-05-31'::date)) OR ((j1033359.validto IS NOT NULL) AND (date(j1033359.validto) >= '2016-05-01'::date) AND (date(j1033359.validto) <= '2016-05-31'::date)) OR ((j1033359.validto IS NULL) AND (date(j1031101.validfrom) <= '2016-05-01'::date)))"
"                                                  ->  Nested Loop Left Join  (cost=76.37..190088.44 rows=1 width=6331)"
"                                                        Join Filter: (j1033358.id = j1031074.id_drg_tip_cetatenie)"
"                                                        ->  Nested Loop Left Join  (cost=76.37..190087.35 rows=1 width=6299)"
"                                                              Join Filter: (j1033304.id = j1031074.id_citizenship)"
"                                                              ->  Nested Loop Left Join  (cost=76.37..190085.36 rows=1 width=5783)"
"                                                                    Join Filter: (j1033299.id = j1031074.id_hospital_release_statuses)"
"                                                                    ->  Nested Loop Left Join  (cost=76.37..190081.60 rows=1 width=5676)"
"                                                                          Join Filter: (j1033295.id = j1031074.idphysician_surgeon)"
"                                                                          ->  Nested Loop Left Join  (cost=76.37..190077.63 rows=1 width=5669)"
"                                                                                Join Filter: (j1031737.id = j1031074.id_focg_stare_externare)"
"                                                                                ->  Nested Loop Left Join  (cost=76.37..190076.52 rows=1 width=5637)"
"                                                                                      Join Filter: (j1031736.id = j1031074.id_focg_tip_externare)"
"                                                                                      ->  Nested Loop Left Join  (cost=76.37..190075.43 rows=1 width=5605)"
"                                                                                            Join Filter: (j1031102.validfrom < j1031101.validfrom)"
"                                                                                            Filter: (j1031102.id IS NULL)"
"                                                                                            ->  Hash Left Join  (cost=76.08..155225.61 rows=94433 width=5605)"
"                                                                                                  Hash Cond: (j1031101.iddepartment = j1033386.id)"
"                                                                                                  ->  Nested Loop Left Join  (cost=73.91..153924.99 rows=94433 width=5589)"
"                                                                                                        ->  Hash Left Join  (cost=73.61..123840.99 rows=82075 width=5577)"
"                                                                                                              Hash Cond: (j1031074.id_exceptie_bilet_internare = j1031100.id)"
"                                                                                                              ->  Hash Left Join  (cost=72.48..123531.99 rows=82075 width=5545)"
"                                                                                                                    Hash Cond: (j1031074.id_focg_criterii_urgenta = j1031099.id)"
"                                                                                                                    ->  Hash Left Join  (cost=71.14..122977.79 rows=82075 width=5513)"
"                                                                                                                          Hash Cond: (j1031074.id_focg_contract_modes = j1031098.id)"
"                                                                                                                          ->  Hash Left Join  (cost=70.07..122285.19 rows=82075 width=5481)"
"                                                                                                                                Hash Cond: (j1031074.id_icd10 = j1031097.id)"
"                                                                                                                                ->  Hash Left Join  (cost=27.50..121681.03 rows=82075 width=5442)"
"                                                                                                                                      Hash Cond: (j1031074.id_diseasecategory = j1031096.id)"
"                                                                                                                                      ->  Hash Left Join  (cost=25.60..121308.32 rows=82075 width=5410)"
"                                                                                                                                            Hash Cond: (j1031074.id_focg_sursa_internare = j1031095.id)"
"                                                                                                                                            ->  Nested Loop Left Join  (cost=24.51..120178.73 rows=82075 width=5378)"
"                                                                                                                                                  ->  Nested Loop Left Join  (cost=24.21..88054.73 rows=82075 width=5364)"
"                                                                                                                                                        ->  Hash Left Join  (cost=23.92..40002.73 rows=82075 width=5360)"
"                                                                                                                                                              Hash Cond: (j1031074.id_formulare_europene = j1031092.id)"
"                                                                                                                                                              ->  Hash Left Join  (cost=22.79..39693.80 rows=82075 width=5328)"
"                                                                                                                                                                    Hash Cond: (j1031074.id_focg_internat_prin = j1031091.id)"
"                                                                                                                                                                    ->  Hash Left Join  (cost=21.70..39145.44 rows=82075 width=5296)"
"                                                                                                                                                                          Hash Cond: (j1031074.id_focg_situatii_speciale = j1031090.id)"
"                                                                                                                                                                          ->  Hash Left Join  (cost=20.61..38834.27 rows=82075 width=5264)"
"                                                                                                                                                                                Hash Cond: (j1031074.id_education_level = j1031089.id)"
"                                                                                                                                                                                ->  Hash Left Join  (cost=19.38..38318.84 rows=82075 width=4748)"
"                                                                                                                                                                                      Hash Cond: (j1031074.idproffesion = j1031088.id)"
"                                                                                                                                                                                      ->  Hash Left Join  (cost=18.20..37659.97 rows=82075 width=4232)"
"                                                                                                                                                                                            Hash Cond: (j1031074.idphysician_madeby = j1031087.id)"
"                                                                                                                                                                                            ->  Hash Left Join  (cost=14.23..37016.58 rows=82075 width=4225)"
"                                                                                                                                                                                                  Hash Cond: (j1031074.idphysiciancurrent = j1031084.id)"
"                                                                                                                                                                                                  ->  Hash Left Join  (cost=10.26..36297.22 rows=82075 width=4218)"
"                                                                                                                                                                                                        Hash Cond: (j1031074.id_focg_criteriu_internare = j1031083.id)"
"                                                                                                                                                                                                        ->  Hash Left Join  (cost=8.95..35533.59 rows=82075 width=4186)"
"                                                                                                                                                                                                              Hash Cond: (j1031074.id_focg_tip_internare = j1031082.id)"
"                                                                                                                                                                                                              ->  Hash Left Join  (cost=7.65..34768.02 rows=82075 width=4154)"
"                                                                                                                                                                                                                    Hash Cond: (j1031074.id_categorie_asigurat = j1031081.id)"
"                                                                                                                                                                                                                    ->  Hash Left Join  (cost=5.98..34151.50 rows=82075 width=4122)"
"                                                                                                                                                                                                                          Hash Cond: (j1031074.id_org_unit = j1031080.id)"
"                                                                                                                                                                                                                          ->  Hash Left Join  (cost=3.88..33323.12 rows=82075 width=4090)"
"                                                                                                                                                                                                                                Hash Cond: (j1031074.idensuredstatus = j1031079.id)"
"                                                                                                                                                                                                                                ->  Hash Left Join  (cost=2.76..32508.58 rows=82075 width=3574)"
"                                                                                                                                                                                                                                      Hash Cond: (j1031074.idensuredstatustype = j1031078.id)"
"                                                                                                                                                                                                                                      ->  Hash Left Join  (cost=1.68..31625.10 rows=82075 width=3058)"
"                                                                                                                                                                                                                                            Hash Cond: (j1031075.idsex = j1031076.id)"
"                                                                                                                                                                                                                                            ->  Merge Left Join  (cost=0.58..30495.48 rows=82075 width=2546)"
"                                                                                                                                                                                                                                                  Merge Cond: (j1031074.idpatient = j1031075.id)"
"                                                                                                                                                                                                                                                  ->  Index Scan using fosz_simple_index33 on focg j1031074  (cost=0.29..23019.37 rows=82075 width=2517)"
"                                                                                                                                                                                                                                                  ->  Index Scan using person_pkey on person j1031075  (cost=0.29..6237.94 rows=85376 width=37)"
"                                                                                                                                                                                                                                            ->  Hash  (cost=1.04..1.04 rows=4 width=520)"
"                                                                                                                                                                                                                                                  ->  Seq Scan on sex j1031076  (cost=0.00..1.04 rows=4 width=520)"
"                                                                                                                                                                                                                                      ->  Hash  (cost=1.04..1.04 rows=4 width=520)"
"                                                                                                                                                                                                                                            ->  Seq Scan on ensuredstatustype j1031078  (cost=0.00..1.04 rows=4 width=520)"
"                                                                                                                                                                                                                                ->  Hash  (cost=1.05..1.05 rows=5 width=520)"
"                                                                                                                                                                                                                                      ->  Seq Scan on ensuredstatus j1031079  (cost=0.00..1.05 rows=5 width=520)"
"                                                                                                                                                                                                                          ->  Hash  (cost=1.49..1.49 rows=49 width=36)"
"                                                                                                                                                                                                                                ->  Seq Scan on org_units j1031080  (cost=0.00..1.49 rows=49 width=36)"
"                                                                                                                                                                                                                    ->  Hash  (cost=1.30..1.30 rows=30 width=36)"
"                                                                                                                                                                                                                          ->  Seq Scan on personstate j1031081  (cost=0.00..1.30 rows=30 width=36)"
"                                                                                                                                                                                                              ->  Hash  (cost=1.13..1.13 rows=13 width=36)"
"                                                                                                                                                                                                                    ->  Seq Scan on focginterntype j1031082  (cost=0.00..1.13 rows=13 width=36)"
"                                                                                                                                                                                                        ->  Hash  (cost=1.14..1.14 rows=14 width=36)"
"                                                                                                                                                                                                              ->  Seq Scan on focginterncrit j1031083  (cost=0.00..1.14 rows=14 width=36)"
"                                                                                                                                                                                                  ->  Hash  (cost=2.32..2.32 rows=132 width=15)"
"                                                                                                                                                                                                        ->  Seq Scan on physicians j1031084  (cost=0.00..2.32 rows=132 width=15)"
"                                                                                                                                                                                            ->  Hash  (cost=2.32..2.32 rows=132 width=11)"
"                                                                                                                                                                                                  ->  Seq Scan on physicians j1031087  (cost=0.00..2.32 rows=132 width=11)"
"                                                                                                                                                                                      ->  Hash  (cost=1.08..1.08 rows=8 width=520)"
"                                                                                                                                                                                            ->  Seq Scan on proffession j1031088  (cost=0.00..1.08 rows=8 width=520)"
"                                                                                                                                                                                ->  Hash  (cost=1.10..1.10 rows=10 width=520)"
"                                                                                                                                                                                      ->  Seq Scan on educationlevel j1031089  (cost=0.00..1.10 rows=10 width=520)"
"                                                                                                                                                                          ->  Hash  (cost=1.04..1.04 rows=4 width=36)"
"                                                                                                                                                                                ->  Seq Scan on focg_situatii_speciale j1031090  (cost=0.00..1.04 rows=4 width=36)"
"                                                                                                                                                                    ->  Hash  (cost=1.04..1.04 rows=4 width=36)"
"                                                                                                                                                                          ->  Seq Scan on focg_internat_prin j1031091  (cost=0.00..1.04 rows=4 width=36)"
"                                                                                                                                                              ->  Hash  (cost=1.06..1.06 rows=6 width=36)"
"                                                                                                                                                                    ->  Seq Scan on formulare_europene j1031092  (cost=0.00..1.06 rows=6 width=36)"
"                                                                                                                                                        ->  Index Scan using fosz_pkey on focg j1031093  (cost=0.29..0.58 rows=1 width=8)"
"                                                                                                                                                              Index Cond: (id = j1031074.id_focg)"
"                                                                                                                                                  ->  Index Scan using person_pkey on person j1031094  (cost=0.29..0.38 rows=1 width=18)"
"                                                                                                                                                        Index Cond: (id = j1031074.id_cnp_mama)"
"                                                                                                                                            ->  Hash  (cost=1.04..1.04 rows=4 width=36)"
"                                                                                                                                                  ->  Seq Scan on focg_sursa_internare j1031095  (cost=0.00..1.04 rows=4 width=36)"
"                                                                                                                                      ->  Hash  (cost=1.40..1.40 rows=40 width=36)"
"                                                                                                                                            ->  Seq Scan on diseasecategory j1031096  (cost=0.00..1.40 rows=40 width=36)"
"                                                                                                                                ->  Hash  (cost=26.70..26.70 rows=1270 width=43)"
"                                                                                                                                      ->  Seq Scan on icd10 j1031097  (cost=0.00..26.70 rows=1270 width=43)"
"                                                                                                                          ->  Hash  (cost=1.03..1.03 rows=3 width=36)"
"                                                                                                                                ->  Seq Scan on focg_contract_modes j1031098  (cost=0.00..1.03 rows=3 width=36)"
"                                                                                                                    ->  Hash  (cost=1.15..1.15 rows=15 width=36)"
"                                                                                                                          ->  Seq Scan on focg_criterii_urgenta j1031099  (cost=0.00..1.15 rows=15 width=36)"
"                                                                                                              ->  Hash  (cost=1.06..1.06 rows=6 width=36)"
"                                                                                                                    ->  Seq Scan on exceptie_bilet_internare j1031100  (cost=0.00..1.06 rows=6 width=36)"
"                                                                                                        ->  Index Scan using focgdepartment_idfocg on focgdepartment j1031101  (cost=0.29..0.36 rows=1 width=16)"
"                                                                                                              Index Cond: (idfocg = j1031074.id)"
"                                                                                                  ->  Hash  (cost=1.52..1.52 rows=52 width=24)"
"                                                                                                        ->  Seq Scan on department j1033386  (cost=0.00..1.52 rows=52 width=24)"
"                                                                                            ->  Index Scan using focgdepartment_idfocg on focgdepartment j1031102  (cost=0.29..0.36 rows=1 width=16)"
"                                                                                                  Index Cond: (idfocg = j1031074.id)"
"                                                                                      ->  Seq Scan on focg_tip_externare j1031736  (cost=0.00..1.04 rows=4 width=36)"
"                                                                                ->  Seq Scan on focg_stare_externare j1031737  (cost=0.00..1.05 rows=5 width=36)"
"                                                                          ->  Seq Scan on physicians j1033295  (cost=0.00..2.32 rows=132 width=11)"
"                                                                    ->  Seq Scan on hospital_release_statuses j1033299  (cost=0.00..2.78 rows=78 width=111)"
"                                                              ->  Seq Scan on citizenship j1033304  (cost=0.00..1.44 rows=44 width=520)"
"                                                        ->  Seq Scan on drg_tip_cetatenie j1033358  (cost=0.00..1.04 rows=4 width=36)"
"                                                  ->  Index Scan using focgdepartment_idfocg on focgdepartment j1033359  (cost=0.29..0.36 rows=1 width=24)"
"                                                        Index Cond: (idfocg = j1031074.id)"
"                                            ->  Seq Scan on department j1033387  (cost=0.00..1.52 rows=52 width=24)"
"                                      ->  Index Scan using focgdepartment_idfocg on focgdepartment j1033360  (cost=0.29..0.36 rows=1 width=16)"
"                                            Index Cond: (idfocg = j1031074.id)"
"                                ->  Index Scan using focgdepartment_idfocg on focgdepartment j1033407  (cost=0.29..0.36 rows=1 width=8)"
"                                      Index Cond: (idfocg = j1031074.id)"
"                          ->  HashAggregate  (cost=22.98..23.02 rows=4 width=4)"
"                                Group Key: j1034477.id"
"                                ->  Nested Loop  (cost=4.73..22.97 rows=4 width=4)"
"                                      ->  Nested Loop  (cost=4.59..22.28 rows=4 width=4)"
"                                            ->  Index Scan using personnel_uni_user on personnel j1034479  (cost=0.28..8.29 rows=1 width=4)"
"                                                  Index Cond: (id_connected_user = 1)"
"                                            ->  Bitmap Heap Scan on personnel_department j1034478  (cost=4.31..13.95 rows=4 width=8)"
"                                                  Recheck Cond: (id_personnel = j1034479.id)"
"                                                  ->  Bitmap Index Scan on personnel_department_id_personnel  (cost=0.00..4.31 rows=4 width=0)"
"                                                        Index Cond: (id_personnel = j1034479.id)"
"                                      ->  Index Only Scan using department_pkey on department j1034477  (cost=0.14..0.16 rows=1 width=4)"
"                                            Index Cond: (id = j1034478.id_department)"
"                    ->  Seq Scan on stari_spitalizari j1033704  (cost=0.00..1.02 rows=2 width=36)"
 
------ Original Message ------
From: "Rob Imig" <rimig88@gmail.com>
To: "Sterpu Victor" <victor@caido.ro>; "PostgreSQL General" <pgsql-general@postgresql.org>
Sent: 9/5/2016 9:03:10 AM
Subject: Re: [GENERAL] Slow query when the select list is big
 
Can you share the full query and output of EXPLAIN ? Not much data here yet.
On Mon, May 9, 2016 at 6:58 AM Sterpu Victor <victor@caido.ro> wrote:
I have a big query that takes about 7 seconds to run(time sending the data to the client is not counted).
Postgres uses 100% of 1 CPU when solving this query. I tried to run the query on a HDD and on a SSD with no difference. HDD show about 10% usage while the query runs.
 
The query has a big "select" list and no "group by" clause. If I delete all selects except one the query runs in under a second(it doesn't matter what field remains selected).
It seems that the query is slow because of the long select, can I do something to make the query faster?
 
Thank you.
 

Re: Slow query when the select list is big

From
John R Pierce
Date:
On 5/8/2016 11:09 PM, Sterpu Victor wrote:
Yes but it is very big.
I don't understand why the select list is influencing the CPU usage.
I was expecting that only the join and where clauses would influence CPU.

what was the query that generated that really complicated execution plan?  it sure looks like a really complicated query with lots of joins and sorts etc etc..



-- 
john r pierce, recycling bits in santa cruz

Re: Slow query when the select list is big

From
"David G. Johnston"
Date:
On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
Yes but it is very big.
I don't understand why the select list is influencing the CPU usage.
I was expecting that only the join and where clauses would influence CPU.


PostgreSQL is smart enough to optimize away stuff that it knows doesn't impact the final query result.

David J.
 

Re: Slow query when the select list is big

From
David Rowley
Date:
On 9 May 2016 at 18:46, David G. Johnston <david.g.johnston@gmail.com> wrote:
> On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
>>
>> Yes but it is very big.
>> I don't understand why the select list is influencing the CPU usage.
>> I was expecting that only the join and where clauses would influence CPU.
>>
>
> PostgreSQL is smart enough to optimize away stuff that it knows doesn't
> impact the final query result.

To be more accurate with what David is saying, PostgreSQL will remove
unused LEFT JOINed relations where the left joined relation can be
proved to not duplicate rows from the right hand side. It would just
be a matter of comparing the EXPLAINs from the query with all the
SELECT items to the one with the single SELECT item to prove that this
is what's happening.

Please also note that this only occurs with LEFT JOINs

It would also be quite helpful for people if you were to include a
copy of the query. It's impossible to reverse engineer what that is
from this EXPLAIN output. I see that your using a windowing function
and performing a LIMIT 1, there may be ways to improve that just by
selecting the single highest j1031101.validfrom row and performing the
joins to the other table on that single row, but that will depend on
which windowing function you're using as the function may require the
other rows in the window frame to calculate the correct result.

--
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services


Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
I solved the problem patialy by swithing the order of a join.
I tested on a slower server to see better the difference.
After moving a single join the query runs in 4.1 seconds insted 6.4
seconds.
I pasted the optimized query down. When I move the join J1033704 at the
end(last join) the time is increased to 6.4 seconds.

SELECT
row_number() OVER (ORDER BY J1031101.validfrom DESC)  AS  nrcrt ,
CASE WHEN(CHAR_LENGTH(J1031074.signature) > 10)  THEN(1)  ELSE(0) END
AS  semnat_internare ,
CASE WHEN(CHAR_LENGTH(J1031074.signature_externare) > 10)  THEN(1)
ELSE(0) END  AS  semnat_externare ,
J1031076.name  AS  sex ,
J1031074.id  AS  id ,
J1031074.siui_appid  AS  siui_appid ,
J1031074.data_adeverinta  AS  data_adeverinta ,
J1031074.is_paliativ  AS  text_paliativ ,
J1031074.cardno  AS  cardno ,
J1031074.cardno_externare  AS  cardno_externare ,
J1031074.sign_date  AS  sign_date ,
J1031074.sign_date_externare  AS  sign_date_externare ,
J1031074.unsigned_string  AS  unsigned_string ,
J1031074.unsigned_string_externare  AS  unsigned_string_externare ,
J1031074.signhash  AS  signhash ,
J1031074.signhash_externare  AS  signhash_externare ,
J1031074.signature  AS  signature ,
J1031074.signature_externare  AS  signature_externare ,
J1031074.send_xml  AS  send_xml ,
J1031074.send_xml_externare  AS  send_xml_externare ,
J1031074.received_xml  AS  received_xml ,
J1031074.received_xml_externare  AS  received_xml_externare ,
J1031074.error  AS  error ,
J1031074.error_externare  AS  error_externare ,
J1031074.validat  AS  validat ,
J1031074.validat_externare  AS  validat_externare ,
J1031074.online  AS  online ,
J1031074.online_externare  AS  online_externare ,
J1031074.serie_bilet_internare  AS  text_serie_bilet_internare ,
J1031074.nr_bilet_internare  AS  text_numar_bilet_internare ,
J1031074.idpatient  AS  _popup_cnp_pacient ,
J1031075.cnp  AS  popup_cnp_pacient ,
J1031075.name  AS  text_nume_pacient ,
J1031075.surname  AS  text_prenume_pacient ,
J1031074.nrfo  AS  text_numar_fosz ,
J1031074.greutate_nastere  AS  text_greutate_nastere ,
J1031078.value  AS  popup_tip_asigurare ,
J1031074.idensuredstatustype  AS  _popup_tip_asigurare ,
J1031079.value  AS  popup_statut_asigurat ,
J1031074.idensuredstatus  AS  _popup_statut_asigurat ,
J1031080.code  AS  popup_cas_asigurat ,
J1031074.id_org_unit  AS  _popup_cas_asigurat ,
J1031081.code  AS  popup_categorie_asigurare ,
J1031074.id_categorie_asigurat  AS  _popup_categorie_asigurare ,
J1031082.name  AS  popup_tip_internare ,
J1031074.id_focg_tip_internare  AS  _popup_tip_internare ,
J1031083.name  AS  popup_criteriu_internare ,
J1031074.id_focg_criteriu_internare  AS  _popup_criteriu_internare ,
J1031084.stencil_no  AS  popup_medic_curant ,
J1031084.id  AS  _popup_medic_curant ,
J1031089.value  AS  popup_nivel_instruire ,
J1031074.id_education_level  AS  _popup_nivel_instruire ,
J1031074.greutate  AS  text_greutate_internare ,
J1031090.nume  AS  popup_situatii_speciale ,
J1031074.id_focg_situatii_speciale  AS  _popup_situatii_speciale ,
J1031091.nume  AS  popup_internat_prin ,
J1031091.id  AS  _popup_internat_prin ,
J1031092.nume  AS  popup_formulare_europene ,
J1031074.id_formulare_europene  AS  _popup_formulare_europene ,
J1031074.id_cnp_mama  AS  _popup_cnp_mama ,
J1031094.cnp  AS  popup_cnp_mama ,
J1031093.nrfo  AS  popup_fo_mama ,
J1031074.id_focg  AS  _popup_fo_mama ,
J1031074.nr_card_euro  AS  text_nr_card_european ,
J1031074.nr_pasaport  AS  text_nr_pasaport ,
J1031074.nr_card_national  AS  text_nr_card_national ,
J1031088.id  AS  _popup_ocupatia ,
J1031088.name  AS  popup_ocupatia ,
J1031074.export_drg  AS  export_drg ,
J1031074.drgcaseid  AS  drgcaseid ,
J1031074.export_ecosoft  AS  export_ecosoft ,
J1031074.mesaj_drg  AS  mesaj_drg ,
J1031074.uid  AS  uid ,
J1031074.mesaj_ecosoft  AS  mesaj_ecosoft ,
J1031074.id_address_domiciliu  AS  text_id_address_domiciliu ,
J1031074.id_address_domiciliu  AS  _text_id_address_domiciliu ,
J1031074.id_address_resedinta  AS  _text_id_address_resedinta ,
J1031074.id_address_resedinta  AS  text_id_address_resedinta ,
'0'  AS  marcator ,
J1031095.id  AS  _popup_sursa_internare ,
J1031095.denumire  AS  popup_sursa_internare ,
J1031096.id  AS  _popup_diseasecategory ,
J1031096.code  AS  popup_diseasecategory ,
J1031097.id  AS  _popup_diagnostic_internare_icd10 ,
J1031097.name  AS  popup_diagnostic_internare_icd10 ,
J1031098.id  AS  _popup_mod_contract ,
J1031098.description  AS  popup_mod_contract ,
J1031099.id  AS  _popup_criteriu_urgenta ,
J1031099.name  AS  popup_criteriu_urgenta ,
J1031100.id  AS  _popup_exceptie_bi ,
J1031100.code  AS  popup_exceptie_bi ,
J1031074.scrisoare_medicala_parafa  AS  text_parafa_scrisoare_medicala ,
J1031074.scrisoare_medicala_contract  AS
text_contract_scrisoare_medicala ,
J1031074.scrisoare_medicala_tip_contract  AS
text_tip_contract_scrisoare_medicala ,
J1031074.export_siui  AS  export_siui ,
J1031074.mesaj_siui  AS  mesaj_siui ,
J1031087.id  AS  _popup_intocmit ,
J1031087.stencil_no  AS  popup_intocmit ,
J1031074.diagnostic_trimitere_text  AS  text_diagnostic_trimitere ,
J1031074.greutate_externare  AS  text_greutate_externare ,
J1031074.data_decesului  AS  text_data_deces ,
J1031736.id  AS  _popup_tip_externare ,
J1031736.descriere  AS  popup_tip_externare ,
J1031737.id  AS  _popup_stare_externare ,
J1031737.descriere  AS  popup_stare_externare ,
J1033295.id  AS  _popup_chirurg ,
J1033295.stencil_no  AS  popup_chirurg ,
J1033299.id  AS  _text_caz_externare ,
J1033299.description  AS  text_caz_externare ,
J1031074.text_ore_ventilatie  AS  text_ore_ventilatie ,
J1031074.drg_cod_grupa  AS  text_drg_cod_grupa ,
J1031074.drg_relative_value  AS  text_drg_relative_value ,
J1031074.data_2500g  AS  text_data_2500g ,
J1031074.prematur_gr_i  AS  text_prematur_gr_i ,
J1033304.id  AS  _popup_cetatenie ,
J1033304.description  AS  popup_cetatenie ,
J1033358.id  AS  _popup_tip_cetatenie ,
J1033358.name  AS  popup_tip_cetatenie ,
J1031074.reinternat_transfer  AS  text_reinternat_transfer ,
J1031074.aviz_comisie  AS  text_aviz_comisie_reinternare ,
J1031074.criteriu_urgenta1  AS  criteriu_urgenta1 ,
J1031074.criteriu_urgenta2  AS  criteriu_urgenta2 ,
J1031074.criteriu_urgenta3  AS  criteriu_urgenta3 ,
J1031074.criteriu_urgenta4  AS  criteriu_urgenta4 ,
J1031074.criteriu_urgenta5  AS  criteriu_urgenta5 ,
J1031074.criteriu_urgenta6  AS  criteriu_urgenta6 ,
J1031074.criteriu_urgenta7  AS  criteriu_urgenta7 ,
J1031074.criteriu_urgenta8  AS  criteriu_urgenta8 ,
J1031074.criteriu_urgenta9a  AS  criteriu_urgenta9a ,
J1031074.criteriu_urgenta9b  AS  criteriu_urgenta9b ,
J1031074.criteriu_urgenta10  AS  criteriu_urgenta10 ,
J1031074.criteriu_urgenta11  AS  criteriu_urgenta11 ,
J1031074.criteriu_urgenta12  AS  criteriu_urgenta12 ,
J1031074.criteriu_urgenta13  AS  criteriu_urgenta13 ,
J1031074.infectie_nosocomiala  AS  infectie_nosocomiala ,
J1031074.cu_programare  AS  cu_programare ,
J1031074.stare_critica_internare  AS  stare_critica_internare ,
J1033704.id  AS  _id_stari_spitalizari ,
J1033704.nume  AS  id_stari_spitalizari
    FROM focg AS J1031074
   LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient)
    LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex)
    LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id =
J1031074.id_focg_serials)
    LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id =
J1031074.idensuredstatustype)
    LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id =
J1031074.idensuredstatus)
    LEFT JOIN org_units AS J1031080 ON ( J1031080.id =
J1031074.id_org_unit)
    LEFT JOIN personstate AS J1031081 ON ( J1031081.id =
J1031074.id_categorie_asigurat)
    LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id =
J1031074.id_focg_tip_internare)
    LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id =
J1031074.id_focg_criteriu_internare)
    LEFT JOIN physicians AS J1031084 ON ( J1031084.id =
J1031074.idphysiciancurrent)
    LEFT JOIN personnel AS J1031085 ON ( J1031085.id =
J1031084.id_personnel)
    LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson)
    LEFT JOIN physicians AS J1031087 ON ( J1031087.id =
J1031074.idphysician_madeby)
    LEFT JOIN proffession AS J1031088 ON ( J1031088.id =
J1031074.idproffesion)
    LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id =
J1031074.id_education_level)
    LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id =
J1031074.id_focg_situatii_speciale)
    LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id =
J1031074.id_focg_internat_prin)
    LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id =
J1031074.id_formulare_europene)
    LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg)
    LEFT JOIN person AS J1031094 ON ( J1031094.id = J1031074.id_cnp_mama)
    LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id =
J1031074.id_focg_sursa_internare)
    LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id =
J1031074.id_diseasecategory)
    LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10)
    LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id =
J1031074.id_focg_contract_modes)
    LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id =
J1031074.id_focg_criterii_urgenta)
    LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id =
J1031074.id_exceptie_bilet_internare)
    LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id =
J1031074.id_focg_tip_externare)
    LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id =
J1031074.id_focg_stare_externare)
    LEFT JOIN physicians AS J1033295 ON ( J1033295.id =
J1031074.idphysician_surgeon)
    LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id =
J1031074.id_hospital_release_statuses)
    LEFT JOIN citizenship AS J1033304 ON ( J1033304.id =
J1031074.id_citizenship)
    LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id =
J1031074.id_drg_tip_cetatenie)
    LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id =
J1031074.id_stari_spitalizari)
    LEFT JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg =
J1031074.id)
    LEFT JOIN department AS J1033386 ON ( J1033386.id =
J1031101.iddepartment)
    LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg =
J1031074.id AND  J1031102.validfrom < J1031101.validfrom)
    LEFT JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg =
J1031074.id)
    LEFT JOIN department AS J1033387 ON ( J1033387.id =
J1033359.iddepartment)
    LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg =
J1031074.id AND  J1033360.validfrom > J1033359.validfrom)
     JOIN focgdepartment AS J1033407 ON ( J1033407.idfocg = J1031074.id
AND  J1033407.iddepartment IN (/*Executing QueryID 476590*/SELECT
DISTINCT(J1034477.id)  AS  ret FROM department AS J1034477  JOIN
personnel_department AS J1034478 ON ( J1034478.id_department =
J1034477.id)
   JOIN personnel AS J1034479 ON ( J1034479.id = J1034478.id_personnel
AND  J1034479.id_connected_user = '1')))

  WHERE   J1031102.id IS NULL   AND    J1033360.id IS NULL   AND    (
(   DATE(J1031101.validfrom)>= DATE('2016-05-01')   AND
DATE(J1031101.validfrom)<= DATE('2016-05-31 00:00:00.0')   )    OR    (
   J1033359.validto IS NOT NULL   AND   DATE(J1033359.validto)>=
DATE('2016-05-01')   AND   DATE(J1033359.validto)<= DATE('2016-05-31
00:00:00.0')   )    OR    (    J1033359.validto IS NULL   AND
DATE(J1031101.validfrom)<= DATE('2016-05-01')   )    )

    ORDER BY J1031101.validfrom DESC
  LIMIT  20 OFFSET 0

------ Original Message ------
From: "David Rowley" <david.rowley@2ndquadrant.com>
To: "Sterpu Victor" <victor@caido.ro>
Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
<pgsql-general@postgresql.org>; "David G. Johnston"
<david.g.johnston@gmail.com>
Sent: 9/5/2016 10:04:54 AM
Subject: Re: [GENERAL] Slow query when the select list is big

>On 9 May 2016 at 18:46, David G. Johnston <david.g.johnston@gmail.com>
>wrote:
>>  On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
>>>
>>>  Yes but it is very big.
>>>  I don't understand why the select list is influencing the CPU usage.
>>>  I was expecting that only the join and where clauses would influence
>>>CPU.
>>>
>>
>>  PostgreSQL is smart enough to optimize away stuff that it knows
>>doesn't
>>  impact the final query result.
>
>To be more accurate with what David is saying, PostgreSQL will remove
>unused LEFT JOINed relations where the left joined relation can be
>proved to not duplicate rows from the right hand side. It would just
>be a matter of comparing the EXPLAINs from the query with all the
>SELECT items to the one with the single SELECT item to prove that this
>is what's happening.
>
>Please also note that this only occurs with LEFT JOINs
>
>It would also be quite helpful for people if you were to include a
>copy of the query. It's impossible to reverse engineer what that is
>from this EXPLAIN output. I see that your using a windowing function
>and performing a LIMIT 1, there may be ways to improve that just by
>selecting the single highest j1031101.validfrom row and performing the
>joins to the other table on that single row, but that will depend on
>which windowing function you're using as the function may require the
>other rows in the window frame to calculate the correct result.
>
>--
>  David Rowley http://www.2ndQuadrant.com/
>  PostgreSQL Development, 24x7 Support, Training & Services



Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
I went to 2.4 seconds by joining first the tables that produce many
rows.

SELECT
row_number() OVER (ORDER BY J1031101.validfrom DESC)  AS  nrcrt ,
J1033386.name  AS  sectie_internare ,
J1033387.name  AS  sectie_externare ,
TO_CHAR(J1031101.validfrom , 'YYYY-MM-DD HH24:MI')  AS  validfrom ,
TO_CHAR(J1033359.validto , 'YYYY-MM-DD HH24:MI')  AS  validto ,
CASE WHEN(CHAR_LENGTH(J1031074.signature) > 10)  THEN(1)  ELSE(0) END
AS  semnat_internare ,
CASE WHEN(CHAR_LENGTH(J1031074.signature_externare) > 10)  THEN(1)
ELSE(0) END  AS  semnat_externare ,
J1031076.name  AS  sex ,
J1031074.id  AS  id ,
J1031074.siui_appid  AS  siui_appid ,
J1031074.data_adeverinta  AS  data_adeverinta ,
J1031074.is_paliativ  AS  text_paliativ ,
J1031074.cardno  AS  cardno ,
J1031074.cardno_externare  AS  cardno_externare ,
J1031074.sign_date  AS  sign_date ,
J1031074.sign_date_externare  AS  sign_date_externare ,
J1031074.unsigned_string  AS  unsigned_string ,
J1031074.unsigned_string_externare  AS  unsigned_string_externare ,
J1031074.signhash  AS  signhash ,
J1031074.signhash_externare  AS  signhash_externare ,
J1031074.signature  AS  signature ,
J1031074.signature_externare  AS  signature_externare ,
J1031074.send_xml  AS  send_xml ,
J1031074.send_xml_externare  AS  send_xml_externare ,
J1031074.received_xml  AS  received_xml ,
J1031074.received_xml_externare  AS  received_xml_externare ,
J1031074.error  AS  error ,
J1031074.error_externare  AS  error_externare ,
J1031074.validat  AS  validat ,
J1031074.validat_externare  AS  validat_externare ,
J1031074.online  AS  online ,
J1031074.online_externare  AS  online_externare ,
J1031074.serie_bilet_internare  AS  text_serie_bilet_internare ,
J1031074.nr_bilet_internare  AS  text_numar_bilet_internare ,
J1031074.idpatient  AS  _popup_cnp_pacient ,
J1031075.cnp  AS  popup_cnp_pacient ,
J1031075.name  AS  text_nume_pacient ,
J1031075.surname  AS  text_prenume_pacient ,
J1031074.nrfo  AS  text_numar_fosz ,
J1031074.greutate_nastere  AS  text_greutate_nastere ,
J1031078.value  AS  popup_tip_asigurare ,
J1031074.idensuredstatustype  AS  _popup_tip_asigurare ,
J1031079.value  AS  popup_statut_asigurat ,
J1031074.idensuredstatus  AS  _popup_statut_asigurat ,
J1031080.code  AS  popup_cas_asigurat ,
J1031074.id_org_unit  AS  _popup_cas_asigurat ,
J1031081.code  AS  popup_categorie_asigurare ,
J1031074.id_categorie_asigurat  AS  _popup_categorie_asigurare ,
J1031082.name  AS  popup_tip_internare ,
J1031074.id_focg_tip_internare  AS  _popup_tip_internare ,
J1031083.name  AS  popup_criteriu_internare ,
J1031074.id_focg_criteriu_internare  AS  _popup_criteriu_internare ,
J1031084.stencil_no  AS  popup_medic_curant ,
J1031084.id  AS  _popup_medic_curant ,
J1031089.value  AS  popup_nivel_instruire ,
J1031074.id_education_level  AS  _popup_nivel_instruire ,
J1031074.greutate  AS  text_greutate_internare ,
J1031090.nume  AS  popup_situatii_speciale ,
J1031074.id_focg_situatii_speciale  AS  _popup_situatii_speciale ,
J1031091.nume  AS  popup_internat_prin ,
J1031091.id  AS  _popup_internat_prin ,
J1031092.nume  AS  popup_formulare_europene ,
J1031074.id_formulare_europene  AS  _popup_formulare_europene ,
J1031074.id_cnp_mama  AS  _popup_cnp_mama ,
J1031094.cnp  AS  popup_cnp_mama ,
J1031093.nrfo  AS  popup_fo_mama ,
J1031074.id_focg  AS  _popup_fo_mama ,
J1031074.nr_card_euro  AS  text_nr_card_european ,
J1031074.nr_pasaport  AS  text_nr_pasaport ,
J1031074.nr_card_national  AS  text_nr_card_national ,
J1031088.id  AS  _popup_ocupatia ,
J1031088.name  AS  popup_ocupatia ,
J1031074.export_drg  AS  export_drg ,
J1031074.drgcaseid  AS  drgcaseid ,
J1031074.export_ecosoft  AS  export_ecosoft ,
J1031074.mesaj_drg  AS  mesaj_drg ,
J1031074.uid  AS  uid ,
J1031074.mesaj_ecosoft  AS  mesaj_ecosoft ,
J1031074.id_address_domiciliu  AS  text_id_address_domiciliu ,
J1031074.id_address_domiciliu  AS  _text_id_address_domiciliu ,
J1031074.id_address_resedinta  AS  _text_id_address_resedinta ,
J1031074.id_address_resedinta  AS  text_id_address_resedinta ,
'0'  AS  marcator ,
J1031095.id  AS  _popup_sursa_internare ,
J1031095.denumire  AS  popup_sursa_internare ,
J1031096.id  AS  _popup_diseasecategory ,
J1031096.code  AS  popup_diseasecategory ,
J1031097.id  AS  _popup_diagnostic_internare_icd10 ,
J1031097.name  AS  popup_diagnostic_internare_icd10 ,
J1031098.id  AS  _popup_mod_contract ,
J1031098.description  AS  popup_mod_contract ,
J1031099.id  AS  _popup_criteriu_urgenta ,
J1031099.name  AS  popup_criteriu_urgenta ,
J1031100.id  AS  _popup_exceptie_bi ,
J1031100.code  AS  popup_exceptie_bi ,
J1031074.scrisoare_medicala_parafa  AS  text_parafa_scrisoare_medicala ,
J1031074.scrisoare_medicala_contract  AS
text_contract_scrisoare_medicala ,
J1031074.scrisoare_medicala_tip_contract  AS
text_tip_contract_scrisoare_medicala ,
J1031074.export_siui  AS  export_siui ,
J1031074.mesaj_siui  AS  mesaj_siui ,
J1031087.id  AS  _popup_intocmit ,
J1031087.stencil_no  AS  popup_intocmit ,
J1031074.diagnostic_trimitere_text  AS  text_diagnostic_trimitere ,
J1031074.greutate_externare  AS  text_greutate_externare ,
J1031074.data_decesului  AS  text_data_deces ,
J1031736.id  AS  _popup_tip_externare ,
J1031736.descriere  AS  popup_tip_externare ,
J1031737.id  AS  _popup_stare_externare ,
J1031737.descriere  AS  popup_stare_externare ,
J1033295.id  AS  _popup_chirurg ,
J1033295.stencil_no  AS  popup_chirurg ,
J1033299.id  AS  _text_caz_externare ,
J1033299.description  AS  text_caz_externare ,
J1031074.text_ore_ventilatie  AS  text_ore_ventilatie ,
J1031074.drg_cod_grupa  AS  text_drg_cod_grupa ,
J1031074.drg_relative_value  AS  text_drg_relative_value ,
J1031074.data_2500g  AS  text_data_2500g ,
J1031074.prematur_gr_i  AS  text_prematur_gr_i ,
J1033304.id  AS  _popup_cetatenie ,
J1033304.description  AS  popup_cetatenie ,
J1033358.id  AS  _popup_tip_cetatenie ,
J1033358.name  AS  popup_tip_cetatenie ,
J1031074.reinternat_transfer  AS  text_reinternat_transfer ,
J1031074.aviz_comisie  AS  text_aviz_comisie_reinternare ,
J1031074.criteriu_urgenta1  AS  criteriu_urgenta1 ,
J1031074.criteriu_urgenta2  AS  criteriu_urgenta2 ,
J1031074.criteriu_urgenta3  AS  criteriu_urgenta3 ,
J1031074.criteriu_urgenta4  AS  criteriu_urgenta4 ,
J1031074.criteriu_urgenta5  AS  criteriu_urgenta5 ,
J1031074.criteriu_urgenta6  AS  criteriu_urgenta6 ,
J1031074.criteriu_urgenta7  AS  criteriu_urgenta7 ,
J1031074.criteriu_urgenta8  AS  criteriu_urgenta8 ,
J1031074.criteriu_urgenta9a  AS  criteriu_urgenta9a ,
J1031074.criteriu_urgenta9b  AS  criteriu_urgenta9b ,
J1031074.criteriu_urgenta10  AS  criteriu_urgenta10 ,
J1031074.criteriu_urgenta11  AS  criteriu_urgenta11 ,
J1031074.criteriu_urgenta12  AS  criteriu_urgenta12 ,
J1031074.criteriu_urgenta13  AS  criteriu_urgenta13 ,
J1031074.infectie_nosocomiala  AS  infectie_nosocomiala ,
J1031074.cu_programare  AS  cu_programare ,
J1031074.stare_critica_internare  AS  stare_critica_internare ,
J1033704.id  AS  _id_stari_spitalizari ,
J1033704.nume  AS  id_stari_spitalizari
    FROM focg AS J1031074
    LEFT JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg =
J1031074.id)
    LEFT JOIN department AS J1033386 ON ( J1033386.id =
J1031101.iddepartment)
    LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg =
J1031074.id AND  J1031102.validfrom < J1031101.validfrom)
    LEFT JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg =
J1031074.id)
    LEFT JOIN department AS J1033387 ON ( J1033387.id =
J1033359.iddepartment)
    LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg =
J1031074.id AND  J1033360.validfrom > J1033359.validfrom)
    JOIN focgdepartment AS J1033407 ON ( J1033407.idfocg = J1031074.id
AND  J1033407.iddepartment IN (/*Executing QueryID 476590*/SELECT
DISTINCT(J1034477.id)  AS  ret FROM department AS J1034477  JOIN
personnel_department AS J1034478 ON ( J1034478.id_department =
J1034477.id) JOIN personnel AS J1034479 ON ( J1034479.id =
J1034478.id_personnel AND  J1034479.id_connected_user = '1')))

    LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient)
    LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex)
    LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id =
J1031074.id_focg_serials)
    LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id =
J1031074.idensuredstatustype)
    LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id =
J1031074.idensuredstatus)
    LEFT JOIN org_units AS J1031080 ON ( J1031080.id =
J1031074.id_org_unit)
    LEFT JOIN personstate AS J1031081 ON ( J1031081.id =
J1031074.id_categorie_asigurat)
    LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id =
J1031074.id_focg_tip_internare)
    LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id =
J1031074.id_focg_criteriu_internare)
    LEFT JOIN physicians AS J1031084 ON ( J1031084.id =
J1031074.idphysiciancurrent)
    LEFT JOIN personnel AS J1031085 ON ( J1031085.id =
J1031084.id_personnel)
    LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson)
    LEFT JOIN physicians AS J1031087 ON ( J1031087.id =
J1031074.idphysician_madeby)
    LEFT JOIN proffession AS J1031088 ON ( J1031088.id =
J1031074.idproffesion)
    LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id =
J1031074.id_education_level)
    LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id =
J1031074.id_focg_situatii_speciale)
    LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id =
J1031074.id_focg_internat_prin)
    LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id =
J1031074.id_formulare_europene)
    LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg)
    LEFT JOIN person AS J1031094 ON ( J1031094.id = J1031074.id_cnp_mama)
    LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id =
J1031074.id_focg_sursa_internare)
    LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id =
J1031074.id_diseasecategory)
    LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10)
    LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id =
J1031074.id_focg_contract_modes)
    LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id =
J1031074.id_focg_criterii_urgenta)
    LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id =
J1031074.id_exceptie_bilet_internare)
    LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id =
J1031074.id_focg_tip_externare)
    LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id =
J1031074.id_focg_stare_externare)
    LEFT JOIN physicians AS J1033295 ON ( J1033295.id =
J1031074.idphysician_surgeon)
    LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id =
J1031074.id_hospital_release_statuses)
    LEFT JOIN citizenship AS J1033304 ON ( J1033304.id =
J1031074.id_citizenship)
    LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id =
J1031074.id_drg_tip_cetatenie)
    LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id =
J1031074.id_stari_spitalizari)

  WHERE   J1031102.id IS NULL   AND    J1033360.id IS NULL   AND    (
(   DATE(J1031101.validfrom)>= DATE('2016-05-01')   AND
DATE(J1031101.validfrom)<= DATE('2016-05-31 00:00:00.0')   )    OR    (
   J1033359.validto IS NOT NULL   AND   DATE(J1033359.validto)>=
DATE('2016-05-01')   AND   DATE(J1033359.validto)<= DATE('2016-05-31
00:00:00.0')   )    OR    (    J1033359.validto IS NULL   AND
DATE(J1031101.validfrom)<= DATE('2016-05-01')   )    )

    ORDER BY J1031101.validfrom DESC
  LIMIT  20 OFFSET 0


------ Original Message ------
From: "Sterpu Victor" <victor@caido.ro>
To: "David Rowley" <david.rowley@2ndquadrant.com>
Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
<pgsql-general@postgresql.org>; "David G. Johnston"
<david.g.johnston@gmail.com>
Sent: 9/5/2016 10:44:45 AM
Subject: Re: [GENERAL] Slow query when the select list is big

>I solved the problem patialy by swithing the order of a join.
>I tested on a slower server to see better the difference.
>After moving a single join the query runs in 4.1 seconds insted 6.4
>seconds.
>I pasted the optimized query down. When I move the join J1033704 at the
>end(last join) the time is increased to 6.4 seconds.
>
>SELECT
>row_number() OVER (ORDER BY J1031101.validfrom DESC) AS nrcrt ,
>CASE WHEN(CHAR_LENGTH(J1031074.signature) > 10) THEN(1) ELSE(0) END AS
>semnat_internare ,
>CASE WHEN(CHAR_LENGTH(J1031074.signature_externare) > 10) THEN(1)
>ELSE(0) END AS semnat_externare ,
>J1031076.name AS sex ,
>J1031074.id AS id ,
>J1031074.siui_appid AS siui_appid ,
>J1031074.data_adeverinta AS data_adeverinta ,
>J1031074.is_paliativ AS text_paliativ ,
>J1031074.cardno AS cardno ,
>J1031074.cardno_externare AS cardno_externare ,
>J1031074.sign_date AS sign_date ,
>J1031074.sign_date_externare AS sign_date_externare ,
>J1031074.unsigned_string AS unsigned_string ,
>J1031074.unsigned_string_externare AS unsigned_string_externare ,
>J1031074.signhash AS signhash ,
>J1031074.signhash_externare AS signhash_externare ,
>J1031074.signature AS signature ,
>J1031074.signature_externare AS signature_externare ,
>J1031074.send_xml AS send_xml ,
>J1031074.send_xml_externare AS send_xml_externare ,
>J1031074.received_xml AS received_xml ,
>J1031074.received_xml_externare AS received_xml_externare ,
>J1031074.error AS error ,
>J1031074.error_externare AS error_externare ,
>J1031074.validat AS validat ,
>J1031074.validat_externare AS validat_externare ,
>J1031074.online AS online ,
>J1031074.online_externare AS online_externare ,
>J1031074.serie_bilet_internare AS text_serie_bilet_internare ,
>J1031074.nr_bilet_internare AS text_numar_bilet_internare ,
>J1031074.idpatient AS _popup_cnp_pacient ,
>J1031075.cnp AS popup_cnp_pacient ,
>J1031075.name AS text_nume_pacient ,
>J1031075.surname AS text_prenume_pacient ,
>J1031074.nrfo AS text_numar_fosz ,
>J1031074.greutate_nastere AS text_greutate_nastere ,
>J1031078.value AS popup_tip_asigurare ,
>J1031074.idensuredstatustype AS _popup_tip_asigurare ,
>J1031079.value AS popup_statut_asigurat ,
>J1031074.idensuredstatus AS _popup_statut_asigurat ,
>J1031080.code AS popup_cas_asigurat ,
>J1031074.id_org_unit AS _popup_cas_asigurat ,
>J1031081.code AS popup_categorie_asigurare ,
>J1031074.id_categorie_asigurat AS _popup_categorie_asigurare ,
>J1031082.name AS popup_tip_internare ,
>J1031074.id_focg_tip_internare AS _popup_tip_internare ,
>J1031083.name AS popup_criteriu_internare ,
>J1031074.id_focg_criteriu_internare AS _popup_criteriu_internare ,
>J1031084.stencil_no AS popup_medic_curant ,
>J1031084.id AS _popup_medic_curant ,
>J1031089.value AS popup_nivel_instruire ,
>J1031074.id_education_level AS _popup_nivel_instruire ,
>J1031074.greutate AS text_greutate_internare ,
>J1031090.nume AS popup_situatii_speciale ,
>J1031074.id_focg_situatii_speciale AS _popup_situatii_speciale ,
>J1031091.nume AS popup_internat_prin ,
>J1031091.id AS _popup_internat_prin ,
>J1031092.nume AS popup_formulare_europene ,
>J1031074.id_formulare_europene AS _popup_formulare_europene ,
>J1031074.id_cnp_mama AS _popup_cnp_mama ,
>J1031094.cnp AS popup_cnp_mama ,
>J1031093.nrfo AS popup_fo_mama ,
>J1031074.id_focg AS _popup_fo_mama ,
>J1031074.nr_card_euro AS text_nr_card_european ,
>J1031074.nr_pasaport AS text_nr_pasaport ,
>J1031074.nr_card_national AS text_nr_card_national ,
>J1031088.id AS _popup_ocupatia ,
>J1031088.name AS popup_ocupatia ,
>J1031074.export_drg AS export_drg ,
>J1031074.drgcaseid AS drgcaseid ,
>J1031074.export_ecosoft AS export_ecosoft ,
>J1031074.mesaj_drg AS mesaj_drg ,
>J1031074.uid AS uid ,
>J1031074.mesaj_ecosoft AS mesaj_ecosoft ,
>J1031074.id_address_domiciliu AS text_id_address_domiciliu ,
>J1031074.id_address_domiciliu AS _text_id_address_domiciliu ,
>J1031074.id_address_resedinta AS _text_id_address_resedinta ,
>J1031074.id_address_resedinta AS text_id_address_resedinta ,
>'0' AS marcator ,
>J1031095.id AS _popup_sursa_internare ,
>J1031095.denumire AS popup_sursa_internare ,
>J1031096.id AS _popup_diseasecategory ,
>J1031096.code AS popup_diseasecategory ,
>J1031097.id AS _popup_diagnostic_internare_icd10 ,
>J1031097.name AS popup_diagnostic_internare_icd10 ,
>J1031098.id AS _popup_mod_contract ,
>J1031098.description AS popup_mod_contract ,
>J1031099.id AS _popup_criteriu_urgenta ,
>J1031099.name AS popup_criteriu_urgenta ,
>J1031100.id AS _popup_exceptie_bi ,
>J1031100.code AS popup_exceptie_bi ,
>J1031074.scrisoare_medicala_parafa AS text_parafa_scrisoare_medicala ,
>J1031074.scrisoare_medicala_contract AS
>text_contract_scrisoare_medicala ,
>J1031074.scrisoare_medicala_tip_contract AS
>text_tip_contract_scrisoare_medicala ,
>J1031074.export_siui AS export_siui ,
>J1031074.mesaj_siui AS mesaj_siui ,
>J1031087.id AS _popup_intocmit ,
>J1031087.stencil_no AS popup_intocmit ,
>J1031074.diagnostic_trimitere_text AS text_diagnostic_trimitere ,
>J1031074.greutate_externare AS text_greutate_externare ,
>J1031074.data_decesului AS text_data_deces ,
>J1031736.id AS _popup_tip_externare ,
>J1031736.descriere AS popup_tip_externare ,
>J1031737.id AS _popup_stare_externare ,
>J1031737.descriere AS popup_stare_externare ,
>J1033295.id AS _popup_chirurg ,
>J1033295.stencil_no AS popup_chirurg ,
>J1033299.id AS _text_caz_externare ,
>J1033299.description AS text_caz_externare ,
>J1031074.text_ore_ventilatie AS text_ore_ventilatie ,
>J1031074.drg_cod_grupa AS text_drg_cod_grupa ,
>J1031074.drg_relative_value AS text_drg_relative_value ,
>J1031074.data_2500g AS text_data_2500g ,
>J1031074.prematur_gr_i AS text_prematur_gr_i ,
>J1033304.id AS _popup_cetatenie ,
>J1033304.description AS popup_cetatenie ,
>J1033358.id AS _popup_tip_cetatenie ,
>J1033358.name AS popup_tip_cetatenie ,
>J1031074.reinternat_transfer AS text_reinternat_transfer ,
>J1031074.aviz_comisie AS text_aviz_comisie_reinternare ,
>J1031074.criteriu_urgenta1 AS criteriu_urgenta1 ,
>J1031074.criteriu_urgenta2 AS criteriu_urgenta2 ,
>J1031074.criteriu_urgenta3 AS criteriu_urgenta3 ,
>J1031074.criteriu_urgenta4 AS criteriu_urgenta4 ,
>J1031074.criteriu_urgenta5 AS criteriu_urgenta5 ,
>J1031074.criteriu_urgenta6 AS criteriu_urgenta6 ,
>J1031074.criteriu_urgenta7 AS criteriu_urgenta7 ,
>J1031074.criteriu_urgenta8 AS criteriu_urgenta8 ,
>J1031074.criteriu_urgenta9a AS criteriu_urgenta9a ,
>J1031074.criteriu_urgenta9b AS criteriu_urgenta9b ,
>J1031074.criteriu_urgenta10 AS criteriu_urgenta10 ,
>J1031074.criteriu_urgenta11 AS criteriu_urgenta11 ,
>J1031074.criteriu_urgenta12 AS criteriu_urgenta12 ,
>J1031074.criteriu_urgenta13 AS criteriu_urgenta13 ,
>J1031074.infectie_nosocomiala AS infectie_nosocomiala ,
>J1031074.cu_programare AS cu_programare ,
>J1031074.stare_critica_internare AS stare_critica_internare ,
>J1033704.id AS _id_stari_spitalizari ,
>J1033704.nume AS id_stari_spitalizari
>    FROM focg AS J1031074
>   LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient)
>    LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex)
>    LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id =
>J1031074.id_focg_serials)
>    LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id =
>J1031074.idensuredstatustype)
>    LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id =
>J1031074.idensuredstatus)
>    LEFT JOIN org_units AS J1031080 ON ( J1031080.id =
>J1031074.id_org_unit)
>    LEFT JOIN personstate AS J1031081 ON ( J1031081.id =
>J1031074.id_categorie_asigurat)
>    LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id =
>J1031074.id_focg_tip_internare)
>    LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id =
>J1031074.id_focg_criteriu_internare)
>    LEFT JOIN physicians AS J1031084 ON ( J1031084.id =
>J1031074.idphysiciancurrent)
>    LEFT JOIN personnel AS J1031085 ON ( J1031085.id =
>J1031084.id_personnel)
>    LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson)
>    LEFT JOIN physicians AS J1031087 ON ( J1031087.id =
>J1031074.idphysician_madeby)
>    LEFT JOIN proffession AS J1031088 ON ( J1031088.id =
>J1031074.idproffesion)
>    LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id =
>J1031074.id_education_level)
>    LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id =
>J1031074.id_focg_situatii_speciale)
>    LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id =
>J1031074.id_focg_internat_prin)
>    LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id =
>J1031074.id_formulare_europene)
>    LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg)
>    LEFT JOIN person AS J1031094 ON ( J1031094.id =
>J1031074.id_cnp_mama)
>    LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id =
>J1031074.id_focg_sursa_internare)
>    LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id =
>J1031074.id_diseasecategory)
>    LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10)
>    LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id =
>J1031074.id_focg_contract_modes)
>    LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id =
>J1031074.id_focg_criterii_urgenta)
>    LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id =
>J1031074.id_exceptie_bilet_internare)
>    LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id =
>J1031074.id_focg_tip_externare)
>    LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id =
>J1031074.id_focg_stare_externare)
>    LEFT JOIN physicians AS J1033295 ON ( J1033295.id =
>J1031074.idphysician_surgeon)
>    LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id =
>J1031074.id_hospital_release_statuses)
>    LEFT JOIN citizenship AS J1033304 ON ( J1033304.id =
>J1031074.id_citizenship)
>    LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id =
>J1031074.id_drg_tip_cetatenie)
>    LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id =
>J1031074.id_stari_spitalizari)
>    LEFT JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg =
>J1031074.id)
>    LEFT JOIN department AS J1033386 ON ( J1033386.id =
>J1031101.iddepartment)
>    LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg =
>J1031074.id AND J1031102.validfrom < J1031101.validfrom)
>    LEFT JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg =
>J1031074.id)
>    LEFT JOIN department AS J1033387 ON ( J1033387.id =
>J1033359.iddepartment)
>    LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg =
>J1031074.id AND J1033360.validfrom > J1033359.validfrom)
>     JOIN focgdepartment AS J1033407 ON ( J1033407.idfocg = J1031074.id
>AND J1033407.iddepartment IN (/*Executing QueryID 476590*/SELECT
>DISTINCT(J1034477.id) AS ret FROM department AS J1034477 JOIN
>personnel_department AS J1034478 ON ( J1034478.id_department =
>J1034477.id)
>   JOIN personnel AS J1034479 ON ( J1034479.id = J1034478.id_personnel
>AND J1034479.id_connected_user = '1')))
>
>  WHERE J1031102.id IS NULL AND J1033360.id IS NULL AND ( (
>DATE(J1031101.validfrom)>= DATE('2016-05-01') AND
>DATE(J1031101.validfrom)<= DATE('2016-05-31 00:00:00.0') ) OR (
>J1033359.validto IS NOT NULL AND DATE(J1033359.validto)>=
>DATE('2016-05-01') AND DATE(J1033359.validto)<= DATE('2016-05-31
>00:00:00.0') ) OR ( J1033359.validto IS NULL AND
>DATE(J1031101.validfrom)<= DATE('2016-05-01') ) )
>
>    ORDER BY J1031101.validfrom DESC
>  LIMIT 20 OFFSET 0
>
>------ Original Message ------
>From: "David Rowley" <david.rowley@2ndquadrant.com>
>To: "Sterpu Victor" <victor@caido.ro>
>Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
><pgsql-general@postgresql.org>; "David G. Johnston"
><david.g.johnston@gmail.com>
>Sent: 9/5/2016 10:04:54 AM
>Subject: Re: [GENERAL] Slow query when the select list is big
>
>>On 9 May 2016 at 18:46, David G. Johnston <david.g.johnston@gmail.com>
>>wrote:
>>>  On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
>>>>
>>>>  Yes but it is very big.
>>>>  I don't understand why the select list is influencing the CPU
>>>>usage.
>>>>  I was expecting that only the join and where clauses would
>>>>influence CPU.
>>>>
>>>
>>>  PostgreSQL is smart enough to optimize away stuff that it knows
>>>doesn't
>>>  impact the final query result.
>>
>>To be more accurate with what David is saying, PostgreSQL will remove
>>unused LEFT JOINed relations where the left joined relation can be
>>proved to not duplicate rows from the right hand side. It would just
>>be a matter of comparing the EXPLAINs from the query with all the
>>SELECT items to the one with the single SELECT item to prove that this
>>is what's happening.
>>
>>Please also note that this only occurs with LEFT JOINs
>>
>>It would also be quite helpful for people if you were to include a
>>copy of the query. It's impossible to reverse engineer what that is
>>from this EXPLAIN output. I see that your using a windowing function
>>and performing a LIMIT 1, there may be ways to improve that just by
>>selecting the single highest j1031101.validfrom row and performing the
>>joins to the other table on that single row, but that will depend on
>>which windowing function you're using as the function may require the
>>other rows in the window frame to calculate the correct result.
>>
>>--
>>  David Rowley http://www.2ndQuadrant.com/
>>  PostgreSQL Development, 24x7 Support, Training & Services
>
>
>
>-- Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
>To make changes to your subscription:
>http://www.postgresql.org/mailpref/pgsql-general



Re: Slow query when the select list is big

From
Victor Yegorov
Date:
2016-05-09 11:01 GMT+03:00 Sterpu Victor <victor@caido.ro>:
I went to 2.4 seconds by joining first the tables that produce many rows.

As you're changing your query quite often, it'd be handy, if you could post both:
- new query version
- it's `EXECUTE (analyze, buffers)` output

If you provide either one or another, but not both, it is difficult to comment on your case.


You can use http://explain.depesz.com/ to post a link to your execution plan here.


--
Victor Y. Yegorov

Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
Acctualy the optimization should be cauzed by the filters on the joins
that have been moved at the begining of the query.
So now postres is making a filter before joining a lot of data.
The fact that these join produce multiple rows is not relevant.
At least this is what I think is heapening.

------ Original Message ------
From: "Sterpu Victor" <victor@caido.ro>
To: "Sterpu Victor" <victor@caido.ro>; "David Rowley"
<david.rowley@2ndquadrant.com>
Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
<pgsql-general@postgresql.org>; "David G. Johnston"
<david.g.johnston@gmail.com>
Sent: 9/5/2016 11:01:56 AM
Subject: Re[2]: [GENERAL] Slow query when the select list is big

>I went to 2.4 seconds by joining first the tables that produce many
>rows.
>
>SELECT
>row_number() OVER (ORDER BY J1031101.validfrom DESC) AS nrcrt ,
>J1033386.name AS sectie_internare ,
>J1033387.name AS sectie_externare ,
>TO_CHAR(J1031101.validfrom , 'YYYY-MM-DD HH24:MI') AS validfrom ,
>TO_CHAR(J1033359.validto , 'YYYY-MM-DD HH24:MI') AS validto ,
>CASE WHEN(CHAR_LENGTH(J1031074.signature) > 10) THEN(1) ELSE(0) END AS
>semnat_internare ,
>CASE WHEN(CHAR_LENGTH(J1031074.signature_externare) > 10) THEN(1)
>ELSE(0) END AS semnat_externare ,
>J1031076.name AS sex ,
>J1031074.id AS id ,
>J1031074.siui_appid AS siui_appid ,
>J1031074.data_adeverinta AS data_adeverinta ,
>J1031074.is_paliativ AS text_paliativ ,
>J1031074.cardno AS cardno ,
>J1031074.cardno_externare AS cardno_externare ,
>J1031074.sign_date AS sign_date ,
>J1031074.sign_date_externare AS sign_date_externare ,
>J1031074.unsigned_string AS unsigned_string ,
>J1031074.unsigned_string_externare AS unsigned_string_externare ,
>J1031074.signhash AS signhash ,
>J1031074.signhash_externare AS signhash_externare ,
>J1031074.signature AS signature ,
>J1031074.signature_externare AS signature_externare ,
>J1031074.send_xml AS send_xml ,
>J1031074.send_xml_externare AS send_xml_externare ,
>J1031074.received_xml AS received_xml ,
>J1031074.received_xml_externare AS received_xml_externare ,
>J1031074.error AS error ,
>J1031074.error_externare AS error_externare ,
>J1031074.validat AS validat ,
>J1031074.validat_externare AS validat_externare ,
>J1031074.online AS online ,
>J1031074.online_externare AS online_externare ,
>J1031074.serie_bilet_internare AS text_serie_bilet_internare ,
>J1031074.nr_bilet_internare AS text_numar_bilet_internare ,
>J1031074.idpatient AS _popup_cnp_pacient ,
>J1031075.cnp AS popup_cnp_pacient ,
>J1031075.name AS text_nume_pacient ,
>J1031075.surname AS text_prenume_pacient ,
>J1031074.nrfo AS text_numar_fosz ,
>J1031074.greutate_nastere AS text_greutate_nastere ,
>J1031078.value AS popup_tip_asigurare ,
>J1031074.idensuredstatustype AS _popup_tip_asigurare ,
>J1031079.value AS popup_statut_asigurat ,
>J1031074.idensuredstatus AS _popup_statut_asigurat ,
>J1031080.code AS popup_cas_asigurat ,
>J1031074.id_org_unit AS _popup_cas_asigurat ,
>J1031081.code AS popup_categorie_asigurare ,
>J1031074.id_categorie_asigurat AS _popup_categorie_asigurare ,
>J1031082.name AS popup_tip_internare ,
>J1031074.id_focg_tip_internare AS _popup_tip_internare ,
>J1031083.name AS popup_criteriu_internare ,
>J1031074.id_focg_criteriu_internare AS _popup_criteriu_internare ,
>J1031084.stencil_no AS popup_medic_curant ,
>J1031084.id AS _popup_medic_curant ,
>J1031089.value AS popup_nivel_instruire ,
>J1031074.id_education_level AS _popup_nivel_instruire ,
>J1031074.greutate AS text_greutate_internare ,
>J1031090.nume AS popup_situatii_speciale ,
>J1031074.id_focg_situatii_speciale AS _popup_situatii_speciale ,
>J1031091.nume AS popup_internat_prin ,
>J1031091.id AS _popup_internat_prin ,
>J1031092.nume AS popup_formulare_europene ,
>J1031074.id_formulare_europene AS _popup_formulare_europene ,
>J1031074.id_cnp_mama AS _popup_cnp_mama ,
>J1031094.cnp AS popup_cnp_mama ,
>J1031093.nrfo AS popup_fo_mama ,
>J1031074.id_focg AS _popup_fo_mama ,
>J1031074.nr_card_euro AS text_nr_card_european ,
>J1031074.nr_pasaport AS text_nr_pasaport ,
>J1031074.nr_card_national AS text_nr_card_national ,
>J1031088.id AS _popup_ocupatia ,
>J1031088.name AS popup_ocupatia ,
>J1031074.export_drg AS export_drg ,
>J1031074.drgcaseid AS drgcaseid ,
>J1031074.export_ecosoft AS export_ecosoft ,
>J1031074.mesaj_drg AS mesaj_drg ,
>J1031074.uid AS uid ,
>J1031074.mesaj_ecosoft AS mesaj_ecosoft ,
>J1031074.id_address_domiciliu AS text_id_address_domiciliu ,
>J1031074.id_address_domiciliu AS _text_id_address_domiciliu ,
>J1031074.id_address_resedinta AS _text_id_address_resedinta ,
>J1031074.id_address_resedinta AS text_id_address_resedinta ,
>'0' AS marcator ,
>J1031095.id AS _popup_sursa_internare ,
>J1031095.denumire AS popup_sursa_internare ,
>J1031096.id AS _popup_diseasecategory ,
>J1031096.code AS popup_diseasecategory ,
>J1031097.id AS _popup_diagnostic_internare_icd10 ,
>J1031097.name AS popup_diagnostic_internare_icd10 ,
>J1031098.id AS _popup_mod_contract ,
>J1031098.description AS popup_mod_contract ,
>J1031099.id AS _popup_criteriu_urgenta ,
>J1031099.name AS popup_criteriu_urgenta ,
>J1031100.id AS _popup_exceptie_bi ,
>J1031100.code AS popup_exceptie_bi ,
>J1031074.scrisoare_medicala_parafa AS text_parafa_scrisoare_medicala ,
>J1031074.scrisoare_medicala_contract AS
>text_contract_scrisoare_medicala ,
>J1031074.scrisoare_medicala_tip_contract AS
>text_tip_contract_scrisoare_medicala ,
>J1031074.export_siui AS export_siui ,
>J1031074.mesaj_siui AS mesaj_siui ,
>J1031087.id AS _popup_intocmit ,
>J1031087.stencil_no AS popup_intocmit ,
>J1031074.diagnostic_trimitere_text AS text_diagnostic_trimitere ,
>J1031074.greutate_externare AS text_greutate_externare ,
>J1031074.data_decesului AS text_data_deces ,
>J1031736.id AS _popup_tip_externare ,
>J1031736.descriere AS popup_tip_externare ,
>J1031737.id AS _popup_stare_externare ,
>J1031737.descriere AS popup_stare_externare ,
>J1033295.id AS _popup_chirurg ,
>J1033295.stencil_no AS popup_chirurg ,
>J1033299.id AS _text_caz_externare ,
>J1033299.description AS text_caz_externare ,
>J1031074.text_ore_ventilatie AS text_ore_ventilatie ,
>J1031074.drg_cod_grupa AS text_drg_cod_grupa ,
>J1031074.drg_relative_value AS text_drg_relative_value ,
>J1031074.data_2500g AS text_data_2500g ,
>J1031074.prematur_gr_i AS text_prematur_gr_i ,
>J1033304.id AS _popup_cetatenie ,
>J1033304.description AS popup_cetatenie ,
>J1033358.id AS _popup_tip_cetatenie ,
>J1033358.name AS popup_tip_cetatenie ,
>J1031074.reinternat_transfer AS text_reinternat_transfer ,
>J1031074.aviz_comisie AS text_aviz_comisie_reinternare ,
>J1031074.criteriu_urgenta1 AS criteriu_urgenta1 ,
>J1031074.criteriu_urgenta2 AS criteriu_urgenta2 ,
>J1031074.criteriu_urgenta3 AS criteriu_urgenta3 ,
>J1031074.criteriu_urgenta4 AS criteriu_urgenta4 ,
>J1031074.criteriu_urgenta5 AS criteriu_urgenta5 ,
>J1031074.criteriu_urgenta6 AS criteriu_urgenta6 ,
>J1031074.criteriu_urgenta7 AS criteriu_urgenta7 ,
>J1031074.criteriu_urgenta8 AS criteriu_urgenta8 ,
>J1031074.criteriu_urgenta9a AS criteriu_urgenta9a ,
>J1031074.criteriu_urgenta9b AS criteriu_urgenta9b ,
>J1031074.criteriu_urgenta10 AS criteriu_urgenta10 ,
>J1031074.criteriu_urgenta11 AS criteriu_urgenta11 ,
>J1031074.criteriu_urgenta12 AS criteriu_urgenta12 ,
>J1031074.criteriu_urgenta13 AS criteriu_urgenta13 ,
>J1031074.infectie_nosocomiala AS infectie_nosocomiala ,
>J1031074.cu_programare AS cu_programare ,
>J1031074.stare_critica_internare AS stare_critica_internare ,
>J1033704.id AS _id_stari_spitalizari ,
>J1033704.nume AS id_stari_spitalizari
>    FROM focg AS J1031074
>    LEFT JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg =
>J1031074.id)
>    LEFT JOIN department AS J1033386 ON ( J1033386.id =
>J1031101.iddepartment)
>    LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg =
>J1031074.id AND J1031102.validfrom < J1031101.validfrom)
>    LEFT JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg =
>J1031074.id)
>    LEFT JOIN department AS J1033387 ON ( J1033387.id =
>J1033359.iddepartment)
>    LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg =
>J1031074.id AND J1033360.validfrom > J1033359.validfrom)
>    JOIN focgdepartment AS J1033407 ON ( J1033407.idfocg = J1031074.id
>AND J1033407.iddepartment IN (/*Executing QueryID 476590*/SELECT
>DISTINCT(J1034477.id) AS ret FROM department AS J1034477 JOIN
>personnel_department AS J1034478 ON ( J1034478.id_department =
>J1034477.id) JOIN personnel AS J1034479 ON ( J1034479.id =
>J1034478.id_personnel AND J1034479.id_connected_user = '1')))
>
>    LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient)
>    LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex)
>    LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id =
>J1031074.id_focg_serials)
>    LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id =
>J1031074.idensuredstatustype)
>    LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id =
>J1031074.idensuredstatus)
>    LEFT JOIN org_units AS J1031080 ON ( J1031080.id =
>J1031074.id_org_unit)
>    LEFT JOIN personstate AS J1031081 ON ( J1031081.id =
>J1031074.id_categorie_asigurat)
>    LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id =
>J1031074.id_focg_tip_internare)
>    LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id =
>J1031074.id_focg_criteriu_internare)
>    LEFT JOIN physicians AS J1031084 ON ( J1031084.id =
>J1031074.idphysiciancurrent)
>    LEFT JOIN personnel AS J1031085 ON ( J1031085.id =
>J1031084.id_personnel)
>    LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson)
>    LEFT JOIN physicians AS J1031087 ON ( J1031087.id =
>J1031074.idphysician_madeby)
>    LEFT JOIN proffession AS J1031088 ON ( J1031088.id =
>J1031074.idproffesion)
>    LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id =
>J1031074.id_education_level)
>    LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id =
>J1031074.id_focg_situatii_speciale)
>    LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id =
>J1031074.id_focg_internat_prin)
>    LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id =
>J1031074.id_formulare_europene)
>    LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg)
>    LEFT JOIN person AS J1031094 ON ( J1031094.id =
>J1031074.id_cnp_mama)
>    LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id =
>J1031074.id_focg_sursa_internare)
>    LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id =
>J1031074.id_diseasecategory)
>    LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10)
>    LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id =
>J1031074.id_focg_contract_modes)
>    LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id =
>J1031074.id_focg_criterii_urgenta)
>    LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id =
>J1031074.id_exceptie_bilet_internare)
>    LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id =
>J1031074.id_focg_tip_externare)
>    LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id =
>J1031074.id_focg_stare_externare)
>    LEFT JOIN physicians AS J1033295 ON ( J1033295.id =
>J1031074.idphysician_surgeon)
>    LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id =
>J1031074.id_hospital_release_statuses)
>    LEFT JOIN citizenship AS J1033304 ON ( J1033304.id =
>J1031074.id_citizenship)
>    LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id =
>J1031074.id_drg_tip_cetatenie)
>    LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id =
>J1031074.id_stari_spitalizari)
>
>  WHERE J1031102.id IS NULL AND J1033360.id IS NULL AND ( (
>DATE(J1031101.validfrom)>= DATE('2016-05-01') AND
>DATE(J1031101.validfrom)<= DATE('2016-05-31 00:00:00.0') ) OR (
>J1033359.validto IS NOT NULL AND DATE(J1033359.validto)>=
>DATE('2016-05-01') AND DATE(J1033359.validto)<= DATE('2016-05-31
>00:00:00.0') ) OR ( J1033359.validto IS NULL AND
>DATE(J1031101.validfrom)<= DATE('2016-05-01') ) )
>
>    ORDER BY J1031101.validfrom DESC
>  LIMIT 20 OFFSET 0
>
>
>------ Original Message ------
>From: "Sterpu Victor" <victor@caido.ro>
>To: "David Rowley" <david.rowley@2ndquadrant.com>
>Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
><pgsql-general@postgresql.org>; "David G. Johnston"
><david.g.johnston@gmail.com>
>Sent: 9/5/2016 10:44:45 AM
>Subject: Re: [GENERAL] Slow query when the select list is big
>
>>I solved the problem patialy by swithing the order of a join.
>>I tested on a slower server to see better the difference.
>>After moving a single join the query runs in 4.1 seconds insted 6.4
>>seconds.
>>I pasted the optimized query down. When I move the join J1033704 at
>>the end(last join) the time is increased to 6.4 seconds.
>>
>>SELECT
>>row_number() OVER (ORDER BY J1031101.validfrom DESC) AS nrcrt ,
>>CASE WHEN(CHAR_LENGTH(J1031074.signature) > 10) THEN(1) ELSE(0) END AS
>>semnat_internare ,
>>CASE WHEN(CHAR_LENGTH(J1031074.signature_externare) > 10) THEN(1)
>>ELSE(0) END AS semnat_externare ,
>>J1031076.name AS sex ,
>>J1031074.id AS id ,
>>J1031074.siui_appid AS siui_appid ,
>>J1031074.data_adeverinta AS data_adeverinta ,
>>J1031074.is_paliativ AS text_paliativ ,
>>J1031074.cardno AS cardno ,
>>J1031074.cardno_externare AS cardno_externare ,
>>J1031074.sign_date AS sign_date ,
>>J1031074.sign_date_externare AS sign_date_externare ,
>>J1031074.unsigned_string AS unsigned_string ,
>>J1031074.unsigned_string_externare AS unsigned_string_externare ,
>>J1031074.signhash AS signhash ,
>>J1031074.signhash_externare AS signhash_externare ,
>>J1031074.signature AS signature ,
>>J1031074.signature_externare AS signature_externare ,
>>J1031074.send_xml AS send_xml ,
>>J1031074.send_xml_externare AS send_xml_externare ,
>>J1031074.received_xml AS received_xml ,
>>J1031074.received_xml_externare AS received_xml_externare ,
>>J1031074.error AS error ,
>>J1031074.error_externare AS error_externare ,
>>J1031074.validat AS validat ,
>>J1031074.validat_externare AS validat_externare ,
>>J1031074.online AS online ,
>>J1031074.online_externare AS online_externare ,
>>J1031074.serie_bilet_internare AS text_serie_bilet_internare ,
>>J1031074.nr_bilet_internare AS text_numar_bilet_internare ,
>>J1031074.idpatient AS _popup_cnp_pacient ,
>>J1031075.cnp AS popup_cnp_pacient ,
>>J1031075.name AS text_nume_pacient ,
>>J1031075.surname AS text_prenume_pacient ,
>>J1031074.nrfo AS text_numar_fosz ,
>>J1031074.greutate_nastere AS text_greutate_nastere ,
>>J1031078.value AS popup_tip_asigurare ,
>>J1031074.idensuredstatustype AS _popup_tip_asigurare ,
>>J1031079.value AS popup_statut_asigurat ,
>>J1031074.idensuredstatus AS _popup_statut_asigurat ,
>>J1031080.code AS popup_cas_asigurat ,
>>J1031074.id_org_unit AS _popup_cas_asigurat ,
>>J1031081.code AS popup_categorie_asigurare ,
>>J1031074.id_categorie_asigurat AS _popup_categorie_asigurare ,
>>J1031082.name AS popup_tip_internare ,
>>J1031074.id_focg_tip_internare AS _popup_tip_internare ,
>>J1031083.name AS popup_criteriu_internare ,
>>J1031074.id_focg_criteriu_internare AS _popup_criteriu_internare ,
>>J1031084.stencil_no AS popup_medic_curant ,
>>J1031084.id AS _popup_medic_curant ,
>>J1031089.value AS popup_nivel_instruire ,
>>J1031074.id_education_level AS _popup_nivel_instruire ,
>>J1031074.greutate AS text_greutate_internare ,
>>J1031090.nume AS popup_situatii_speciale ,
>>J1031074.id_focg_situatii_speciale AS _popup_situatii_speciale ,
>>J1031091.nume AS popup_internat_prin ,
>>J1031091.id AS _popup_internat_prin ,
>>J1031092.nume AS popup_formulare_europene ,
>>J1031074.id_formulare_europene AS _popup_formulare_europene ,
>>J1031074.id_cnp_mama AS _popup_cnp_mama ,
>>J1031094.cnp AS popup_cnp_mama ,
>>J1031093.nrfo AS popup_fo_mama ,
>>J1031074.id_focg AS _popup_fo_mama ,
>>J1031074.nr_card_euro AS text_nr_card_european ,
>>J1031074.nr_pasaport AS text_nr_pasaport ,
>>J1031074.nr_card_national AS text_nr_card_national ,
>>J1031088.id AS _popup_ocupatia ,
>>J1031088.name AS popup_ocupatia ,
>>J1031074.export_drg AS export_drg ,
>>J1031074.drgcaseid AS drgcaseid ,
>>J1031074.export_ecosoft AS export_ecosoft ,
>>J1031074.mesaj_drg AS mesaj_drg ,
>>J1031074.uid AS uid ,
>>J1031074.mesaj_ecosoft AS mesaj_ecosoft ,
>>J1031074.id_address_domiciliu AS text_id_address_domiciliu ,
>>J1031074.id_address_domiciliu AS _text_id_address_domiciliu ,
>>J1031074.id_address_resedinta AS _text_id_address_resedinta ,
>>J1031074.id_address_resedinta AS text_id_address_resedinta ,
>>'0' AS marcator ,
>>J1031095.id AS _popup_sursa_internare ,
>>J1031095.denumire AS popup_sursa_internare ,
>>J1031096.id AS _popup_diseasecategory ,
>>J1031096.code AS popup_diseasecategory ,
>>J1031097.id AS _popup_diagnostic_internare_icd10 ,
>>J1031097.name AS popup_diagnostic_internare_icd10 ,
>>J1031098.id AS _popup_mod_contract ,
>>J1031098.description AS popup_mod_contract ,
>>J1031099.id AS _popup_criteriu_urgenta ,
>>J1031099.name AS popup_criteriu_urgenta ,
>>J1031100.id AS _popup_exceptie_bi ,
>>J1031100.code AS popup_exceptie_bi ,
>>J1031074.scrisoare_medicala_parafa AS text_parafa_scrisoare_medicala ,
>>J1031074.scrisoare_medicala_contract AS
>>text_contract_scrisoare_medicala ,
>>J1031074.scrisoare_medicala_tip_contract AS
>>text_tip_contract_scrisoare_medicala ,
>>J1031074.export_siui AS export_siui ,
>>J1031074.mesaj_siui AS mesaj_siui ,
>>J1031087.id AS _popup_intocmit ,
>>J1031087.stencil_no AS popup_intocmit ,
>>J1031074.diagnostic_trimitere_text AS text_diagnostic_trimitere ,
>>J1031074.greutate_externare AS text_greutate_externare ,
>>J1031074.data_decesului AS text_data_deces ,
>>J1031736.id AS _popup_tip_externare ,
>>J1031736.descriere AS popup_tip_externare ,
>>J1031737.id AS _popup_stare_externare ,
>>J1031737.descriere AS popup_stare_externare ,
>>J1033295.id AS _popup_chirurg ,
>>J1033295.stencil_no AS popup_chirurg ,
>>J1033299.id AS _text_caz_externare ,
>>J1033299.description AS text_caz_externare ,
>>J1031074.text_ore_ventilatie AS text_ore_ventilatie ,
>>J1031074.drg_cod_grupa AS text_drg_cod_grupa ,
>>J1031074.drg_relative_value AS text_drg_relative_value ,
>>J1031074.data_2500g AS text_data_2500g ,
>>J1031074.prematur_gr_i AS text_prematur_gr_i ,
>>J1033304.id AS _popup_cetatenie ,
>>J1033304.description AS popup_cetatenie ,
>>J1033358.id AS _popup_tip_cetatenie ,
>>J1033358.name AS popup_tip_cetatenie ,
>>J1031074.reinternat_transfer AS text_reinternat_transfer ,
>>J1031074.aviz_comisie AS text_aviz_comisie_reinternare ,
>>J1031074.criteriu_urgenta1 AS criteriu_urgenta1 ,
>>J1031074.criteriu_urgenta2 AS criteriu_urgenta2 ,
>>J1031074.criteriu_urgenta3 AS criteriu_urgenta3 ,
>>J1031074.criteriu_urgenta4 AS criteriu_urgenta4 ,
>>J1031074.criteriu_urgenta5 AS criteriu_urgenta5 ,
>>J1031074.criteriu_urgenta6 AS criteriu_urgenta6 ,
>>J1031074.criteriu_urgenta7 AS criteriu_urgenta7 ,
>>J1031074.criteriu_urgenta8 AS criteriu_urgenta8 ,
>>J1031074.criteriu_urgenta9a AS criteriu_urgenta9a ,
>>J1031074.criteriu_urgenta9b AS criteriu_urgenta9b ,
>>J1031074.criteriu_urgenta10 AS criteriu_urgenta10 ,
>>J1031074.criteriu_urgenta11 AS criteriu_urgenta11 ,
>>J1031074.criteriu_urgenta12 AS criteriu_urgenta12 ,
>>J1031074.criteriu_urgenta13 AS criteriu_urgenta13 ,
>>J1031074.infectie_nosocomiala AS infectie_nosocomiala ,
>>J1031074.cu_programare AS cu_programare ,
>>J1031074.stare_critica_internare AS stare_critica_internare ,
>>J1033704.id AS _id_stari_spitalizari ,
>>J1033704.nume AS id_stari_spitalizari
>>    FROM focg AS J1031074
>>   LEFT JOIN person AS J1031075 ON ( J1031075.id = J1031074.idpatient)
>>    LEFT JOIN sex AS J1031076 ON ( J1031076.id = J1031075.idsex)
>>    LEFT JOIN focg_serials AS J1031077 ON ( J1031077.id =
>>J1031074.id_focg_serials)
>>    LEFT JOIN ensuredstatustype AS J1031078 ON ( J1031078.id =
>>J1031074.idensuredstatustype)
>>    LEFT JOIN ensuredstatus AS J1031079 ON ( J1031079.id =
>>J1031074.idensuredstatus)
>>    LEFT JOIN org_units AS J1031080 ON ( J1031080.id =
>>J1031074.id_org_unit)
>>    LEFT JOIN personstate AS J1031081 ON ( J1031081.id =
>>J1031074.id_categorie_asigurat)
>>    LEFT JOIN focginterntype AS J1031082 ON ( J1031082.id =
>>J1031074.id_focg_tip_internare)
>>    LEFT JOIN focginterncrit AS J1031083 ON ( J1031083.id =
>>J1031074.id_focg_criteriu_internare)
>>    LEFT JOIN physicians AS J1031084 ON ( J1031084.id =
>>J1031074.idphysiciancurrent)
>>    LEFT JOIN personnel AS J1031085 ON ( J1031085.id =
>>J1031084.id_personnel)
>>    LEFT JOIN person AS J1031086 ON ( J1031086.id = J1031085.idperson)
>>    LEFT JOIN physicians AS J1031087 ON ( J1031087.id =
>>J1031074.idphysician_madeby)
>>    LEFT JOIN proffession AS J1031088 ON ( J1031088.id =
>>J1031074.idproffesion)
>>    LEFT JOIN educationlevel AS J1031089 ON ( J1031089.id =
>>J1031074.id_education_level)
>>    LEFT JOIN focg_situatii_speciale AS J1031090 ON ( J1031090.id =
>>J1031074.id_focg_situatii_speciale)
>>    LEFT JOIN focg_internat_prin AS J1031091 ON ( J1031091.id =
>>J1031074.id_focg_internat_prin)
>>    LEFT JOIN formulare_europene AS J1031092 ON ( J1031092.id =
>>J1031074.id_formulare_europene)
>>    LEFT JOIN focg AS J1031093 ON ( J1031093.id = J1031074.id_focg)
>>    LEFT JOIN person AS J1031094 ON ( J1031094.id =
>>J1031074.id_cnp_mama)
>>    LEFT JOIN focg_sursa_internare AS J1031095 ON ( J1031095.id =
>>J1031074.id_focg_sursa_internare)
>>    LEFT JOIN diseasecategory AS J1031096 ON ( J1031096.id =
>>J1031074.id_diseasecategory)
>>    LEFT JOIN icd10 AS J1031097 ON ( J1031097.id = J1031074.id_icd10)
>>    LEFT JOIN focg_contract_modes AS J1031098 ON ( J1031098.id =
>>J1031074.id_focg_contract_modes)
>>    LEFT JOIN focg_criterii_urgenta AS J1031099 ON ( J1031099.id =
>>J1031074.id_focg_criterii_urgenta)
>>    LEFT JOIN exceptie_bilet_internare AS J1031100 ON ( J1031100.id =
>>J1031074.id_exceptie_bilet_internare)
>>    LEFT JOIN focg_tip_externare AS J1031736 ON ( J1031736.id =
>>J1031074.id_focg_tip_externare)
>>    LEFT JOIN focg_stare_externare AS J1031737 ON ( J1031737.id =
>>J1031074.id_focg_stare_externare)
>>    LEFT JOIN physicians AS J1033295 ON ( J1033295.id =
>>J1031074.idphysician_surgeon)
>>    LEFT JOIN hospital_release_statuses AS J1033299 ON ( J1033299.id =
>>J1031074.id_hospital_release_statuses)
>>    LEFT JOIN citizenship AS J1033304 ON ( J1033304.id =
>>J1031074.id_citizenship)
>>    LEFT JOIN drg_tip_cetatenie AS J1033358 ON ( J1033358.id =
>>J1031074.id_drg_tip_cetatenie)
>>    LEFT JOIN stari_spitalizari AS J1033704 ON ( J1033704.id =
>>J1031074.id_stari_spitalizari)
>>    LEFT JOIN focgdepartment AS J1031101 ON ( J1031101.idfocg =
>>J1031074.id)
>>    LEFT JOIN department AS J1033386 ON ( J1033386.id =
>>J1031101.iddepartment)
>>    LEFT JOIN focgdepartment AS J1031102 ON ( J1031102.idfocg =
>>J1031074.id AND J1031102.validfrom < J1031101.validfrom)
>>    LEFT JOIN focgdepartment AS J1033359 ON ( J1033359.idfocg =
>>J1031074.id)
>>    LEFT JOIN department AS J1033387 ON ( J1033387.id =
>>J1033359.iddepartment)
>>    LEFT JOIN focgdepartment AS J1033360 ON ( J1033360.idfocg =
>>J1031074.id AND J1033360.validfrom > J1033359.validfrom)
>>     JOIN focgdepartment AS J1033407 ON ( J1033407.idfocg = J1031074.id
>>AND J1033407.iddepartment IN (/*Executing QueryID 476590*/SELECT
>>DISTINCT(J1034477.id) AS ret FROM department AS J1034477 JOIN
>>personnel_department AS J1034478 ON ( J1034478.id_department =
>>J1034477.id)
>>   JOIN personnel AS J1034479 ON ( J1034479.id = J1034478.id_personnel
>>AND J1034479.id_connected_user = '1')))
>>
>>  WHERE J1031102.id IS NULL AND J1033360.id IS NULL AND ( (
>>DATE(J1031101.validfrom)>= DATE('2016-05-01') AND
>>DATE(J1031101.validfrom)<= DATE('2016-05-31 00:00:00.0') ) OR (
>>J1033359.validto IS NOT NULL AND DATE(J1033359.validto)>=
>>DATE('2016-05-01') AND DATE(J1033359.validto)<= DATE('2016-05-31
>>00:00:00.0') ) OR ( J1033359.validto IS NULL AND
>>DATE(J1031101.validfrom)<= DATE('2016-05-01') ) )
>>
>>    ORDER BY J1031101.validfrom DESC
>>  LIMIT 20 OFFSET 0
>>
>>------ Original Message ------
>>From: "David Rowley" <david.rowley@2ndquadrant.com>
>>To: "Sterpu Victor" <victor@caido.ro>
>>Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
>><pgsql-general@postgresql.org>; "David G. Johnston"
>><david.g.johnston@gmail.com>
>>Sent: 9/5/2016 10:04:54 AM
>>Subject: Re: [GENERAL] Slow query when the select list is big
>>
>>>On 9 May 2016 at 18:46, David G. Johnston
>>><david.g.johnston@gmail.com> wrote:
>>>>  On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
>>>>>
>>>>>  Yes but it is very big.
>>>>>  I don't understand why the select list is influencing the CPU
>>>>>usage.
>>>>>  I was expecting that only the join and where clauses would
>>>>>influence CPU.
>>>>>
>>>>
>>>>  PostgreSQL is smart enough to optimize away stuff that it knows
>>>>doesn't
>>>>  impact the final query result.
>>>
>>>To be more accurate with what David is saying, PostgreSQL will remove
>>>unused LEFT JOINed relations where the left joined relation can be
>>>proved to not duplicate rows from the right hand side. It would just
>>>be a matter of comparing the EXPLAINs from the query with all the
>>>SELECT items to the one with the single SELECT item to prove that
>>>this
>>>is what's happening.
>>>
>>>Please also note that this only occurs with LEFT JOINs
>>>
>>>It would also be quite helpful for people if you were to include a
>>>copy of the query. It's impossible to reverse engineer what that is
>>>from this EXPLAIN output. I see that your using a windowing function
>>>and performing a LIMIT 1, there may be ways to improve that just by
>>>selecting the single highest j1031101.validfrom row and performing
>>>the
>>>joins to the other table on that single row, but that will depend on
>>>which windowing function you're using as the function may require the
>>>other rows in the window frame to calculate the correct result.
>>>
>>>--
>>>  David Rowley http://www.2ndQuadrant.com/
>>>  PostgreSQL Development, 24x7 Support, Training & Services
>>
>>
>>
>>-- Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
>>To make changes to your subscription:
>>http://www.postgresql.org/mailpref/pgsql-general
>



Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
I tested it now, EXPLAIN ANALYZE is not showing LEFT JOIN at all if I
don't select from the joined tables.
Now is clear why the query is so mutch more efficient when I select less
data.

Thank you

------ Original Message ------
From: "David Rowley" <david.rowley@2ndquadrant.com>
To: "Sterpu Victor" <victor@caido.ro>
Cc: "Rob Imig" <rimig88@gmail.com>; "PostgreSQL General"
<pgsql-general@postgresql.org>; "David G. Johnston"
<david.g.johnston@gmail.com>
Sent: 9/5/2016 10:04:54 AM
Subject: Re: [GENERAL] Slow query when the select list is big

>On 9 May 2016 at 18:46, David G. Johnston <david.g.johnston@gmail.com>
>wrote:
>>  On Sunday, May 8, 2016, Sterpu Victor <victor@caido.ro> wrote:
>>>
>>>  Yes but it is very big.
>>>  I don't understand why the select list is influencing the CPU usage.
>>>  I was expecting that only the join and where clauses would influence
>>>CPU.
>>>
>>
>>  PostgreSQL is smart enough to optimize away stuff that it knows
>>doesn't
>>  impact the final query result.
>
>To be more accurate with what David is saying, PostgreSQL will remove
>unused LEFT JOINed relations where the left joined relation can be
>proved to not duplicate rows from the right hand side. It would just
>be a matter of comparing the EXPLAINs from the query with all the
>SELECT items to the one with the single SELECT item to prove that this
>is what's happening.
>
>Please also note that this only occurs with LEFT JOINs
>
>It would also be quite helpful for people if you were to include a
>copy of the query. It's impossible to reverse engineer what that is
>from this EXPLAIN output. I see that your using a windowing function
>and performing a LIMIT 1, there may be ways to improve that just by
>selecting the single highest j1031101.validfrom row and performing the
>joins to the other table on that single row, but that will depend on
>which windowing function you're using as the function may require the
>other rows in the window frame to calculate the correct result.
>
>--
>  David Rowley http://www.2ndQuadrant.com/
>  PostgreSQL Development, 24x7 Support, Training & Services



Re: Slow query when the select list is big

From
Karl Czajkowski
Date:
On May 09, Sterpu Victor modulated:
> I tested it now, EXPLAIN ANALYZE is not showing LEFT JOIN at all if
> I don't select from the joined tables.
> Now is clear why the query is so mutch more efficient when I select
> less data.
>
> Thank you
>

With so many joins, you may want to experiment with postgresql
parameter tuning.  These parameters in particular can have a
significant impact on the plan choice and execution time:

   work_mem
   effective_cache_size

   from_collapse_limit
   join_collapse_limit

   geqo_threshold
   geqo_effort

Setting these to appropriately large values can make analytic queries
run much faster.  Of course, setting them too high can also make for
very bad plans which cause the DB server to over subscribe its memory
and start swapping...  it requires a bit of reading and a bit of
experimentation to find ideal settings for your environment.


Karl



Re: Slow query when the select list is big

From
"Sterpu Victor"
Date:
It works fine now, on my test server execution time went down from 6.4
seconds to 1.4 seconds and on the production server went down from 3.2
sec to 600ms.
To optimize the query I changed the order of some joins(the joins that
where used to limit rows are at the begining of the query)
I tried some of these parameters, I will try all tomorow.

------ Original Message ------
From: "Karl Czajkowski" <karlcz@isi.edu>
To: "Sterpu Victor" <victor@caido.ro>
Cc: "PostgreSQL General" <pgsql-general@postgresql.org>
Sent: 9/5/2016 8:47:12 PM
Subject: Re: Slow query when the select list is big

>On May 09, Sterpu Victor modulated:
>>  I tested it now, EXPLAIN ANALYZE is not showing LEFT JOIN at all if
>>  I don't select from the joined tables.
>>  Now is clear why the query is so mutch more efficient when I select
>>  less data.
>>
>>  Thank you
>>
>
>With so many joins, you may want to experiment with postgresql
>parameter tuning. These parameters in particular can have a
>significant impact on the plan choice and execution time:
>
>    work_mem
>    effective_cache_size
>
>    from_collapse_limit
>    join_collapse_limit
>
>    geqo_threshold
>    geqo_effort
>
>Setting these to appropriately large values can make analytic queries
>run much faster. Of course, setting them too high can also make for
>very bad plans which cause the DB server to over subscribe its memory
>and start swapping... it requires a bit of reading and a bit of
>experimentation to find ideal settings for your environment.
>
>
>Karl
>