Hello CTutte,
thanks for your concern, yes the servers are identical in ram, cpu and disk speed. Exactly two clones.
Yes, sure to send the DDL of the table.
I said that I notice a clear difference in the select max part, if I put a fixed value in its place, things change sharply.
CREATE TABLE TABLE1 (
FLDTB1_0010 numeric(5) NOT NULL,
FLDTB1_0070 varchar(3) NOT NULL,
FLDTB1_1051 varchar(8) NULL,
FLDTB1_1052 varchar(8) NULL,
FLDTB1_4111 timestamp NULL,
CONSTRAINT TABLE1_pkey PRIMARY KEY (FLDTB1_0010, FLDTB1_0070)
);
CREATE TABLE TABLE2 (
FLDTB2_0010 numeric(5) NOT NULL,
FLDTB2_0070 varchar(3) NOT NULL,
FLDTB2_0293 timestamp NOT NULL,
FLDTB2_3150_1 numeric(15,8) NULL,
CONSTRAINT TABLE2_pkey PRIMARY KEY (FLDTB2_0010, FLDTB2_0070, FLDTB2_0293)
);
SELECT PREZZO1, PREZZO2 FROM
( SELECT FLDTB1_0070 PREZZO1, A.FLDTB2_3150_1 PREZZO2 FROM TABLE2 A
JOIN TABLE1 ON FLDTB1_0010 = FLDTB2_0010 AND FLDTB1_0070 = FLDTB2_0070
WHERE A.FLDTB2_0010 = ‘01010’ AND A.FLDTB2_0293 = (SELECT MAX(B.FLDTB2_0293)
FROM TABLE2 B
WHERE A.FLDTB2_0010 = B.FLDTB2_0010 AND A.FLDTB2_0070 = B.FLDTB2_0070 ) ) B
GROUP BY PREZZO1, PREZZO2;
I also send you the query analysis …
Group (cost=77163.67…77168.92 rows=700 width=11) (actual time=2383.257…2383.274 rows=40 loops=1)
Group Key: TABLE1.FLDTB1_0070, a.FLDTB2_3150_1
→ Sort (cost=77163.67…77165.42 rows=700 width=11) (actual time=2383.254…2383.260 rows=40 loops=1)
Sort Key: TABLE1.FLDTB1_0070, a.FLDTB2_3150_1
Sort Method: quicksort Memory: 26kB
→ Merge Join (cost=77090.47…77130.59 rows=700 width=11) (actual time=2383.052…2383.208 rows=40 loops=1)
Merge Cond: ((TABLE1.FLDTB1_0070)::text = (a.FLDTB2_0070)::text)
→ Index Only Scan using TABLE1_pkey on TABLE1 (cost=0.14…15.56 rows=184 width=9) (actual time=0.091…0.185 rows=183 loops=1)
Index Cond: (FLDTB1_0010 = ‘1010’::numeric)
Heap Fetches: 183
→ Sort (cost=77090.24…77091.99 rows=700 width=16) (actual time=2382.947…2382.952 rows=41 loops=1)
Sort Key: a.FLDTB2_0070
Sort Method: quicksort Memory: 27kB
→ Seq Scan on TABLE2 a (cost=0.00…77057.16 rows=700 width=16) (actual time=10.726…2382.834 rows=41 loops=1)
Filter: ((FLDTB2_0010 = ‘1010’::numeric) AND (FLDTB2_0293 = (SubPlan 2)))
Rows Removed by Filter: 140038
SubPlan 2
→ Result (cost=0.47…0.50 rows=1 width=8) (actual time=0.016…0.016 rows=1 loops=140079)
InitPlan 1 (returns $2)
→ Limit (cost=0.42…0.47 rows=1 width=8) (actual time=0.016…0.016 rows=1 loops=140079)
→ Index Only Scan Backward using TABLE2_pkey on TABLE2 b (cost=0.42…180.74 rows=3502 width=8) (actual time=0.016…0.016 rows=1 loops=140079)
Index Cond: ((FLDTB2_0010 = a.FLDTB2_0010) AND (FLDTB2_0070 = (a.FLDTB2_0070)::text) AND (FLDTB2_0293 IS NOT NULL))
Heap Fetches: 140066
Planning Time: 1.092 ms
Execution Time: 2383.452 ms
A thousand thanks