Postgresでのテストですので参考まで
SQL文は上から順に(1)(2)(3)(4)としました。
※SQL文の1番目のものは、PostgresではSQLエラーでした。ですので対象外といたしました。
postgresでは * のgroup byができませんし、出力順が保証されていません。
検査対象件数は1万件としました。(インデックスは有りません)
結果は、costを実行時間とすると、早いもの順で(4)<(2)<(3)となります。
特に(3)は、条件の部分でSQLその都度実行される事により、他の2つより大変時間が掛かる結果となっています。
EXPLAIN ANALYZEの結果
Execution time:実行時間です(ミリ秒)
sql
1 NO .2
2 QUERY PLAN
3 ----------------------------------------------------------------------------------------------------------------------------------------
4 Hash Join ( cost = 230.30 . .1015 .16 rows = 56 width = 12 ) ( actual time = 48.100 . .85 .661 rows = 3383 loops = 1 )
5 Hash Cond: ( ( product . type = product_1 . type ) AND ( product . price = ( max ( product_1 . price ) ) ) )
6 - > Seq Scan on product ( cost = 0.00 . .167 .20 rows = 11220 width = 12 ) ( actual time = 0.017 . .15 .101 rows = 10001 loops = 1 )
7 - > Hash ( cost = 227.30 . .227 .30 rows = 200 width = 8 ) ( actual time = 48.040 . .48 .040 rows = 3334 loops = 1 )
8 Buckets: 4096 ( originally 1024 ) Batches: 1 ( originally 1 ) Memory Usage : 163 kB
9 - > HashAggregate ( cost = 223.30 . .225 .30 rows = 200 width = 8 ) ( actual time = 36.361 . .42 .345 rows = 3334 loops = 1 )
10 Group Key : product_1 . type
11 - > Seq Scan on product product_1 ( cost = 0.00 . .167 .20 rows = 11220 width = 8 ) ( actual time = 0.007 . .16 .087 rows = 10001 loops = 1 )
12 Planning time : 0.329 ms
13 Execution time : 90.400 ms
14 ( 10 rows )
15
16 NO .3
17 QUERY PLAN
18 --------------------------------------------------------------------------------------------------------------------------
19 Seq Scan on product ( cost = 0.00 . .2192583 .25 rows = 56 width = 12 ) ( actual time = 4.054 . .12745 .577 rows = 3383 loops = 1 )
20 Filter: ( price = ( SubPlan 1 ) )
21 Rows Removed by Filter: 6618
22 SubPlan 1
23 - > Aggregate ( cost = 195.39 . .195 .40 rows = 1 width = 4 ) ( actual time = 1.235 . .1 .246 rows = 1 loops = 10001 )
24 - > Seq Scan on product tmp ( cost = 0.00 . .195 .25 rows = 56 width = 4 ) ( actual time = 0.577 . .1 .174 rows = 3 loops = 10001 )
25 Filter: ( product . type = type )
26 Rows Removed by Filter: 9998
27 Planning time : 0.143 ms
28 Execution time : 12776.098 ms
29 ( 10 rows )
30
31 NO .4
32 QUERY PLAN
33 ----------------------------------------------------------------------------------------------------------------------------
34 Hash Anti Join ( cost = 307.45 . .667 .49 rows = 9350 width = 12 ) ( actual time = 36.436 . .78 .295 rows = 3383 loops = 1 )
35 Hash Cond: ( product . type = tmp . type )
36 Join Filter: ( product . price < tmp . price )
37 Rows Removed by Join Filter: 14529
38 - > Seq Scan on product ( cost = 0.00 . .167 .20 rows = 11220 width = 12 ) ( actual time = 0.011 . .15 .269 rows = 10001 loops = 1 )
39 - > Hash ( cost = 167.20 . .167 .20 rows = 11220 width = 8 ) ( actual time = 36.365 . .36 .365 rows = 10001 loops = 1 )
40 Buckets: 16384 Batches: 1 Memory Usage : 519 kB
41 - > Seq Scan on product tmp ( cost = 0.00 . .167 .20 rows = 11220 width = 8 ) ( actual time = 0.009 . .18 .940 rows = 10001 loops = 1 )
42 Planning time : 0.164 ms
43 Execution time : 83.163 ms
44 ( 10 rows )
45
(2)につきましてもPostgres都合上、下記に修正いたしました。
SELECT product.*
FROM product
INNER JOIN(
SELECT type, MAX(price) AS max_price
FROM product
GROUP BY type
) tmp
ON product.type = tmp.type
AND product.price = tmp.max_price;
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
退会済みユーザー
2017/02/27 08:49