| 946 | | because scanning the table and performing the hash joins was estimated to be cheaper for the current data distribution. |
| 947 | | |
| 948 | | The query was again executed 10 times with both indexes present and |
| 949 | | |
| 950 | | {{{ |
| 951 | | |
| | 946 | because scanning the table and performing the hash joins was estimated to be cheaper for the current data distribution. All of this is shown in the output bellow: |
| | 947 | |
| | 948 | {{{ |
| | 949 | Sort (cost=7826.09..7826.10 rows=1 width=282) (actual time=98.314..98.323 rows=6 loops=1) |
| | 950 | Sort Key: de.date_modified DESC, (((COALESCE(sum(((op_1.quantity)::numeric * op_1.price_at_purchase)) FILTER (WHERE (o_1.order_id IS NOT NULL)), 0.00)) - pre.revenue_before)) DESC |
| | 951 | Sort Method: quicksort Memory: 25kB |
| | 952 | Buffers: shared hit=1163 |
| | 953 | CTE discount_events |
| | 954 | -> Hash Join (cost=22.19..55.57 rows=10 width=60) (actual time=0.047..0.054 rows=6 loops=1) |
| | 955 | Hash Cond: (mp.modification_id = m.modification_id) |
| | 956 | Buffers: shared hit=2 |
| | 957 | -> Seq Scan on modification_products mp (cost=0.00..28.50 rows=1850 width=16) (actual time=0.007..0.008 rows=16 loops=1) |
| | 958 | Buffers: shared hit=1 |
| | 959 | -> Hash (cost=22.12..22.12 rows=5 width=52) (actual time=0.017..0.018 rows=5 loops=1) |
| | 960 | Buckets: 1024 Batches: 1 Memory Usage: 9kB |
| | 961 | Buffers: shared hit=1 |
| | 962 | -> Seq Scan on modifications m (cost=0.00..22.12 rows=5 width=52) (actual time=0.010..0.012 rows=5 loops=1) |
| | 963 | Filter: (type_of_modification = 'DISCOUNT'::modification_type) |
| | 964 | Rows Removed by Filter: 8 |
| | 965 | Buffers: shared hit=1 |
| | 966 | -> Nested Loop (cost=7768.41..7770.52 rows=1 width=282) (actual time=98.280..98.307 rows=6 loops=1) |
| | 967 | Buffers: shared hit=1163 |
| | 968 | -> Hash Join (cost=7768.27..7769.52 rows=1 width=184) (actual time=98.265..98.276 rows=6 loops=1) |
| | 969 | Hash Cond: (p.product_id = de.product_id) |
| | 970 | Buffers: shared hit=1151 |
| | 971 | -> Seq Scan on products p (cost=0.00..1.17 rows=17 width=20) (actual time=0.015..0.018 rows=17 loops=1) |
| | 972 | Buffers: shared hit=1 |
| | 973 | -> Hash (cost=7768.26..7768.26 rows=1 width=188) (actual time=98.239..98.244 rows=6 loops=1) |
| | 974 | Buckets: 1024 Batches: 1 Memory Usage: 9kB |
| | 975 | Buffers: shared hit=1150 |
| | 976 | -> Nested Loop (cost=7767.70..7768.26 rows=1 width=188) (actual time=98.220..98.239 rows=6 loops=1) |
| | 977 | Join Filter: ((de.product_id = de_2.product_id) AND (de.modification_id = de_2.modification_id)) |
| | 978 | Rows Removed by Join Filter: 15 |
| | 979 | Buffers: shared hit=1150 |
| | 980 | -> Merge Join (cost=3884.24..3884.40 rows=1 width=132) (actual time=51.554..51.563 rows=6 loops=1) |
| | 981 | Merge Cond: ((de.product_id = pre.product_id) AND (de.modification_id = pre.modification_id)) |
| | 982 | Buffers: shared hit=576 |
| | 983 | -> Sort (cost=0.37..0.39 rows=10 width=52) (actual time=0.065..0.066 rows=6 loops=1) |
| | 984 | Sort Key: de.product_id, de.modification_id |
| | 985 | Sort Method: quicksort Memory: 25kB |
| | 986 | Buffers: shared hit=2 |
| | 987 | -> CTE Scan on discount_events de (cost=0.00..0.20 rows=10 width=52) (actual time=0.050..0.057 rows=6 loops=1) |
| | 988 | Buffers: shared hit=2 |
| | 989 | -> Sort (cost=3883.87..3883.90 rows=10 width=80) (actual time=51.485..51.488 rows=6 loops=1) |
| | 990 | Sort Key: pre.product_id, pre.modification_id |
| | 991 | Sort Method: quicksort Memory: 25kB |
| | 992 | Buffers: shared hit=574 |
| | 993 | -> Subquery Scan on pre (cost=3883.46..3883.71 rows=10 width=80) (actual time=51.472..51.480 rows=6 loops=1) |
| | 994 | Buffers: shared hit=574 |
| | 995 | -> HashAggregate (cost=3883.46..3883.61 rows=10 width=80) (actual time=51.472..51.478 rows=6 loops=1) |
| | 996 | Group Key: de_1.modification_id, de_1.product_id |
| | 997 | Batches: 1 Memory Usage: 24kB |
| | 998 | Buffers: shared hit=574 |
| | 999 | -> Hash Left Join (cost=227.47..2758.14 rows=75021 width=39) (actual time=1.840..39.403 rows=50007 loops=1) |
| | 1000 | Hash Cond: (op.order_id = o.order_id) |
| | 1001 | Join Filter: ((o.purchase_date < de_1.date_modified) AND (o.purchase_date >= (de_1.date_modified - '30 days'::interval))) |
| | 1002 | Rows Removed by Join Filter: 9178 |
| | 1003 | Buffers: shared hit=574 |
| | 1004 | -> Hash Right Join (cost=0.33..2334.07 rows=75021 width=43) (actual time=0.024..26.496 rows=50007 loops=1) |
| | 1005 | Hash Cond: (op.product_id = de_1.product_id) |
| | 1006 | Buffers: shared hit=552 |
| | 1007 | -> Seq Scan on order_products op (cost=0.00..1302.21 rows=75021 width=31) (actual time=0.012..6.080 rows=75021 loops=1) |
| | 1008 | Buffers: shared hit=552 |
| | 1009 | -> Hash (cost=0.20..0.20 rows=10 width=20) (actual time=0.005..0.005 rows=6 loops=1) |
| | 1010 | Buckets: 1024 Batches: 1 Memory Usage: 9kB |
| | 1011 | -> CTE Scan on discount_events de_1 (cost=0.00..0.20 rows=10 width=20) (actual time=0.001..0.002 rows=6 loops=1) |
| | 1012 | -> Hash (cost=164.53..164.53 rows=5009 width=12) (actual time=1.798..1.798 rows=5009 loops=1) |
| | 1013 | Buckets: 8192 Batches: 1 Memory Usage: 280kB |
| | 1014 | Buffers: shared hit=22 |
| | 1015 | -> Index Only Scan using idx_orders_status_purchase_date on orders o (cost=0.29..164.53 rows=5009 width=12) (actual time=0.034..0.937 rows=5009 loops=1) |
| | 1016 | Index Cond: (status = ANY ('{PAID,SHIPPED,DELIVERED}'::order_status_type[])) |
| | 1017 | Heap Fetches: 0 |
| | 1018 | Buffers: shared hit=22 |
| | 1019 | -> HashAggregate (cost=3883.46..3883.61 rows=10 width=80) (actual time=7.777..7.778 rows=4 loops=6) |
| | 1020 | Group Key: de_2.modification_id, de_2.product_id |
| | 1021 | Batches: 1 Memory Usage: 24kB |
| | 1022 | Buffers: shared hit=574 |
| | 1023 | -> Hash Left Join (cost=227.47..2758.14 rows=75021 width=39) (actual time=1.816..35.408 rows=50007 loops=1) |
| | 1024 | Hash Cond: (op_1.order_id = o_1.order_id) |
| | 1025 | Join Filter: ((o_1.purchase_date > de_2.date_modified) AND (o_1.purchase_date <= (de_2.date_modified + '30 days'::interval))) |
| | 1026 | Rows Removed by Join Filter: 9680 |
| | 1027 | Buffers: shared hit=574 |
| | 1028 | -> Hash Right Join (cost=0.33..2334.07 rows=75021 width=43) (actual time=0.025..23.451 rows=50007 loops=1) |
| | 1029 | Hash Cond: (op_1.product_id = de_2.product_id) |
| | 1030 | Buffers: shared hit=552 |
| | 1031 | -> Seq Scan on order_products op_1 (cost=0.00..1302.21 rows=75021 width=31) (actual time=0.011..5.512 rows=75021 loops=1) |
| | 1032 | Buffers: shared hit=552 |
| | 1033 | -> Hash (cost=0.20..0.20 rows=10 width=20) (actual time=0.005..0.005 rows=6 loops=1) |
| | 1034 | Buckets: 1024 Batches: 1 Memory Usage: 9kB |
| | 1035 | -> CTE Scan on discount_events de_2 (cost=0.00..0.20 rows=10 width=20) (actual time=0.001..0.003 rows=6 loops=1) |
| | 1036 | -> Hash (cost=164.53..164.53 rows=5009 width=12) (actual time=1.773..1.773 rows=5009 loops=1) |
| | 1037 | Buckets: 8192 Batches: 1 Memory Usage: 280kB |
| | 1038 | Buffers: shared hit=22 |
| | 1039 | -> Index Only Scan using idx_orders_status_purchase_date on orders o_1 (cost=0.29..164.53 rows=5009 width=12) (actual time=0.029..0.923 rows=5009 loops=1) |
| | 1040 | Index Cond: (status = ANY ('{PAID,SHIPPED,DELIVERED}'::order_status_type[])) |
| | 1041 | Heap Fetches: 0 |
| | 1042 | Buffers: shared hit=22 |
| | 1043 | -> Index Scan using releases_pkey on releases r (cost=0.14..0.86 rows=1 width=18) (actual time=0.002..0.002 rows=1 loops=6) |
| | 1044 | Index Cond: (release_id = p.release_id) |
| | 1045 | Buffers: shared hit=12 |
| | 1046 | Planning: |
| | 1047 | Buffers: shared hit=56 |
| | 1048 | Planning Time: 2.028 ms |
| | 1049 | Execution Time: 98.520 ms |
| | 1050 | }}} |
| | 1051 | |
| | 1052 | The query was executed 10 times with each execution time shown bellow: |
| | 1053 | |
| | 1054 | {{{ |
| | 1055 | Execution 1: 97.293 ms |
| | 1056 | |
| | 1057 | Execution 2: 97.255 ms |
| | 1058 | |
| | 1059 | Execution 3: 97.952 ms |
| | 1060 | |
| | 1061 | Execution 4: 99.562 ms |
| | 1062 | |
| | 1063 | Execution 5: 96.874 ms |
| | 1064 | |
| | 1065 | Execution 6: 96.652 ms |
| | 1066 | |
| | 1067 | Execution 7: 98.997 ms |
| | 1068 | |
| | 1069 | Execution 8: 97.512 ms |
| | 1070 | |
| | 1071 | Execution 9: 98.037 ms |
| | 1072 | |
| | 1073 | Execution 10: 96.970 ms |