Solution not found as I thought. I integrated the query in a view and the query plan became very bad once again.
The reason is that when I am using the view I have the joins in a differerent order.


Does anyone have an idea to solve this.

Sebastian

a) bad order but the one I have in my application
explain analyze
SELECT taufgaben.fid AS taufgaben_fid, taufgaben.fprojekt_id AS
taufgaben_fprojekt_id, taufgaben.fnummer AS taufgaben_fnummer,
taufgaben.fbudget AS taufgaben_fbudget,
taufgaben.ftyp AS taufgaben_ftyp,
taufgaben.fberechnungsart AS taufgaben_fberechnungsart,
taufgaben.fverrechnung_extern AS taufgaben_fverrechnung_extern,
taufgaben.fverrechnungsbasis AS taufgaben_fverrechnungsbasis,
taufgaben.fstatus AS taufgaben_fstatus,
taufgaben.fkurzbeschreibung AS
taufgaben_fkurzbeschreibung,
taufgaben.fansprechpartner AS
taufgaben_fansprechpartner,
taufgaben.fanforderer AS taufgaben_fanforderer,
taufgaben.fstandort_id AS taufgaben_fstandort_id,
taufgaben.fwunschtermin AS taufgaben_fwunschtermin,
taufgaben.fstarttermin AS taufgaben_fstarttermin,
taufgaben.fgesamtaufwand AS taufgaben_fgesamtaufwand,
taufgaben.fistaufwand AS taufgaben_fistaufwand,
taufgaben.fprio AS taufgaben_fprio,
taufgaben.ftester AS taufgaben_ftester,
taufgaben.ffaellig AS taufgaben_ffaellig,
taufgaben.flevel AS taufgaben_flevel,
taufgaben.fkategorie AS taufgaben_fkategorie,
taufgaben.feintragbearbeitung AS taufgaben_feintragbearbeitung,
taufgaben.fbearbeitungsstatus AS taufgaben_fbearbeitungsstatus,
taufgaben.fsolllimit AS taufgaben_fsolllimit,
taufgaben.fistlimit AS taufgaben_fistlimit,
taufgaben.fpauschalbetrag AS taufgaben_fpauschalbetrag,
taufgaben.frechnungslaeufe_id AS taufgaben_frechnungslaeufe_id,
taufgaben.fzuberechnen AS taufgaben_fzuberechnen,
taufgaben.floesungsbeschreibung AS taufgaben_floesungsbeschreibung,
taufgaben.ffehlerbeschreibung AS taufgaben_ffehlerbeschreibung,
taufgaben.faufgabenstellung AS taufgaben_faufgabenstellung,
taufgaben.fkritischeaenderungen AS taufgaben_fkritischeaenderungen,
taufgaben.fbdeaufgabenersteller_id AS taufgaben_fbdeaufgabenersteller_id,
taufgaben.fzufaktorieren AS taufgaben_fzufaktorieren,
taufgaben.fisdirty AS taufgaben_fisdirty,
taufgaben.fnf_kunde_stunden AS taufgaben_fnf_kunde_stunden,
taufgaben.fzf_kunde_stunden AS taufgaben_fzf_kunde_stunden,
taufgaben.fbf_kunde_stunden AS taufgaben_fbf_kunde_stunden,
taufgaben.fnf_kunde_betrag AS taufgaben_fnf_kunde_betrag,
taufgaben.fzf_kunde_betrag AS taufgaben_fzf_kunde_betrag,
taufgaben.fbf_kunde_betrag AS taufgaben_fbf_kunde_betrag,
taufgaben.fgesamt_brutto_stunden AS taufgaben_fgesamt_brutto_stunden,
taufgaben.fgesamt_brutto_betrag AS taufgaben_fgesamt_brutto_betrag,
taufgaben.fhinweisgesendet AS taufgaben_fhinweisgesendet,
taufgaben.fwarnunggesendet AS taufgaben_fwarnunggesendet,
taufgaben.fnfgesamtaufwand AS taufgaben_fnfgesamtaufwand,
taufgaben.fnf_netto_stunden AS taufgaben_fnf_netto_stunden,
taufgaben.fnf_brutto_stunden AS taufgaben_fnf_brutto_stunden,
taufgaben.fnfhinweisgesendet AS taufgaben_fnfhinweisgesendet,
taufgaben.fnfwarnunggesendet AS taufgaben_fnfwarnunggesendet,
taufgaben.fhatzeiten AS taufgaben_fhatzeiten,
taufgaben.fnichtpublicrechnungsfaehig AS taufgaben_fnichtpublicrechnungsfaehig,
taufgaben.fnichtpublicrechnungsfaehigbetrag AS taufgaben_fnichtpublicrechnungsfaehigbetrag,
taufgaben.fnichtberechenbar AS taufgaben_fnichtberechenbar,
taufgaben.fnichtberechenbarbetrag AS taufgaben_fnichtberechenbarbetrag,
taufgaben.finternertester AS taufgaben_finternertester,
taufgaben.finterngetestet AS taufgaben_finterngetestet,
taufgaben.fanzahlbearbeiter AS taufgaben_fanzahlbearbeiter,
patchdaten.faufgaben_id AS pataid
,vprojekt.*
FROM taufgaben LEFT JOIN (
SELECT DISTINCT taufgaben_patches.faufgaben_id
FROM taufgaben_patches
) patchdaten ON taufgaben.fid = patchdaten.faufgaben_id
JOIN vprojekt ON taufgaben.fprojekt_id = vprojekt.tprojekte_fid


join taufgaben_mitarbeiter am on taufgaben.fid = am.faufgaben_id

where
am.fmitarbeiter_id = 54
and
taufgaben.fbearbeitungsstatus <> 2


"Nested Loop (cost=1349.13..1435.29 rows=1 width=2541) (actual time=1640.000..3687.000 rows=62 loops=1)"
" Join Filter: ("inner".fid = "outer".faufgaben_id)"
" -> Index Scan using idx_tauf_mit_mitid on taufgaben_mitarbeiter am (cost=0.00..80.65 rows=35 width=4) (actual time=0.000..0.000 rows=765 loops=1)"
" Index Cond: (fmitarbeiter_id = 54)"
" -> Materialize (cost=1349.13..1349.20 rows=7 width=2541) (actual time=0.531..1.570 rows=1120 loops=765)"
" -> Merge Join (cost=1343.42..1349.13 rows=7 width=2541) (actual time=406.000..515.000 rows=1120 loops=1)"
" Merge Cond: ("outer".fid = "inner".fprojekt_id)"
" -> Sort (cost=130.89..130.90 rows=6 width=1494) (actual time=203.000..203.000 rows=876 loops=1)"
" Sort Key: tprojekte.fid"
" -> Merge Join (cost=130.52..130.81 rows=6 width=1494) (actual time=156.000..187.000 rows=876 loops=1)"
" Merge Cond: ("outer".fkunden_id = "inner".fid)"
" -> Sort (cost=127.06..127.07 rows=6 width=1455) (actual time=156.000..156.000 rows=876 loops=1)"
" Sort Key: tkunden_kst.fkunden_id"
" -> Merge Join (cost=126.34..126.98 rows=6 width=1455) (actual time=109.000..125.000 rows=876 loops=1)"
" Merge Cond: ("outer".fprojektleiter_id = "inner".fid)"
" -> Sort (cost=118.56..118.58 rows=9 width=580) (actual time=109.000..109.000 rows=876 loops=1)"
" Sort Key: tprojekte.fprojektleiter_id"
" -> Merge Join (cost=117.88..118.42 rows=9 width=580) (actual time=62.000..93.000 rows=876 loops=1)"
" Merge Cond: ("outer".fkunden_kst_id = "inner".fid)"
" -> Sort (cost=114.60..114.68 rows=31 width=508) (actual time=62.000..62.000 rows=876 loops=1)"
" Sort Key: tprojekte.fkunden_kst_id"
" -> Merge Join (cost=109.10..113.84 rows=31 width=508) (actual time=31.000..62.000 rows=876 loops=1)"
" Merge Cond: ("outer".fid = "inner".fkostentraeger_id)"
" -> Sort (cost=13.40..13.41 rows=7 width=162) (actual time=0.000..0.000 rows=158 loops=1)"
" Sort Key: tkostentraeger.fid"
" -> Merge Join (cost=3.06..13.30 rows=7 width=162) (actual time=0.000..0.000 rows=158 loops=1)"
" Merge Cond: ("outer".fkostenstellen_id = "inner".fid)"
" -> Index Scan using idx_kostenstellen_id on tkostentraeger (cost=0.00..9.74 rows=158 width=55) (actual time=0.000..0.000 rows=158 loops=1)"
" -> Sort (cost=3.06..3.08 rows=7 width=119) (actual time=0.000..0.000 rows=158 loops=1)"
" Sort Key: tkostenstellen.fid"
" -> Merge Join (cost=2.76..2.96 rows=7 width=119) (actual time=0.000..0.000 rows=19 loops=1)"
" Merge Cond: ("outer".fabteilungen_id = "inner".fid)"
" -> Sort (cost=1.59..1.64 rows=19 width=55) (actual time=0.000..0.000 rows=19 loops=1)"
" Sort Key: tkostenstellen.fabteilungen_id"
" -> Seq Scan on tkostenstellen (cost=0.00..1.19 rows=19 width=55) (actual time=0.000..0.000 rows=19 loops=1)"
" -> Sort (cost=1.17..1.19 rows=7 width=76) (actual time=0.000..0.000 rows=19 loops=1)"
" Sort Key: tabteilungen.fid"
" -> Seq Scan on tabteilungen (cost=0.00..1.07 rows=7 width=76) (actual time=0.000..0.000 rows=7 loops=1)"
" -> Sort (cost=95.71..97.90 rows=878 width=354) (actual time=31.000..46.000 rows=877 loops=1)"
" Sort Key: tprojekte.fkostentraeger_id"
" -> Seq Scan on tprojekte (cost=0.00..52.78 rows=878 width=354) (actual time=0.000..15.000 rows=878 loops=1)"
" -> Sort (cost=3.28..3.42 rows=58 width=80) (actual time=0.000..0.000 rows=892 loops=1)"
" Sort Key: tkunden_kst.fid"
" -> Seq Scan on tkunden_kst (cost=0.00..1.58 rows=58 width=80) (actual time=0.000..0.000 rows=58 loops=1)"
" -> Sort (cost=7.78..8.05 rows=109 width=883) (actual time=0.000..0.000 rows=950 loops=1)"
" Sort Key: tuser.fid"
" -> Seq Scan on tuser (cost=0.00..4.09 rows=109 width=883) (actual time=0.000..0.000 rows=109 loops=1)"
" -> Sort (cost=3.46..3.56 rows=40 width=51) (actual time=0.000..0.000 rows=887 loops=1)"
" Sort Key: tkunden.fid"
" -> Seq Scan on tkunden (cost=0.00..2.40 rows=40 width=51) (actual time=0.000..0.000 rows=40 loops=1)"
" -> Sort (cost=1212.53..1215.33 rows=1120 width=1047) (actual time=203.000..203.000 rows=1120 loops=1)"
" Sort Key: taufgaben.fprojekt_id"
" -> Merge Left Join (cost=1148.83..1155.80 rows=1120 width=1047) (actual time=140.000..203.000 rows=1120 loops=1)"
" Merge Cond: ("outer".fid = "inner".faufgaben_id)"
" -> Sort (cost=910.60..913.40 rows=1120 width=1043) (actual time=78.000..78.000 rows=1120 loops=1)"
" Sort Key: taufgaben.fid"
" -> Seq Scan on taufgaben (cost=0.00..853.88 rows=1120 width=1043) (actual time=0.000..78.000 rows=1120 loops=1)"
" Filter: (fbearbeitungsstatus <> 2)"
" -> Sort (cost=238.23..238.73 rows=200 width=4) (actual time=62.000..93.000 rows=4773 loops=1)"
" Sort Key: patchdaten.faufgaben_id"
" -> Subquery Scan patchdaten (cost=0.00..230.59 rows=200 width=4) (actual time=0.000..47.000 rows=4773 loops=1)"
" -> Unique (cost=0.00..228.59 rows=200 width=4) (actual time=0.000..0.000 rows=4773 loops=1)"
" -> Index Scan using idx_aufpa_aufgabeid on taufgaben_patches (cost=0.00..212.74 rows=6340 width=4) (actual time=0.000..0.000 rows=6340 loops=1)"
"Total runtime: 3703.000 ms"





good order

explain analyze
SELECT taufgaben.fid AS taufgaben_fid, taufgaben.fprojekt_id AS
taufgaben_fprojekt_id, taufgaben.fnummer AS taufgaben_fnummer,
taufgaben.fbudget AS taufgaben_fbudget,
taufgaben.ftyp AS taufgaben_ftyp,
taufgaben.fberechnungsart AS taufgaben_fberechnungsart,
taufgaben.fverrechnung_extern AS taufgaben_fverrechnung_extern,
taufgaben.fverrechnungsbasis AS taufgaben_fverrechnungsbasis,
taufgaben.fstatus AS taufgaben_fstatus,
taufgaben.fkurzbeschreibung AS
taufgaben_fkurzbeschreibung,
taufgaben.fansprechpartner AS
taufgaben_fansprechpartner,
taufgaben.fanforderer AS taufgaben_fanforderer,
taufgaben.fstandort_id AS taufgaben_fstandort_id,
taufgaben.fwunschtermin AS taufgaben_fwunschtermin,
taufgaben.fstarttermin AS taufgaben_fstarttermin,
taufgaben.fgesamtaufwand AS taufgaben_fgesamtaufwand,
taufgaben.fistaufwand AS taufgaben_fistaufwand,
taufgaben.fprio AS taufgaben_fprio,
taufgaben.ftester AS taufgaben_ftester,
taufgaben.ffaellig AS taufgaben_ffaellig,
taufgaben.flevel AS taufgaben_flevel,
taufgaben.fkategorie AS taufgaben_fkategorie,
taufgaben.feintragbearbeitung AS taufgaben_feintragbearbeitung,
taufgaben.fbearbeitungsstatus AS taufgaben_fbearbeitungsstatus,
taufgaben.fsolllimit AS taufgaben_fsolllimit,
taufgaben.fistlimit AS taufgaben_fistlimit,
taufgaben.fpauschalbetrag AS taufgaben_fpauschalbetrag,
taufgaben.frechnungslaeufe_id AS taufgaben_frechnungslaeufe_id,
taufgaben.fzuberechnen AS taufgaben_fzuberechnen,
taufgaben.floesungsbeschreibung AS taufgaben_floesungsbeschreibung,
taufgaben.ffehlerbeschreibung AS taufgaben_ffehlerbeschreibung,
taufgaben.faufgabenstellung AS taufgaben_faufgabenstellung,
taufgaben.fkritischeaenderungen AS taufgaben_fkritischeaenderungen,
taufgaben.fbdeaufgabenersteller_id AS taufgaben_fbdeaufgabenersteller_id,
taufgaben.fzufaktorieren AS taufgaben_fzufaktorieren,
taufgaben.fisdirty AS taufgaben_fisdirty,
taufgaben.fnf_kunde_stunden AS taufgaben_fnf_kunde_stunden,
taufgaben.fzf_kunde_stunden AS taufgaben_fzf_kunde_stunden,
taufgaben.fbf_kunde_stunden AS taufgaben_fbf_kunde_stunden,
taufgaben.fnf_kunde_betrag AS taufgaben_fnf_kunde_betrag,
taufgaben.fzf_kunde_betrag AS taufgaben_fzf_kunde_betrag,
taufgaben.fbf_kunde_betrag AS taufgaben_fbf_kunde_betrag,
taufgaben.fgesamt_brutto_stunden AS taufgaben_fgesamt_brutto_stunden,
taufgaben.fgesamt_brutto_betrag AS taufgaben_fgesamt_brutto_betrag,
taufgaben.fhinweisgesendet AS taufgaben_fhinweisgesendet,
taufgaben.fwarnunggesendet AS taufgaben_fwarnunggesendet,
taufgaben.fnfgesamtaufwand AS taufgaben_fnfgesamtaufwand,
taufgaben.fnf_netto_stunden AS taufgaben_fnf_netto_stunden,
taufgaben.fnf_brutto_stunden AS taufgaben_fnf_brutto_stunden,
taufgaben.fnfhinweisgesendet AS taufgaben_fnfhinweisgesendet,
taufgaben.fnfwarnunggesendet AS taufgaben_fnfwarnunggesendet,
taufgaben.fhatzeiten AS taufgaben_fhatzeiten,
taufgaben.fnichtpublicrechnungsfaehig AS taufgaben_fnichtpublicrechnungsfaehig,
taufgaben.fnichtpublicrechnungsfaehigbetrag AS taufgaben_fnichtpublicrechnungsfaehigbetrag,
taufgaben.fnichtberechenbar AS taufgaben_fnichtberechenbar,
taufgaben.fnichtberechenbarbetrag AS taufgaben_fnichtberechenbarbetrag,
taufgaben.finternertester AS taufgaben_finternertester,
taufgaben.finterngetestet AS taufgaben_finterngetestet,
taufgaben.fanzahlbearbeiter AS taufgaben_fanzahlbearbeiter,
patchdaten.faufgaben_id AS pataid
,vprojekt.*
FROM taufgaben LEFT JOIN (
SELECT DISTINCT taufgaben_patches.faufgaben_id
FROM taufgaben_patches
) patchdaten ON taufgaben.fid = patchdaten.faufgaben_id


join taufgaben_mitarbeiter am on taufgaben.fid = am.faufgaben_id

JOIN vprojekt ON taufgaben.fprojekt_id = vprojekt.tprojekte_fid


where am.fmitarbeiter_id = 54 and taufgaben.fbearbeitungsstatus <> 2

"Merge Join (cost=1371.38..1371.45 rows=1 width=2541) (actual time=422.000..438.000 rows=62 loops=1)"
" Merge Cond: ("outer".fprojekt_id = "inner".fid)"
" -> Sort (cost=1240.49..1240.51 rows=7 width=1047) (actual time=219.000..219.000 rows=62 loops=1)"
" Sort Key: taufgaben.fprojekt_id"
" -> Merge Join (cost=1230.38..1240.39 rows=7 width=1047) (actual time=157.000..219.000 rows=62 loops=1)"
" Merge Cond: ("outer".fid = "inner".faufgaben_id)"
" -> Merge Left Join (cost=1148.83..1155.80 rows=1120 width=1047) (actual time=141.000..203.000 rows=1118 loops=1)"
" Merge Cond: ("outer".fid = "inner".faufgaben_id)"
" -> Sort (cost=910.60..913.40 rows=1120 width=1043) (actual time=94.000..94.000 rows=1118 loops=1)"
" Sort Key: taufgaben.fid"
" -> Seq Scan on taufgaben (cost=0.00..853.88 rows=1120 width=1043) (actual time=0.000..94.000 rows=1120 loops=1)"
" Filter: (fbearbeitungsstatus <> 2)"
" -> Sort (cost=238.23..238.73 rows=200 width=4) (actual time=47.000..47.000 rows=4773 loops=1)"
" Sort Key: patchdaten.faufgaben_id"
" -> Subquery Scan patchdaten (cost=0.00..230.59 rows=200 width=4) (actual time=0.000..47.000 rows=4773 loops=1)"
" -> Unique (cost=0.00..228.59 rows=200 width=4) (actual time=0.000..31.000 rows=4773 loops=1)"
" -> Index Scan using idx_aufpa_aufgabeid on taufgaben_patches (cost=0.00..212.74 rows=6340 width=4) (actual time=0.000..15.000 rows=6340 loops=1)"
" -> Sort (cost=81.54..81.63 rows=35 width=4) (actual time=16.000..16.000 rows=765 loops=1)"
" Sort Key: am.faufgaben_id"
" -> Index Scan using idx_tauf_mit_mitid on taufgaben_mitarbeiter am (cost=0.00..80.65 rows=35 width=4) (actual time=0.000..16.000 rows=765 loops=1)"
" Index Cond: (fmitarbeiter_id = 54)"
" -> Sort (cost=130.89..130.90 rows=6 width=1494) (actual time=203.000..203.000 rows=916 loops=1)"
" Sort Key: tprojekte.fid"
" -> Merge Join (cost=130.52..130.81 rows=6 width=1494) (actual time=156.000..203.000 rows=876 loops=1)"
" Merge Cond: ("outer".fkunden_id = "inner".fid)"
" -> Sort (cost=127.06..127.07 rows=6 width=1455) (actual time=156.000..156.000 rows=876 loops=1)"
" Sort Key: tkunden_kst.fkunden_id"
" -> Merge Join (cost=126.34..126.98 rows=6 width=1455) (actual time=110.000..141.000 rows=876 loops=1)"
" Merge Cond: ("outer".fprojektleiter_id = "inner".fid)"
" -> Sort (cost=118.56..118.58 rows=9 width=580) (actual time=110.000..110.000 rows=876 loops=1)"
" Sort Key: tprojekte.fprojektleiter_id"
" -> Merge Join (cost=117.88..118.42 rows=9 width=580) (actual time=63.000..94.000 rows=876 loops=1)"
" Merge Cond: ("outer".fkunden_kst_id = "inner".fid)"
" -> Sort (cost=114.60..114.68 rows=31 width=508) (actual time=63.000..63.000 rows=876 loops=1)"
" Sort Key: tprojekte.fkunden_kst_id"
" -> Merge Join (cost=109.10..113.84 rows=31 width=508) (actual time=31.000..63.000 rows=876 loops=1)"
" Merge Cond: ("outer".fid = "inner".fkostentraeger_id)"
" -> Sort (cost=13.40..13.41 rows=7 width=162) (actual time=0.000..0.000 rows=158 loops=1)"
" Sort Key: tkostentraeger.fid"
" -> Merge Join (cost=3.06..13.30 rows=7 width=162) (actual time=0.000..0.000 rows=158 loops=1)"
" Merge Cond: ("outer".fkostenstellen_id = "inner".fid)"
" -> Index Scan using idx_kostenstellen_id on tkostentraeger (cost=0.00..9.74 rows=158 width=55) (actual time=0.000..0.000 rows=158 loops=1)"
" -> Sort (cost=3.06..3.08 rows=7 width=119) (actual time=0.000..0.000 rows=158 loops=1)"
" Sort Key: tkostenstellen.fid"
" -> Merge Join (cost=2.76..2.96 rows=7 width=119) (actual time=0.000..0.000 rows=19 loops=1)"
" Merge Cond: ("outer".fabteilungen_id = "inner".fid)"
" -> Sort (cost=1.59..1.64 rows=19 width=55) (actual time=0.000..0.000 rows=19 loops=1)"
" Sort Key: tkostenstellen.fabteilungen_id"
" -> Seq Scan on tkostenstellen (cost=0.00..1.19 rows=19 width=55) (actual time=0.000..0.000 rows=19 loops=1)"
" -> Sort (cost=1.17..1.19 rows=7 width=76) (actual time=0.000..0.000 rows=19 loops=1)"
" Sort Key: tabteilungen.fid"
" -> Seq Scan on tabteilungen (cost=0.00..1.07 rows=7 width=76) (actual time=0.000..0.000 rows=7 loops=1)"
" -> Sort (cost=95.71..97.90 rows=878 width=354) (actual time=31.000..31.000 rows=877 loops=1)"
" Sort Key: tprojekte.fkostentraeger_id"
" -> Seq Scan on tprojekte (cost=0.00..52.78 rows=878 width=354) (actual time=0.000..31.000 rows=878 loops=1)"
" -> Sort (cost=3.28..3.42 rows=58 width=80) (actual time=0.000..0.000 rows=892 loops=1)"
" Sort Key: tkunden_kst.fid"
" -> Seq Scan on tkunden_kst (cost=0.00..1.58 rows=58 width=80) (actual time=0.000..0.000 rows=58 loops=1)"
" -> Sort (cost=7.78..8.05 rows=109 width=883) (actual time=0.000..0.000 rows=950 loops=1)"
" Sort Key: tuser.fid"
" -> Seq Scan on tuser (cost=0.00..4.09 rows=109 width=883) (actual time=0.000..0.000 rows=109 loops=1)"
" -> Sort (cost=3.46..3.56 rows=40 width=51) (actual time=0.000..0.000 rows=887 loops=1)"
" Sort Key: tkunden.fid"
" -> Seq Scan on tkunden (cost=0.00..2.40 rows=40 width=51) (actual time=0.000..0.000 rows=40 loops=1)"
"Total runtime: 438.000 ms"




---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Reply via email to