Na ja, syntaktisch ist "select distinct" schon glatter. Also aus Sicht der SQL-Syntax und für Wartbarkeit bei großen Sachen würde ich mich auch erst mal dafür entscheiden. Allerdings:
Ich habe beides mal an einer kurzen Tabelle getestet. Und bin erschrocken!
Code:
barrett=# explain analyze select distinct basisbefund from histotyp;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Unique (cost=70.69..75.38 rows=320 width=19) (actual time=12.836..16.953 rows=195 loops=1)
-> Sort (cost=70.69..73.03 rows=938 width=19) (actual time=12.832..13.050 rows=775 loops=1)
Sort Key: basisbefund
-> Seq Scan on histotyp (cost=0.00..24.38 rows=938 width=19) (actual time=0.013..1.157 rows=775 loops=1)
Total runtime: 17.155 ms
(5 Zeilen)
gegenüber
Code:
barrett=# explain analyze select basisbefund from histotyp group by basisbefund;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
HashAggregate (cost=26.73..26.73 rows=320 width=19) (actual time=5.010..5.195 rows=195 loops=1)
-> Seq Scan on histotyp (cost=0.00..24.38 rows=938 width=19) (actual time=0.009..0.683 rows=775 loops=1)
Total runtime: 5.309 ms
(3 Zeilen)
Das ist gerade mal 1/3 der Ausführungszeit!
Für mich heißt das: So eine Anfrage teste ich erst mal, bevor ich mich auf eins festlege. (Hier war es Postgres. Die Ergebnisse können stark schwanken je nach rdbms)
Lesezeichen