Changes between Version 7 and Version 8 of OtherTopics
- Timestamp:
- 06/03/26 11:59:52 (3 weeks ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
OtherTopics
v7 v8 106 106 107 107 {{{ 108 Gather Merge (cost=810 30.64..195549.38 rows=967764 width=146) (actual time=94.209..106.394rows=2 loops=1)108 Gather Merge (cost=81029.64..195548.38 rows=967764 width=147) (actual time=102.092..113.433 rows=2 loops=1) 109 109 Workers Planned: 3 110 110 Workers Launched: 3 111 Buffers: shared hit=2233 6111 Buffers: shared hit=22335 112 112 InitPlan 1 113 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time= 3.367..3.369rows=1 loops=1)113 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=6.105..6.106 rows=1 loops=1) 114 114 Buffers: shared hit=1 115 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 15..0.016rows=1 loops=1)115 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.028..0.028 rows=1 loops=1) 116 116 Buffers: shared hit=1 117 -> Sort (cost=800 30.57..80837.04 rows=322588 width=146) (actual time=60.212..60.213rows=0 loops=4)117 -> Sort (cost=80029.57..80836.04 rows=322588 width=147) (actual time=65.204..65.206 rows=0 loops=4) 118 118 Sort Key: bookings.date_from DESC 119 119 Sort Method: quicksort Memory: 25kB 120 Buffers: shared hit=2233 5120 Buffers: shared hit=22334 121 121 Worker 0: Sort Method: quicksort Memory: 25kB 122 122 Worker 1: Sort Method: quicksort Memory: 25kB 123 123 Worker 2: Sort Method: quicksort Memory: 25kB 124 -> Parallel Seq Scan on bookings (cost=0.00..2625 6.35 rows=322588 width=146) (actual time=37.611..60.082rows=0 loops=4)124 -> Parallel Seq Scan on bookings (cost=0.00..26255.35 rows=322588 width=147) (actual time=41.358..65.056 rows=0 loops=4) 125 125 Filter: ((sitter_id)::text = ((InitPlan 1).col1)::text) 126 126 Rows Removed by Filter: 250005 127 Buffers: shared hit=22224 128 Planning Time: 0.168 ms 127 Buffers: shared hit=22223 128 Planning: 129 Buffers: shared hit=139 read=1 130 Planning Time: 1.125 ms 129 131 JIT: 130 132 Functions: 12 131 133 Options: Inlining false, Optimization false, Expressions true, Deforming true 132 Timing: Generation 1. 466 ms (Deform 0.675 ms), Inlining 0.000 ms, Optimization 1.430 ms, Emission 19.219 ms, Total 22.115ms133 Execution Time: 1 06.834ms134 }}} 135 136 '''Average execution time (10 attempts):''' 1 05.235ms134 Timing: Generation 1.536 ms (Deform 0.703 ms), Inlining 0.000 ms, Optimization 1.491 ms, Emission 21.760 ms, Total 24.788 ms 135 Execution Time: 148.520 ms 136 }}} 137 138 '''Average execution time (10 attempts):''' 145.812ms 137 139 138 140 Next, we add this index: … … 153 155 154 156 {{{ 155 Index Scan using idx_bookings_sitter_date on bookings (cost=0.45..7250 3.52 rows=1000022 width=146) (actual time=0.066..0.068rows=2 loops=1)157 Index Scan using idx_bookings_sitter_date on bookings (cost=0.45..72505.21 rows=1000022 width=147) (actual time=0.101..0.104 rows=2 loops=1) 156 158 Index Cond: ((sitter_id)::text = ((InitPlan 1).col1)::text) 157 159 Buffers: shared hit=2 read=3 158 160 InitPlan 1 159 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=0.0 12..0.013rows=1 loops=1)161 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=0.040..0.040 rows=1 loops=1) 160 162 Buffers: shared hit=1 161 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 11..0.011rows=1 loops=1)163 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.038..0.038 rows=1 loops=1) 162 164 Buffers: shared hit=1 163 165 Planning: 164 Buffers: shared hit= 39 read=2165 Planning Time: 0. 618ms166 Execution Time: 0. 092 ms167 }}} 168 169 '''Average execution time (10 attempts):''' 0. 087ms166 Buffers: shared hit=124 read=5 167 Planning Time: 0.969 ms 168 Execution Time: 0.132 ms 169 }}} 170 171 '''Average execution time (10 attempts):''' 0.125ms 170 172 171 173 Because the execution time has been massively lowered by bypassing the expensive sequential scan and memory sort, we '''keep''' this index. … … 187 189 188 190 {{{ 189 Finalize Aggregate (cost=33210.02..33210.03 rows=1 width=32) (actual time=121.837..135.038 rows=1 loops=1) 190 Buffers: shared hit=25818 191 InitPlan 1 192 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=0.014..0.015 rows=1 loops=1) 193 Buffers: shared hit=1 194 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.012..0.013 rows=1 loops=1) 195 Buffers: shared hit=1 196 -> Gather (cost=33209.67..33209.98 rows=3 width=32) (actual time=119.802..135.016 rows=4 loops=1) 197 Workers Planned: 3 198 Workers Launched: 3 199 Buffers: shared hit=25818 200 -> Partial Aggregate (cost=32209.67..32209.68 rows=1 width=32) (actual time=90.870..90.873 rows=1 loops=4) 201 Buffers: shared hit=25817 202 -> Parallel Hash Join (cost=4971.92..32075.06 rows=53846 width=4) (actual time=77.089..90.867 rows=0 loops=4) 203 Hash Cond: ((b.booking_id)::text = (r.booking_id)::text) 204 Buffers: shared hit=25817 205 -> Parallel Seq Scan on bookings b (cost=0.00..26256.35 rows=322588 width=37) (actual time=41.349..55.119 rows=0 loops=4) 206 Filter: ((sitter_id)::text = ((InitPlan 1).col1)::text) 207 Rows Removed by Filter: 250005 208 Buffers: shared hit=22224 209 -> Parallel Hash (cost=4102.52..4102.52 rows=69552 width=41) (actual time=34.636..34.637 rows=41731 loops=4) 210 Buckets: 262144 Batches: 1 Memory Usage: 15200kB 211 Buffers: shared hit=3407 212 -> Parallel Seq Scan on reviews r (cost=0.00..4102.52 rows=69552 width=41) (actual time=0.026..11.172 rows=41731 loops=4) 213 Buffers: shared hit=3407 214 Planning: 215 Buffers: shared hit=16 216 Planning Time: 0.444 ms 217 Execution Time: 135.097 ms 218 }}} 219 220 '''Average execution time (10 attempts):''' 131.722ms 221 222 We add this index: 223 224 {{{ 225 #!sql 226 CREATE INDEX idx_reviews_booking_id 227 ON project.reviews (booking_id); 228 }}} 229 230 Execution with indexes: 231 232 {{{ 233 Finalize Aggregate (cost=33210.02..33210.03 rows=1 width=32) (actual time=125.303..138.277 rows=1 loops=1) 234 Buffers: shared hit=25818 191 Finalize Aggregate (cost=32123.42..32123.43 rows=1 width=32) (actual time=106.390..116.499 rows=1 loops=1) 192 Buffers: shared hit=24792 235 193 InitPlan 1 236 194 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=0.013..0.014 rows=1 loops=1) 237 195 Buffers: shared hit=1 238 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.01 2..0.012 rows=1 loops=1)196 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.011..0.012 rows=1 loops=1) 239 197 Buffers: shared hit=1 240 -> Gather (cost=3 3209.67..33209.98 rows=3 width=32) (actual time=123.351..138.255rows=4 loops=1)198 -> Gather (cost=32123.08..32123.39 rows=3 width=32) (actual time=104.969..116.470 rows=4 loops=1) 241 199 Workers Planned: 3 242 200 Workers Launched: 3 243 Buffers: shared hit=2 5818244 -> Partial Aggregate (cost=3 2209.67..32209.68 rows=1 width=32) (actual time=93.069..93.072rows=1 loops=4)245 Buffers: shared hit=2 5817246 -> Parallel Hash Join (cost= 4971.92..32075.06 rows=53846 width=4) (actual time=78.783..93.066rows=0 loops=4)201 Buffers: shared hit=24792 202 -> Partial Aggregate (cost=31123.08..31123.09 rows=1 width=32) (actual time=75.769..75.773 rows=1 loops=4) 203 Buffers: shared hit=24791 204 -> Parallel Hash Join (cost=3926.81..31028.95 rows=37651 width=4) (actual time=62.399..75.767 rows=0 loops=4) 247 205 Hash Cond: ((b.booking_id)::text = (r.booking_id)::text) 248 Buffers: shared hit=2 5817249 -> Parallel Seq Scan on bookings b (cost=0.00..2625 6.35 rows=322588 width=37) (actual time=42.860..57.138rows=0 loops=4)206 Buffers: shared hit=24791 207 -> Parallel Seq Scan on bookings b (cost=0.00..26255.35 rows=322588 width=37) (actual time=40.115..53.479 rows=0 loops=4) 250 208 Filter: ((sitter_id)::text = ((InitPlan 1).col1)::text) 251 209 Rows Removed by Filter: 250005 252 Buffers: shared hit=2222 4253 -> Parallel Hash (cost= 4102.52..4102.52 rows=69552 width=41) (actual time=34.827..34.828 rows=41731loops=4)254 Buckets: 262144 Batches: 1 Memory Usage: 15168kB255 Buffers: shared hit= 3407256 -> Parallel Seq Scan on reviews r (cost=0.00.. 4102.52 rows=69552 width=41) (actual time=0.029..11.437 rows=41731loops=4)257 Buffers: shared hit= 3407210 Buffers: shared hit=22223 211 -> Parallel Hash (cost=3068.58..3068.58 rows=68658 width=41) (actual time=21.453..21.454 rows=29180 loops=4) 212 Buckets: 131072 Batches: 1 Memory Usage: 10208kB 213 Buffers: shared hit=2382 214 -> Parallel Seq Scan on reviews r (cost=0.00..3068.58 rows=68658 width=41) (actual time=0.026..7.819 rows=29180 loops=4) 215 Buffers: shared hit=2382 258 216 Planning: 259 Buffers: shared hit=16 260 Planning Time: 0.554 ms 261 Execution Time: 138.346 ms 262 }}} 263 264 '''Average execution time (10 attempts):''' 138.346ms 265 266 There is practically no change in the execution time, and the query planner completely ignored the index because it determined that performing a Parallel Seq Scan and Parallel Hash Join was cheaper than doing individual index lookups on such a large dataset. Because the index is not utilized for this query and only adds complexity during INSERT and UPDATE operations, we will '''not''' keep this index. 217 Buffers: shared hit=165 read=2 218 Planning Time: 1.200 ms 219 Execution Time: 116.627 ms 220 }}} 221 222 '''Average execution time (10 attempts):''' 115.143ms 223 224 We add this index: 225 226 {{{ 227 #!sql 228 CREATE INDEX idx_reviews_booking_id 229 ON project.reviews (booking_id); 230 }}} 231 232 Execution with indexes: 233 234 {{{ 235 Finalize Aggregate (cost=32123.42..32123.43 rows=1 width=32) (actual time=102.850..113.265 rows=1 loops=1) 236 Buffers: shared hit=24792 237 InitPlan 1 238 -> Limit (cost=0.00..0.02 rows=1 width=90) (actual time=0.014..0.016 rows=1 loops=1) 239 Buffers: shared hit=1 240 -> Seq Scan on pet_sitters (cost=0.00..16.90 rows=690 width=90) (actual time=0.013..0.013 rows=1 loops=1) 241 Buffers: shared hit=1 242 -> Gather (cost=32123.08..32123.39 rows=3 width=32) (actual time=101.457..113.235 rows=4 loops=1) 243 Workers Planned: 3 244 Workers Launched: 3 245 Buffers: shared hit=24792 246 -> Partial Aggregate (cost=31123.08..31123.09 rows=1 width=32) (actual time=72.199..72.202 rows=1 loops=4) 247 Buffers: shared hit=24791 248 -> Parallel Hash Join (cost=3926.81..31028.95 rows=37651 width=4) (actual time=59.642..72.196 rows=0 loops=4) 249 Hash Cond: ((b.booking_id)::text = (r.booking_id)::text) 250 Buffers: shared hit=24791 251 -> Parallel Seq Scan on bookings b (cost=0.00..26255.35 rows=322588 width=37) (actual time=37.082..49.436 rows=0 loops=4) 252 Filter: ((sitter_id)::text = ((InitPlan 1).col1)::text) 253 Rows Removed by Filter: 250005 254 Buffers: shared hit=22223 255 -> Parallel Hash (cost=3068.58..3068.58 rows=68658 width=41) (actual time=21.936..21.937 rows=29180 loops=4) 256 Buckets: 131072 Batches: 1 Memory Usage: 10240kB 257 Buffers: shared hit=2382 258 -> Parallel Seq Scan on reviews r (cost=0.00..3068.58 rows=68658 width=41) (actual time=0.020..7.757 rows=29180 loops=4) 259 Buffers: shared hit=2382 260 Planning: 261 Buffers: shared hit=78 read=6 262 Planning Time: 1.082 ms 263 Execution Time: 113.336 ms 264 }}} 265 266 '''Average execution time (10 attempts):''' 114.288ms 267 268 There is practically no change in the execution time. Because the index is not utilized for this query and only adds overhead during INSERT and UPDATE operations, we will '''not''' keep this index. 267 269 268 270 === 3. Sitter Performance Analytics (Phase 6 Query) === … … 335 337 Execution without indexes: 336 338 {{{ 337 Limit (cost=15 1645.74..151645.77 rows=10 width=176) (actual time=2620.994..2631.056rows=4 loops=1)338 Buffers: shared hit=7 40349339 Limit (cost=154247.04..154247.06 rows=10 width=176) (actual time=2600.384..2611.703 rows=4 loops=1) 340 Buffers: shared hit=735904 read=1 339 341 CTE params 340 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.0 24..0.025rows=1 loops=1)341 -> Sort (cost=15 1645.72..151646.30 rows=230 width=176) (actual time=2562.262..2572.322rows=4 loops=1)342 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.012..0.013 rows=1 loops=1) 343 -> Sort (cost=154247.02..154247.59 rows=230 width=176) (actual time=2544.398..2555.714 rows=4 loops=1) 342 344 Sort Key: (dense_rank() OVER (?)) 343 345 Sort Method: quicksort Memory: 26kB 344 Buffers: shared hit=7 40349345 -> WindowAgg (cost=15 1631.00..151640.75 rows=230 width=176) (actual time=2562.200..2572.274rows=4 loops=1)346 Buffers: shared hit=7 40346347 -> Sort (cost=15 1630.98..151631.55 rows=230 width=152) (actual time=2562.183..2572.243rows=4 loops=1)346 Buffers: shared hit=735904 read=1 347 -> WindowAgg (cost=154232.29..154242.05 rows=230 width=176) (actual time=2544.338..2555.668 rows=4 loops=1) 348 Buffers: shared hit=735901 read=1 349 -> Sort (cost=154232.27..154232.85 rows=230 width=152) (actual time=2544.310..2555.625 rows=4 loops=1) 348 350 Sort Key: ((((((COALESCE(sf.total_revenue, '0'::bigint))::numeric * 0.5) + ((COALESCE(ss.completed_bookings, '0'::bigint) * 10))::numeric) + (COALESCE(sr.avg_rating, '0'::numeric) * '15'::numeric)) - ((COALESCE(ss.missed_bookings, '0'::bigint) * 5))::numeric)) DESC 349 351 Sort Method: quicksort Memory: 25kB 350 Buffers: shared hit=7 40346351 -> Hash Join (cost=15 1596.30..151621.96 rows=230 width=152) (actual time=2562.155..2572.225rows=4 loops=1)352 Buffers: shared hit=735901 read=1 353 -> Hash Join (cost=154197.60..154223.25 rows=230 width=152) (actual time=2544.282..2555.608 rows=4 loops=1) 352 354 Hash Cond: ((u.user_id)::text = (ps.user_id)::text) 353 Buffers: shared hit=7 40343354 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=64) (actual time=0.0 15..0.018rows=12 loops=1)355 Buffers: shared hit=1356 -> Hash (cost=15 1593.43..151593.43 rows=230 width=146) (actual time=2562.102..2572.160 rows=4 loops=1)355 Buffers: shared hit=735898 read=1 356 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=64) (actual time=0.041..0.045 rows=12 loops=1) 357 Buffers: shared read=1 358 -> Hash (cost=154194.72..154194.72 rows=230 width=146) (actual time=2544.166..2555.480 rows=4 loops=1) 357 359 Buckets: 1024 Batches: 1 Memory Usage: 9kB 358 Buffers: shared hit=7 40342359 -> Merge Left Join (cost=15 1588.77..151593.43 rows=230 width=146) (actual time=2562.070..2572.145 rows=4 loops=1)360 Buffers: shared hit=735898 361 -> Merge Left Join (cost=154190.07..154194.72 rows=230 width=146) (actual time=2544.131..2555.465 rows=4 loops=1) 360 362 Merge Cond: ((ps.user_id)::text = (sr.sitter_id)::text) 361 Buffers: shared hit=7 40342362 -> Merge Left Join (cost= 99928.44..99932.50 rows=230 width=122) (actual time=2328.059..2328.084rows=4 loops=1)363 Buffers: shared hit=735898 364 -> Merge Left Join (cost=109447.58..109451.64 rows=230 width=122) (actual time=2328.303..2328.329 rows=4 loops=1) 363 365 Merge Cond: ((ps.user_id)::text = (sf.sitter_id)::text) 364 Buffers: shared hit=71 4518365 -> Merge Left Join (cost=4 3617.47..43620.94 rows=230 width=114) (actual time=779.217..779.233rows=4 loops=1)366 Buffers: shared hit=711107 367 -> Merge Left Join (cost=48939.75..48943.22 rows=230 width=114) (actual time=724.264..724.279 rows=4 loops=1) 366 368 Merge Cond: ((ps.user_id)::text = (ss.sitter_id)::text) 367 369 Filter: (COALESCE(ss.total_bookings, '0'::bigint) > 0) 368 Buffers: shared hit=2 3035369 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.0 24..0.026rows=4 loops=1)370 Buffers: shared hit=22224 371 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.018..0.021 rows=4 loops=1) 370 372 Sort Key: ps.user_id 371 373 Sort Method: quicksort Memory: 25kB 372 374 Buffers: shared hit=1 373 -> Seq Scan on pet_sitters ps (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 14..0.015rows=4 loops=1)375 -> Seq Scan on pet_sitters ps (cost=0.00..16.90 rows=690 width=90) (actual time=0.008..0.009 rows=4 loops=1) 374 376 Buffers: shared hit=1 375 -> Sort (cost=4 3568.03..43568.04 rows=1 width=61) (actual time=779.154..779.159rows=4 loops=1)377 -> Sort (cost=48890.31..48890.32 rows=1 width=61) (actual time=724.208..724.212 rows=4 loops=1) 376 378 Sort Key: ss.sitter_id 377 379 Sort Method: quicksort Memory: 25kB 378 Buffers: shared hit=2 3034379 -> Subquery Scan on ss (cost=4 3568.00..43568.02 rows=1 width=61) (actual time=779.137..779.144rows=4 loops=1)380 Buffers: shared hit=2 3034381 -> HashAggregate (cost=4 3568.00..43568.01 rows=1 width=61) (actual time=779.130..779.134rows=4 loops=1)380 Buffers: shared hit=22223 381 -> Subquery Scan on ss (cost=48890.28..48890.30 rows=1 width=61) (actual time=724.190..724.196 rows=4 loops=1) 382 Buffers: shared hit=22223 383 -> HashAggregate (cost=48890.28..48890.29 rows=1 width=61) (actual time=724.182..724.187 rows=4 loops=1) 382 384 Group Key: b.sitter_id 383 385 Batches: 1 Memory Usage: 24kB 384 Buffers: shared hit=23034 385 -> Nested Loop (cost=16900.42..41901.29 rows=111114 width=83) (actual time=44.143..421.383 rows=1000017 loops=1) 386 Buffers: shared hit=23034 387 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.028..0.030 rows=1 loops=1) 388 -> Bitmap Heap Scan on bookings b (cost=16900.42..40790.13 rows=111114 width=87) (actual time=44.095..233.073 rows=1000017 loops=1) 389 Recheck Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 390 Heap Blocks: exact=22223 391 Buffers: shared hit=23034 392 -> Bitmap Index Scan on idx_bookings_owner_date (cost=0.00..16872.65 rows=111114 width=0) (actual time=39.433..39.433 rows=1000017 loops=1) 393 Index Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 394 Buffers: shared hit=811 395 -> Sort (cost=56310.97..56310.97 rows=1 width=45) (actual time=1548.817..1548.822 rows=2 loops=1) 386 Buffers: shared hit=22223 387 -> Nested Loop (cost=0.00..47223.57 rows=111114 width=83) (actual time=0.044..359.696 rows=1000017 loops=1) 388 Join Filter: ((b.date_from >= p.start_date) AND (b.date_from < p.end_date)) 389 Rows Removed by Join Filter: 5 390 Buffers: shared hit=22223 391 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.015..0.017 rows=1 loops=1) 392 -> Seq Scan on bookings b (cost=0.00..32223.22 rows=1000022 width=87) (actual time=0.013..106.436 rows=1000022 loops=1) 393 Buffers: shared hit=22223 394 -> Sort (cost=60507.84..60507.84 rows=1 width=45) (actual time=1604.016..1604.022 rows=2 loops=1) 396 395 Sort Key: sf.sitter_id 397 396 Sort Method: quicksort Memory: 25kB 398 Buffers: shared hit=6 91483399 -> Subquery Scan on sf (cost= 56310.94..56310.96 rows=1 width=45) (actual time=1548.794..1548.800rows=2 loops=1)400 Buffers: shared hit=6 91483401 -> HashAggregate (cost= 56310.94..56310.95 rows=1 width=45) (actual time=1548.786..1548.791rows=2 loops=1)397 Buffers: shared hit=688883 398 -> Subquery Scan on sf (cost=60507.81..60507.83 rows=1 width=45) (actual time=1603.989..1603.995 rows=2 loops=1) 399 Buffers: shared hit=688883 400 -> HashAggregate (cost=60507.81..60507.82 rows=1 width=45) (actual time=1603.982..1603.986 rows=2 loops=1) 402 401 Group Key: b_1.sitter_id 403 402 Batches: 1 Memory Usage: 24kB 404 Buffers: shared hit=6 91483405 -> Nested Loop (cost=0.42.. 56216.86 rows=18815 width=41) (actual time=0.103..1495.703 rows=167315 loops=1)406 Buffers: shared hit=6 91483407 -> Nested Loop (cost=0.00..372 63.35 rows=18815 width=74) (actual time=0.043..225.930 rows=167315 loops=1)403 Buffers: shared hit=688883 404 -> Nested Loop (cost=0.42..60413.23 rows=18915 width=41) (actual time=0.129..1550.156 rows=166665 loops=1) 405 Buffers: shared hit=688883 406 -> Nested Loop (cost=0.00..37276.85 rows=18915 width=74) (actual time=0.043..232.987 rows=166665 loops=1) 408 407 Join Filter: ((b_1.date_from >= p_1.start_date) AND (b_1.date_from < p_1.end_date)) 409 408 Rows Removed by Join Filter: 3 410 409 Buffers: shared hit=22223 411 410 -> CTE Scan on params p_1 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.003 rows=1 loops=1) 412 -> Seq Scan on bookings b_1 (cost=0.00..34723.28 rows=1 69337 width=78) (actual time=0.026..183.123 rows=167318 loops=1)411 -> Seq Scan on bookings b_1 (cost=0.00..34723.28 rows=170237 width=78) (actual time=0.024..189.504 rows=166668 loops=1) 413 412 Filter: ((status)::text = 'Completed'::text) 414 Rows Removed by Filter: 83 2704413 Rows Removed by Filter: 833354 415 414 Buffers: shared hit=22223 416 -> Index Scan using payments_booking_id_key on payments pay (cost=0.42..1. 01 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=167315)415 -> Index Scan using payments_booking_id_key on payments pay (cost=0.42..1.22 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=166665) 417 416 Index Cond: ((booking_id)::text = (b_1.booking_id)::text) 418 Buffers: shared hit=66 9260419 -> Sort (cost= 51660.34..51660.34 rows=1 width=61) (actual time=233.986..244.032rows=2 loops=1)417 Buffers: shared hit=666660 418 -> Sort (cost=44742.49..44742.49 rows=1 width=61) (actual time=215.803..227.106 rows=2 loops=1) 420 419 Sort Key: sr.sitter_id 421 420 Sort Method: quicksort Memory: 25kB 422 Buffers: shared hit=2 5824423 -> Subquery Scan on sr (cost= 51660.30..51660.33 rows=1 width=61) (actual time=233.958..244.007rows=2 loops=1)424 Buffers: shared hit=2 5824425 -> HashAggregate (cost= 51660.30..51660.32 rows=1 width=61) (actual time=233.948..243.997rows=2 loops=1)421 Buffers: shared hit=24791 422 -> Subquery Scan on sr (cost=44742.45..44742.48 rows=1 width=61) (actual time=215.773..227.079 rows=2 loops=1) 423 Buffers: shared hit=24791 424 -> HashAggregate (cost=44742.45..44742.47 rows=1 width=61) (actual time=215.766..227.070 rows=2 loops=1) 426 425 Group Key: b_2.sitter_id 427 426 Batches: 1 Memory Usage: 24kB 428 Buffers: shared hit=2 5824429 -> Nested Loop (cost= 5983.61..51520.87 rows=18591 width=78) (actual time=76.620..198.922 rows=167316loops=1)427 Buffers: shared hit=24791 428 -> Nested Loop (cost=4926.81..44645.18 rows=12969 width=78) (actual time=68.121..195.697 rows=116717 loops=1) 430 429 Join Filter: ((b_2.date_from >= p_2.start_date) AND (b_2.date_from < p_2.end_date)) 431 Rows Removed by Join Filter: 3432 Buffers: shared hit=2 5824433 -> CTE Scan on params p_2 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.00 3rows=1 loops=1)434 -> Gather (cost= 5983.61..49011.08 rows=167318 width=82) (actual time=76.604..171.175 rows=167319 loops=1)430 Rows Removed by Join Filter: 2 431 Buffers: shared hit=24791 432 -> CTE Scan on params p_2 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.004 rows=1 loops=1) 433 -> Gather (cost=4926.81..42894.38 rows=116719 width=82) (actual time=68.087..176.699 rows=116719 loops=1) 435 434 Workers Planned: 3 436 435 Workers Launched: 3 437 Buffers: shared hit=2 5824438 -> Parallel Hash Join (cost= 4983.61..31279.28 rows=53974 width=82) (actual time=45.331..167.054 rows=41830 loops=4)436 Buffers: shared hit=24791 437 -> Parallel Hash Join (cost=3926.81..30222.48 rows=37651 width=82) (actual time=36.517..159.380 rows=29180 loops=4) 439 438 Hash Cond: ((b_2.booking_id)::text = (r.booking_id)::text) 440 Buffers: shared hit=2 5824441 -> Parallel Seq Scan on bookings b_2 (cost=0.00..25448.88 rows=322588 width=78) (actual time=0.01 7..27.723rows=250006 loops=4)439 Buffers: shared hit=24791 440 -> Parallel Seq Scan on bookings b_2 (cost=0.00..25448.88 rows=322588 width=78) (actual time=0.012..34.092 rows=250006 loops=4) 442 441 Buffers: shared hit=22223 443 -> Parallel Hash (cost= 4112.16..4112.16 rows=69716 width=78) (actual time=44.396..44.398 rows=41830 loops=4)444 Buckets: 262144 Batches: 1 Memory Usage: 20448kB445 Buffers: shared hit= 3415446 -> Parallel Seq Scan on reviews r (cost=0.00.. 4112.16 rows=69716 width=78) (actual time=9.212..20.518 rows=41830 loops=4)447 Buffers: shared hit= 3415442 -> Parallel Hash (cost=3068.58..3068.58 rows=68658 width=78) (actual time=35.713..35.714 rows=29180 loops=4) 443 Buckets: 131072 Batches: 1 Memory Usage: 13856kB 444 Buffers: shared hit=2382 445 -> Parallel Seq Scan on reviews r (cost=0.00..3068.58 rows=68658 width=78) (actual time=9.315..16.953 rows=29180 loops=4) 446 Buffers: shared hit=2382 448 447 Planning: 449 Buffers: shared hit= 611450 Planning Time: 4.704ms448 Buffers: shared hit=227 read=5 449 Planning Time: 3.010 ms 451 450 JIT: 452 Functions: 11 6451 Functions: 115 453 452 Options: Inlining false, Optimization false, Expressions true, Deforming true 454 Timing: Generation 9. 344 ms (Deform 4.395 ms), Inlining 0.000 ms, Optimization 4.065 ms, Emission 91.907 ms, Total 105.316ms455 Execution Time: 26 70.429ms456 }}} 457 458 '''Execution time:''' 26 70.429ms453 Timing: Generation 9.518 ms (Deform 4.386 ms), Inlining 0.000 ms, Optimization 4.262 ms, Emission 89.334 ms, Total 103.115 ms 454 Execution Time: 2619.146 ms 455 }}} 456 457 '''Execution time:''' 2619.146 ms 459 458 460 459 We attempt to optimize this analytical query by introducing multiple foreign key indexes: … … 470 469 Execution with indexes: 471 470 {{{ 472 Limit (cost=11 3555.49..113555.51 rows=10 width=176) (actual time=3667.482..3667.503rows=4 loops=1)473 Buffers: shared hit=7 42025, temp read=10108 written=10108471 Limit (cost=110101.62..110101.65 rows=10 width=176) (actual time=3500.078..3500.100 rows=4 loops=1) 472 Buffers: shared hit=738392, temp read=6439 written=6439 474 473 CTE params 475 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.0 13..0.014rows=1 loops=1)476 -> Sort (cost=11 3555.47..113556.04 rows=230 width=176) (actual time=3609.923..3609.942 rows=4 loops=1)474 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.027..0.028 rows=1 loops=1) 475 -> Sort (cost=110101.60..110102.18 rows=230 width=176) (actual time=3442.513..3442.532 rows=4 loops=1) 477 476 Sort Key: (dense_rank() OVER (?)) 478 477 Sort Method: quicksort Memory: 26kB 479 Buffers: shared hit=7 42025, temp read=10108 written=10108480 -> WindowAgg (cost=11 3540.74..113550.50 rows=230 width=176) (actual time=3609.856..3609.894rows=4 loops=1)481 Buffers: shared hit=7 42022, temp read=10108 written=10108482 -> Sort (cost=11 3540.72..113541.30 rows=230 width=152) (actual time=3609.838..3609.856rows=4 loops=1)478 Buffers: shared hit=738392, temp read=6439 written=6439 479 -> WindowAgg (cost=110086.87..110096.63 rows=230 width=176) (actual time=3442.451..3442.485 rows=4 loops=1) 480 Buffers: shared hit=738389, temp read=6439 written=6439 481 -> Sort (cost=110086.86..110087.43 rows=230 width=152) (actual time=3442.434..3442.453 rows=4 loops=1) 483 482 Sort Key: ((((((COALESCE(sf.total_revenue, '0'::bigint))::numeric * 0.5) + ((COALESCE(ss.completed_bookings, '0'::bigint) * 10))::numeric) + (COALESCE(sr.avg_rating, '0'::numeric) * '15'::numeric)) - ((COALESCE(ss.missed_bookings, '0'::bigint) * 5))::numeric)) DESC 484 483 Sort Method: quicksort Memory: 25kB 485 Buffers: shared hit=7 42022, temp read=10108 written=10108486 -> Hash Join (cost=11 3506.05..113531.70 rows=230 width=152) (actual time=3609.810..3609.839rows=4 loops=1)484 Buffers: shared hit=738389, temp read=6439 written=6439 485 -> Hash Join (cost=110052.18..110077.83 rows=230 width=152) (actual time=3442.406..3442.435 rows=4 loops=1) 487 486 Hash Cond: ((u.user_id)::text = (ps.user_id)::text) 488 Buffers: shared hit=7 42019, temp read=10108 written=10108489 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=64) (actual time=0.0 22..0.027rows=12 loops=1)487 Buffers: shared hit=738386, temp read=6439 written=6439 488 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=64) (actual time=0.015..0.020 rows=12 loops=1) 490 489 Buffers: shared hit=1 491 -> Hash (cost=11 3503.17..113503.17 rows=230 width=146) (actual time=3609.749..3609.766rows=4 loops=1)490 -> Hash (cost=110049.30..110049.30 rows=230 width=146) (actual time=3442.349..3442.367 rows=4 loops=1) 492 491 Buckets: 1024 Batches: 1 Memory Usage: 9kB 493 Buffers: shared hit=7 42018, temp read=10108 written=10108494 -> Merge Left Join (cost=11 3498.52..113503.17 rows=230 width=146) (actual time=3609.711..3609.745rows=4 loops=1)492 Buffers: shared hit=738385, temp read=6439 written=6439 493 -> Merge Left Join (cost=110044.65..110049.30 rows=230 width=146) (actual time=3442.306..3442.342 rows=4 loops=1) 495 494 Merge Cond: ((ps.user_id)::text = (sr.sitter_id)::text) 496 Buffers: shared hit=7 42018, temp read=10108 written=10108497 -> Merge Left Join (cost=72 271.51..72275.57 rows=230 width=122) (actual time=2380.858..2380.883rows=4 loops=1)495 Buffers: shared hit=738385, temp read=6439 written=6439 496 -> Merge Left Join (cost=72317.28..72321.34 rows=230 width=122) (actual time=2386.271..2386.298 rows=4 loops=1) 498 497 Merge Cond: ((ps.user_id)::text = (sf.sitter_id)::text) 499 Buffers: shared hit=71 5489500 -> Merge Left Join (cost=28260.39..28263.86 rows=230 width=114) (actual time=7 81.667..781.682rows=4 loops=1)498 Buffers: shared hit=712889 499 -> Merge Left Join (cost=28260.39..28263.86 rows=230 width=114) (actual time=797.385..797.401 rows=4 loops=1) 501 500 Merge Cond: ((ps.user_id)::text = (ss.sitter_id)::text) 502 501 Filter: (COALESCE(ss.total_bookings, '0'::bigint) > 0) 503 502 Buffers: shared hit=23115 504 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.0 18..0.020rows=4 loops=1)503 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.024..0.026 rows=4 loops=1) 505 504 Sort Key: ps.user_id 506 505 Sort Method: quicksort Memory: 25kB 507 506 Buffers: shared hit=1 508 -> Seq Scan on pet_sitters ps (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 07..0.008rows=4 loops=1)507 -> Seq Scan on pet_sitters ps (cost=0.00..16.90 rows=690 width=90) (actual time=0.013..0.014 rows=4 loops=1) 509 508 Buffers: shared hit=1 510 -> Sort (cost=28210.95..28210.96 rows=1 width=61) (actual time=7 81.612..781.617 rows=4 loops=1)509 -> Sort (cost=28210.95..28210.96 rows=1 width=61) (actual time=797.322..797.327 rows=4 loops=1) 511 510 Sort Key: ss.sitter_id 512 511 Sort Method: quicksort Memory: 25kB 513 512 Buffers: shared hit=23114 514 -> Subquery Scan on ss (cost=28210.92..28210.94 rows=1 width=61) (actual time=7 81.595..781.601 rows=4 loops=1)513 -> Subquery Scan on ss (cost=28210.92..28210.94 rows=1 width=61) (actual time=797.304..797.311 rows=4 loops=1) 515 514 Buffers: shared hit=23114 516 -> HashAggregate (cost=28210.92..28210.93 rows=1 width=61) (actual time=7 81.588..781.593rows=4 loops=1)515 -> HashAggregate (cost=28210.92..28210.93 rows=1 width=61) (actual time=797.297..797.302 rows=4 loops=1) 517 516 Group Key: b.sitter_id 518 517 Batches: 1 Memory Usage: 24kB 519 518 Buffers: shared hit=23114 520 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=83) (actual time=4 2.444..423.581rows=1000017 loops=1)519 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=83) (actual time=43.375..439.128 rows=1000017 loops=1) 521 520 Buffers: shared hit=23114 522 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.0 16..0.018rows=1 loops=1)523 -> Bitmap Heap Scan on bookings b (cost=1543.34..25433.05 rows=111114 width=87) (actual time=4 2.408..230.648rows=1000017 loops=1)521 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.030..0.033 rows=1 loops=1) 522 -> Bitmap Heap Scan on bookings b (cost=1543.34..25433.05 rows=111114 width=87) (actual time=43.324..251.336 rows=1000017 loops=1) 524 523 Recheck Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 525 524 Heap Blocks: exact=22223 526 525 Buffers: shared hit=23114 527 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=3 7.787..37.787rows=1000017 loops=1)526 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=38.673..38.674 rows=1000017 loops=1) 528 527 Index Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 529 528 Buffers: shared hit=891 530 -> Sort (cost=440 11.12..44011.12 rows=1 width=45) (actual time=1599.167..1599.172rows=2 loops=1)529 -> Sort (cost=44056.89..44056.90 rows=1 width=45) (actual time=1588.861..1588.866 rows=2 loops=1) 531 530 Sort Key: sf.sitter_id 532 531 Sort Method: quicksort Memory: 25kB 533 Buffers: shared hit=6 92374534 -> Subquery Scan on sf (cost=440 11.09..44011.11 rows=1 width=45) (actual time=1599.144..1599.151rows=2 loops=1)535 Buffers: shared hit=6 92374536 -> HashAggregate (cost=440 11.09..44011.10 rows=1 width=45) (actual time=1599.136..1599.141rows=2 loops=1)532 Buffers: shared hit=689774 533 -> Subquery Scan on sf (cost=44056.86..44056.88 rows=1 width=45) (actual time=1588.837..1588.844 rows=2 loops=1) 534 Buffers: shared hit=689774 535 -> HashAggregate (cost=44056.86..44056.87 rows=1 width=45) (actual time=1588.829..1588.834 rows=2 loops=1) 537 536 Group Key: b_1.sitter_id 538 537 Batches: 1 Memory Usage: 24kB 539 Buffers: shared hit=6 92374540 -> Nested Loop (cost=1520. 69..43917.01 rows=18815 width=41) (actual time=40.913..1546.819 rows=167315 loops=1)541 Buffers: shared hit=6 92374542 -> Nested Loop (cost=1520.2 7..25875.93 rows=18815 width=74) (actual time=40.876..285.030 rows=167315 loops=1)538 Buffers: shared hit=689774 539 -> Nested Loop (cost=1520.72..43962.29 rows=18915 width=41) (actual time=41.086..1537.591 rows=166665 loops=1) 540 Buffers: shared hit=689774 541 -> Nested Loop (cost=1520.29..25876.96 rows=18915 width=74) (actual time=41.016..285.706 rows=166665 loops=1) 543 542 Buffers: shared hit=23114 544 543 -> CTE Scan on params p_1 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.003 rows=1 loops=1) 545 -> Bitmap Heap Scan on bookings b_1 (cost=1520.2 7..25687.76 rows=18815 width=78) (actual time=40.864..253.061 rows=167315 loops=1)544 -> Bitmap Heap Scan on bookings b_1 (cost=1520.29..25687.79 rows=18915 width=78) (actual time=41.004..254.671 rows=166665 loops=1) 546 545 Recheck Cond: ((date_from >= p_1.start_date) AND (date_from < p_1.end_date)) 547 546 Filter: ((status)::text = 'Completed'::text) 548 Rows Removed by Filter: 83 2702547 Rows Removed by Filter: 833352 549 548 Heap Blocks: exact=22223 550 549 Buffers: shared hit=23114 551 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=36. 257..36.257 rows=1000017 loops=1)550 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=36.387..36.387 rows=1000017 loops=1) 552 551 Index Cond: ((date_from >= p_1.start_date) AND (date_from < p_1.end_date)) 553 552 Buffers: shared hit=891 554 -> Index Scan using idx_payments_booking_id on payments pay (cost=0.42..0.96 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=16 7315)553 -> Index Scan using idx_payments_booking_id on payments pay (cost=0.42..0.96 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=166665) 555 554 Index Cond: ((booking_id)::text = (b_1.booking_id)::text) 556 Buffers: shared hit=66 9260557 -> Sort (cost= 41227.01..41227.02 rows=1 width=61) (actual time=1228.829..1228.835 rows=2 loops=1)555 Buffers: shared hit=666660 556 -> Sort (cost=37727.37..37727.37 rows=1 width=61) (actual time=1056.010..1056.015 rows=2 loops=1) 558 557 Sort Key: sr.sitter_id 559 558 Sort Method: quicksort Memory: 25kB 560 Buffers: shared hit=2 6529, temp read=10108 written=10108561 -> Subquery Scan on sr (cost= 41226.98..41227.00 rows=1 width=61) (actual time=1228.801..1228.810rows=2 loops=1)562 Buffers: shared hit=2 6529, temp read=10108 written=10108563 -> HashAggregate (cost= 41226.98..41226.99 rows=1 width=61) (actual time=1228.793..1228.801rows=2 loops=1)559 Buffers: shared hit=25496, temp read=6439 written=6439 560 -> Subquery Scan on sr (cost=37727.33..37727.36 rows=1 width=61) (actual time=1055.985..1055.994 rows=2 loops=1) 561 Buffers: shared hit=25496, temp read=6439 written=6439 562 -> HashAggregate (cost=37727.33..37727.35 rows=1 width=61) (actual time=1055.977..1055.984 rows=2 loops=1) 564 563 Group Key: b_2.sitter_id 565 564 Batches: 1 Memory Usage: 24kB 566 Buffers: shared hit=2 6529, temp read=10108 written=10108567 -> Hash Join (cost= 10848.00..41087.54 rows=18591 width=78) (actual time=157.648..1174.825 rows=167316loops=1)565 Buffers: shared hit=25496, temp read=6439 written=6439 566 -> Hash Join (cost=8033.52..37630.07 rows=12969 width=78) (actual time=121.902..1017.507 rows=116717 loops=1) 568 567 Hash Cond: ((b_2.booking_id)::text = (r.booking_id)::text) 569 Buffers: shared hit=2 6529, temp read=10108 written=10108570 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=74) (actual time=40.1 33..417.946rows=1000017 loops=1)568 Buffers: shared hit=25496, temp read=6439 written=6439 569 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=74) (actual time=40.177..418.927 rows=1000017 loops=1) 571 570 Buffers: shared hit=23114 572 -> CTE Scan on params p_2 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.00 2rows=1 loops=1)573 -> Bitmap Heap Scan on bookings b_2 (cost=1543.34..25433.05 rows=111114 width=78) (actual time=40.1 13..221.102rows=1000017 loops=1)571 -> CTE Scan on params p_2 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.003 rows=1 loops=1) 572 -> Bitmap Heap Scan on bookings b_2 (cost=1543.34..25433.05 rows=111114 width=78) (actual time=40.156..222.658 rows=1000017 loops=1) 574 573 Recheck Cond: ((date_from >= p_2.start_date) AND (date_from < p_2.end_date)) 575 574 Heap Blocks: exact=22223 576 575 Buffers: shared hit=23114 577 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=35.5 09..35.509rows=1000017 loops=1)576 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=35.548..35.548 rows=1000017 loops=1) 578 577 Index Cond: ((date_from >= p_2.start_date) AND (date_from < p_2.end_date)) 579 578 Buffers: shared hit=891 580 -> Hash (cost= 5088.18..5088.18 rows=167318 width=78) (actual time=117.367..117.368 rows=167319 loops=1)581 Buckets: 131072 Batches: 4 Memory Usage: 5507kB582 Buffers: shared hit= 3415, temp written=1501583 -> Seq Scan on reviews r (cost=0.00.. 5088.18 rows=167318 width=78) (actual time=0.026..42.889 rows=167319 loops=1)584 Buffers: shared hit= 3415579 -> Hash (cost=3549.19..3549.19 rows=116719 width=78) (actual time=81.514..81.515 rows=116719 loops=1) 580 Buckets: 131072 Batches: 2 Memory Usage: 7251kB 581 Buffers: shared hit=2382, temp written=702 582 -> Seq Scan on reviews r (cost=0.00..3549.19 rows=116719 width=78) (actual time=0.025..30.472 rows=116719 loops=1) 583 Buffers: shared hit=2382 585 584 Planning: 586 Buffers: shared hit=6 57587 Planning Time: 4.960ms585 Buffers: shared hit=668 586 Planning Time: 5.315 ms 588 587 JIT: 589 588 Functions: 81 590 589 Options: Inlining false, Optimization false, Expressions true, Deforming true 591 Timing: Generation 6.320 ms (Deform 2.740 ms), Inlining 0.000 ms, Optimization 2.235 ms, Emission 55.696 ms, Total 64.251 ms 592 Execution Time: 3709.157 ms 593 594 }}} 595 596 '''Execution time:''' 3709.157 ms 597 598 Instead of improving performance, adding these indexes actually '''increased''' the execution time by roughly 1 second, therefore we are '''not''' keeping the indexes. 590 Timing: Generation 6.369 ms (Deform 2.765 ms), Inlining 0.000 ms, Optimization 2.281 ms, Emission 55.664 ms, Total 64.314 ms 591 Execution Time: 3540.806 ms 592 }}} 593 594 '''Execution time:''' 3540.806 ms 595 596 Instead of improving performance, adding these indexes actually '''increased''' the execution time by nearly 1 second. The indexes are also used in Scenario 4 below. 599 597 600 598 === 4. Highest Paying Customers Analytics (Phase 6 Query) === … … 665 663 Execution without indexes: 666 664 {{{ 667 Limit (cost=1 61052.28..161052.30 rows=10 width=115) (actual time=5023.716..5023.735rows=2 loops=1)668 Buffers: shared hit=7 30190, temp read=26867 written=26872665 Limit (cost=170067.13..170067.15 rows=10 width=115) (actual time=5322.701..5322.720 rows=2 loops=1) 666 Buffers: shared hit=726269, temp read=26853 written=26858 669 667 CTE params 670 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.013..0.01 3rows=1 loops=1)671 -> Sort (cost=1 61052.26..161052.83 rows=230 width=115) (actual time=4971.843..4971.859 rows=2 loops=1)668 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.013..0.014 rows=1 loops=1) 669 -> Sort (cost=170067.11..170067.68 rows=230 width=115) (actual time=5264.981..5264.999 rows=2 loops=1) 672 670 Sort Key: (dense_rank() OVER (?)) 673 671 Sort Method: quicksort Memory: 25kB 674 Buffers: shared hit=7 30190, temp read=26867 written=26872675 -> WindowAgg (cost=1 61042.71..161047.29 rows=230 width=115) (actual time=4971.808..4971.835rows=2 loops=1)676 Buffers: shared hit=7 30190, temp read=26867 written=26872677 -> Sort (cost=1 61042.69..161043.26 rows=230 width=349) (actual time=4971.773..4971.789rows=2 loops=1)672 Buffers: shared hit=726269, temp read=26853 written=26858 673 -> WindowAgg (cost=170057.56..170062.14 rows=230 width=115) (actual time=5264.923..5264.951 rows=2 loops=1) 674 Buffers: shared hit=726266, temp read=26853 written=26858 675 -> Sort (cost=170057.54..170058.11 rows=230 width=349) (actual time=5264.888..5264.905 rows=2 loops=1) 678 676 Sort Key: (COALESCE((sum(pay.amount)), '0'::bigint)) DESC, (COALESCE((count(DISTINCT b.booking_id)), '0'::bigint)) DESC 679 677 Sort Method: quicksort Memory: 25kB 680 Buffers: shared hit=7 30190, temp read=26867 written=26872681 -> Hash Join (cost=16 0830.10..161033.66 rows=230 width=349) (actual time=4755.875..4971.782rows=2 loops=1)678 Buffers: shared hit=726266, temp read=26853 written=26858 679 -> Hash Join (cost=169840.97..170048.52 rows=230 width=349) (actual time=5049.927..5264.888 rows=2 loops=1) 682 680 Hash Cond: ((po.user_id)::text = (u.user_id)::text) 683 Buffers: shared hit=7 30190, temp read=26867 written=26872684 -> Merge Left Join (cost=16 0807.05..161010.01 rows=230 width=388) (actual time=4755.799..4971.700rows=2 loops=1)681 Buffers: shared hit=726266, temp read=26853 written=26858 682 -> Merge Left Join (cost=169817.92..170024.86 rows=230 width=388) (actual time=5049.843..5264.798 rows=2 loops=1) 685 683 Merge Cond: ((po.user_id)::text = (pets.owner_id)::text) 686 Buffers: shared hit=7 30189, temp read=26867 written=26872687 -> Merge Left Join (cost=16 0791.71..160990.09 rows=230 width=380) (actual time=4755.644..4971.535 rows=2 loops=1)684 Buffers: shared hit=726265, temp read=26853 written=26858 685 -> Merge Left Join (cost=169816.17..170021.77 rows=230 width=380) (actual time=5049.710..5264.655 rows=2 loops=1) 688 686 Merge Cond: ((po.user_id)::text = (service_counts.owner_id)::text) 689 Buffers: shared hit=7 30188, temp read=26867 written=26872690 -> Merge Left Join (cost= 57602.12..57793.76 rows=230 width=106) (actual time=2088.524..2304.386rows=2 loops=1)687 Buffers: shared hit=726264, temp read=26853 written=26858 688 -> Merge Left Join (cost=61806.32..61998.99 rows=230 width=106) (actual time=2266.934..2481.844 rows=2 loops=1) 691 689 Merge Cond: ((po.user_id)::text = (b.owner_id)::text) 692 690 Filter: (COALESCE((count(DISTINCT b.booking_id)), '0'::bigint) > 0) 693 691 Rows Removed by Filter: 5 694 Buffers: shared hit=6 91484, temp read=1865 written=1870695 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.0 23..0.027rows=7 loops=1)692 Buffers: shared hit=688884, temp read=1858 written=1863 693 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.040..0.044 rows=7 loops=1) 696 694 Sort Key: po.user_id 697 695 Sort Method: quicksort Memory: 25kB 698 696 Buffers: shared hit=1 699 -> Seq Scan on pet_owners po (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 09..0.011rows=7 loops=1)697 -> Seq Scan on pet_owners po (cost=0.00..16.90 rows=690 width=90) (actual time=0.027..0.028 rows=7 loops=1) 700 698 Buffers: shared hit=1 701 -> Materialize (cost= 57552.69..57740.86 rows=1 width=53) (actual time=2088.462..2304.313rows=2 loops=1)702 Buffers: shared hit=6 91483, temp read=1865 written=1870703 -> GroupAggregate (cost= 57552.69..57740.85 rows=1 width=53) (actual time=2088.458..2304.305rows=2 loops=1)699 -> Materialize (cost=61756.88..61946.08 rows=2 width=53) (actual time=2266.857..2481.755 rows=2 loops=1) 700 Buffers: shared hit=688883, temp read=1858 written=1863 701 -> GroupAggregate (cost=61756.88..61946.05 rows=2 width=53) (actual time=2266.852..2481.746 rows=2 loops=1) 704 702 Group Key: b.owner_id 705 Buffers: shared hit=6 91483, temp read=1865 written=1870706 -> Sort (cost= 57552.69..57599.73 rows=18815 width=78) (actual time=2088.388..2265.596 rows=167315 loops=1)703 Buffers: shared hit=688883, temp read=1858 written=1863 704 -> Sort (cost=61756.88..61804.17 rows=18915 width=78) (actual time=2266.784..2442.950 rows=166665 loops=1) 707 705 Sort Key: b.owner_id, b.booking_id 708 Sort Method: external merge Disk: 14 920kB709 Buffers: shared hit=6 91483, temp read=1865 written=1870710 -> Nested Loop (cost=0.42.. 56216.86 rows=18815 width=78) (actual time=0.077..1445.651 rows=167315 loops=1)711 Buffers: shared hit=6 91483712 -> Nested Loop (cost=0.00..372 63.35 rows=18815 width=74) (actual time=0.043..218.948 rows=167315 loops=1)706 Sort Method: external merge Disk: 14864kB 707 Buffers: shared hit=688883, temp read=1858 written=1863 708 -> Nested Loop (cost=0.42..60413.23 rows=18915 width=78) (actual time=0.185..1616.303 rows=166665 loops=1) 709 Buffers: shared hit=688883 710 -> Nested Loop (cost=0.00..37276.85 rows=18915 width=74) (actual time=0.057..274.855 rows=166665 loops=1) 713 711 Join Filter: ((b.date_from >= p.start_date) AND (b.date_from < p.end_date)) 714 712 Rows Removed by Join Filter: 3 715 713 Buffers: shared hit=22223 716 714 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.015..0.017 rows=1 loops=1) 717 -> Seq Scan on bookings b (cost=0.00..34723.28 rows=1 69337 width=78) (actual time=0.015..182.152 rows=167318 loops=1)715 -> Seq Scan on bookings b (cost=0.00..34723.28 rows=170237 width=78) (actual time=0.026..235.609 rows=166668 loops=1) 718 716 Filter: ((status)::text = 'Completed'::text) 719 Rows Removed by Filter: 83 2704717 Rows Removed by Filter: 833354 720 718 Buffers: shared hit=22223 721 -> Index Scan using payments_booking_id_key on payments pay (cost=0.42..1. 01 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=167315)719 -> Index Scan using payments_booking_id_key on payments pay (cost=0.42..1.22 rows=1 width=41) (actual time=0.008..0.008 rows=1 loops=166665) 722 720 Index Cond: ((booking_id)::text = (b.booking_id)::text) 723 Buffers: shared hit=66 9260724 -> Materialize (cost=10 3189.58..103195.74 rows=1 width=311) (actual time=2667.097..2667.120 rows=2 loops=1)725 Buffers: shared hit=3 8704, temp read=25002 written=25002726 -> Subquery Scan on service_counts (cost=10 3189.58..103195.74 rows=1 width=311) (actual time=2667.092..2667.114rows=2 loops=1)721 Buffers: shared hit=666660 722 -> Materialize (cost=108009.85..108022.19 rows=2 width=311) (actual time=2782.752..2782.780 rows=2 loops=1) 723 Buffers: shared hit=37380, temp read=24995 written=24995 724 -> Subquery Scan on service_counts (cost=108009.85..108022.18 rows=2 width=311) (actual time=2782.740..2782.766 rows=2 loops=1) 727 725 Filter: (service_counts.rank_num = 1) 728 Buffers: shared hit=3 8704, temp read=25002 written=25002729 -> WindowAgg (cost=10 3189.58..103193.36 rows=190 width=327) (actual time=2667.085..2667.106 rows=2 loops=1)726 Buffers: shared hit=37380, temp read=24995 written=24995 727 -> WindowAgg (cost=108009.85..108017.43 rows=380 width=327) (actual time=2782.732..2782.756 rows=2 loops=1) 730 728 Run Condition: (row_number() OVER (?) <= 1) 731 Buffers: shared hit=3 8704, temp read=25002 written=25002732 -> Sort (cost=10 3189.56..103190.04 rows=190 width=319) (actual time=2667.062..2667.070rows=5 loops=1)729 Buffers: shared hit=37380, temp read=24995 written=24995 730 -> Sort (cost=108009.83..108010.78 rows=380 width=319) (actual time=2782.709..2782.717 rows=5 loops=1) 733 731 Sort Key: b_1.owner_id, (count(bs.service_id)) DESC 734 732 Sort Method: quicksort Memory: 25kB 735 Buffers: shared hit=3 8704, temp read=25002 written=25002736 -> HashAggregate (cost=10 3180.47..103182.37 rows=190 width=319) (actual time=2667.034..2667.043rows=5 loops=1)733 Buffers: shared hit=37380, temp read=24995 written=24995 734 -> HashAggregate (cost=107989.75..107993.55 rows=380 width=319) (actual time=2782.681..2782.692 rows=5 loops=1) 737 735 Group Key: b_1.owner_id, s.type 738 Batches: 1 Memory Usage: 40kB739 Buffers: shared hit=3 8704, temp read=25002 written=25002740 -> Hash Join (cost= 44715.49..102347.12 rows=111114 width=348) (actual time=1301.306..2326.037rows=1000017 loops=1)736 Batches: 1 Memory Usage: 37kB 737 Buffers: shared hit=37380, temp read=24995 written=24995 738 -> Hash Join (cost=50037.77..107156.39 rows=111114 width=348) (actual time=792.873..2447.981 rows=1000017 loops=1) 741 739 Hash Cond: ((bs.service_id)::text = (s.service_id)::text) 742 Buffers: shared hit=3 8704, temp read=25002 written=25002743 -> Hash Join (cost= 44701.22..102034.66 rows=111114 width=74) (actual time=1301.240..2045.345rows=1000017 loops=1)740 Buffers: shared hit=37380, temp read=24995 written=24995 741 -> Hash Join (cost=50023.50..106843.94 rows=111114 width=74) (actual time=792.796..2158.768 rows=1000017 loops=1) 744 742 Hash Cond: ((bs.booking_id)::text = (b_1.booking_id)::text) 745 Buffers: shared hit=38703, temp read=25002 written=25002 746 -> Seq Scan on booking_services bs (cost=0.00..25669.22 rows=1000022 width=74) (actual time=0.017..158.668 rows=1000022 loops=1) 747 Buffers: shared hit=15669 748 -> Hash (cost=41901.29..41901.29 rows=111114 width=74) (actual time=844.625..844.628 rows=1000017 loops=1) 749 Buckets: 131072 (originally 131072) Batches: 16 (originally 2) Memory Usage: 7550kB 750 Buffers: shared hit=23034, temp written=10744 751 -> Nested Loop (cost=16900.42..41901.29 rows=111114 width=74) (actual time=41.046..409.694 rows=1000017 loops=1) 752 Buffers: shared hit=23034 743 Buffers: shared hit=37379, temp read=24995 written=24995 744 -> Seq Scan on booking_services bs (cost=0.00..25156.22 rows=1000022 width=74) (actual time=0.034..182.963 rows=1000022 loops=1) 745 Buffers: shared hit=15156 746 -> Hash (cost=47223.57..47223.57 rows=111114 width=74) (actual time=789.943..789.945 rows=1000017 loops=1) 747 Buckets: 131072 (originally 131072) Batches: 16 (originally 2) Memory Usage: 7487kB 748 Buffers: shared hit=22223, temp written=10750 749 -> Nested Loop (cost=0.00..47223.57 rows=111114 width=74) (actual time=0.034..347.150 rows=1000017 loops=1) 750 Join Filter: ((b_1.date_from >= p_1.start_date) AND (b_1.date_from < p_1.end_date)) 751 Rows Removed by Join Filter: 5 752 Buffers: shared hit=22223 753 753 -> CTE Scan on params p_1 (cost=0.00..0.02 rows=1 width=8) (actual time=0.002..0.004 rows=1 loops=1) 754 -> Bitmap Heap Scan on bookings b_1 (cost=16900.42..40790.13 rows=111114 width=78) (actual time=41.023..223.873 rows=1000017 loops=1) 755 Recheck Cond: ((date_from >= p_1.start_date) AND (date_from < p_1.end_date)) 756 Heap Blocks: exact=22223 757 Buffers: shared hit=23034 758 -> Bitmap Index Scan on idx_bookings_owner_date (cost=0.00..16872.65 rows=111114 width=0) (actual time=36.458..36.459 rows=1000017 loops=1) 759 Index Cond: ((date_from >= p_1.start_date) AND (date_from < p_1.end_date)) 760 Buffers: shared hit=811 761 -> Hash (cost=11.90..11.90 rows=190 width=364) (actual time=0.037..0.038 rows=4 loops=1) 754 -> Seq Scan on bookings b_1 (cost=0.00..32223.22 rows=1000022 width=78) (actual time=0.010..102.527 rows=1000022 loops=1) 755 Buffers: shared hit=22223 756 -> Hash (cost=11.90..11.90 rows=190 width=364) (actual time=0.044..0.045 rows=4 loops=1) 762 757 Buckets: 1024 Batches: 1 Memory Usage: 9kB 763 758 Buffers: shared hit=1 764 -> Seq Scan on services s (cost=0.00..11.90 rows=190 width=364) (actual time=0.0 26..0.027rows=4 loops=1)759 -> Seq Scan on services s (cost=0.00..11.90 rows=190 width=364) (actual time=0.031..0.033 rows=4 loops=1) 765 760 Buffers: shared hit=1 766 -> GroupAggregate (cost=1 5.34..17.44 rows=120 width=98) (actual time=0.103..0.115 rows=7loops=1)761 -> GroupAggregate (cost=1.75..2.15 rows=23 width=98) (actual time=0.093..0.104 rows=6 loops=1) 767 762 Group Key: pets.owner_id 768 763 Buffers: shared hit=1 769 -> Sort (cost=1 5.34..15.64 rows=120 width=180) (actual time=0.078..0.081 rows=23loops=1)764 -> Sort (cost=1.75..1.81 rows=23 width=180) (actual time=0.068..0.071 rows=16 loops=1) 770 765 Sort Key: pets.owner_id 771 Sort Method: quicksort Memory: 2 7kB766 Sort Method: quicksort Memory: 26kB 772 767 Buffers: shared hit=1 773 -> Seq Scan on pets (cost=0.00..1 1.20 rows=120 width=180) (actual time=0.044..0.051 rows=23loops=1)768 -> Seq Scan on pets (cost=0.00..1.23 rows=23 width=180) (actual time=0.030..0.034 rows=16 loops=1) 774 769 Buffers: shared hit=1 775 -> Hash (cost=15.80..15.80 rows=580 width=51) (actual time=0.0 44..0.045rows=12 loops=1)770 -> Hash (cost=15.80..15.80 rows=580 width=51) (actual time=0.057..0.058 rows=12 loops=1) 776 771 Buckets: 1024 Batches: 1 Memory Usage: 9kB 777 772 Buffers: shared hit=1 778 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=51) (actual time=0.0 27..0.030rows=12 loops=1)773 -> Seq Scan on users u (cost=0.00..15.80 rows=580 width=51) (actual time=0.033..0.036 rows=12 loops=1) 779 774 Buffers: shared hit=1 780 775 Planning: 781 Buffers: shared hit= 34782 Planning Time: 1.751ms776 Buffers: shared hit=570 777 Planning Time: 4.934 ms 783 778 JIT: 784 Functions: 8 6779 Functions: 85 785 780 Options: Inlining false, Optimization false, Expressions true, Deforming true 786 Timing: Generation 4.189 ms (Deform 1.818 ms), Inlining 0.000 ms, Optimization 1.556 ms, Emission 50.729 ms, Total 56.474 ms 787 Execution Time: 5033.000 ms 788 789 }}} 790 791 '''Execution time:''' 5033.000 ms 781 Timing: Generation 6.167 ms (Deform 2.815 ms), Inlining 0.000 ms, Optimization 2.209 ms, Emission 55.891 ms, Total 64.267 ms 782 Execution Time: 5367.798 ms 783 }}} 784 785 '''Execution time:''' 5367.798 ms 792 786 793 787 Execution with the indexes we created in Scenario 3: 794 788 {{{ 795 Limit (cost=133 693.17..133693.19 rows=10 width=115) (actual time=11494.356..11494.375rows=2 loops=1)796 Buffers: shared hit=470 8355 read=7205, temp read=1865 written=1870789 Limit (cost=133535.97..133535.99 rows=10 width=115) (actual time=11115.289..11115.307 rows=2 loops=1) 790 Buffers: shared hit=4705758 read=7205, temp read=1858 written=1863 797 791 CTE params 798 792 -> Result (cost=0.00..0.02 rows=1 width=8) (actual time=0.013..0.014 rows=1 loops=1) 799 -> Sort (cost=133 693.15..133693.72 rows=230 width=115) (actual time=11443.882..11443.898rows=2 loops=1)793 -> Sort (cost=133535.95..133536.52 rows=230 width=115) (actual time=11060.413..11060.429 rows=2 loops=1) 800 794 Sort Key: (dense_rank() OVER (?)) 801 795 Sort Method: quicksort Memory: 25kB 802 Buffers: shared hit=470 8355 read=7205, temp read=1865 written=1870803 -> WindowAgg (cost=133 683.60..133688.18 rows=230 width=115) (actual time=11443.849..11443.877 rows=2 loops=1)804 Buffers: shared hit=470 8355 read=7205, temp read=1865 written=1870805 -> Sort (cost=133 683.58..133684.15 rows=230 width=349) (actual time=11443.814..11443.830rows=2 loops=1)796 Buffers: shared hit=4705758 read=7205, temp read=1858 written=1863 797 -> WindowAgg (cost=133526.40..133530.98 rows=230 width=115) (actual time=11060.340..11060.367 rows=2 loops=1) 798 Buffers: shared hit=4705755 read=7205, temp read=1858 written=1863 799 -> Sort (cost=133526.38..133526.95 rows=230 width=349) (actual time=11060.310..11060.326 rows=2 loops=1) 806 800 Sort Key: (COALESCE((sum(pay.amount)), '0'::bigint)) DESC, (COALESCE((count(DISTINCT b.booking_id)), '0'::bigint)) DESC 807 801 Sort Method: quicksort Memory: 25kB 808 Buffers: shared hit=470 8355 read=7205, temp read=1865 written=1870809 -> Hash Join (cost=133 474.23..133674.56 rows=230 width=349) (actual time=11228.070..11443.823rows=2 loops=1)802 Buffers: shared hit=4705755 read=7205, temp read=1858 written=1863 803 -> Hash Join (cost=133310.05..133517.35 rows=230 width=349) (actual time=10845.768..11060.317 rows=2 loops=1) 810 804 Hash Cond: ((po.user_id)::text = (u.user_id)::text) 811 Buffers: shared hit=470 8355 read=7205, temp read=1865 written=1870812 -> Merge Left Join (cost=133 451.18..133650.90 rows=230 width=388) (actual time=11227.999..11443.746rows=2 loops=1)805 Buffers: shared hit=4705755 read=7205, temp read=1858 written=1863 806 -> Merge Left Join (cost=133287.00..133493.70 rows=230 width=388) (actual time=10845.686..11060.231 rows=2 loops=1) 813 807 Merge Cond: ((po.user_id)::text = (pets.owner_id)::text) 814 Buffers: shared hit=470 8354 read=7205, temp read=1865 written=1870815 -> Merge Left Join (cost=133 449.43..133647.81 rows=230 width=380) (actual time=11227.862..11443.598rows=2 loops=1)808 Buffers: shared hit=4705754 read=7205, temp read=1858 written=1863 809 -> Merge Left Join (cost=133285.52..133491.12 rows=230 width=380) (actual time=10845.563..11060.100 rows=2 loops=1) 816 810 Merge Cond: ((po.user_id)::text = (service_counts.owner_id)::text) 817 Buffers: shared hit=470 8353 read=7205, temp read=1865 written=1870818 -> Merge Left Join (cost=453 02.27..45493.91 rows=230 width=106) (actual time=2137.209..2352.916rows=2 loops=1)811 Buffers: shared hit=4705753 read=7205, temp read=1858 written=1863 812 -> Merge Left Join (cost=45355.37..45548.05 rows=230 width=106) (actual time=2279.274..2493.782 rows=2 loops=1) 819 813 Merge Cond: ((po.user_id)::text = (b.owner_id)::text) 820 814 Filter: (COALESCE((count(DISTINCT b.booking_id)), '0'::bigint) > 0) 821 815 Rows Removed by Filter: 5 822 Buffers: shared hit=6 92375, temp read=1865 written=1870823 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.0 21..0.025rows=7 loops=1)816 Buffers: shared hit=689775, temp read=1858 written=1863 817 -> Sort (cost=49.44..51.16 rows=690 width=90) (actual time=0.048..0.053 rows=7 loops=1) 824 818 Sort Key: po.user_id 825 819 Sort Method: quicksort Memory: 25kB 826 820 Buffers: shared hit=1 827 -> Seq Scan on pet_owners po (cost=0.00..16.90 rows=690 width=90) (actual time=0.0 09..0.011rows=7 loops=1)821 -> Seq Scan on pet_owners po (cost=0.00..16.90 rows=690 width=90) (actual time=0.036..0.038 rows=7 loops=1) 828 822 Buffers: shared hit=1 829 -> Materialize (cost=45 252.84..45441.01 rows=1 width=53) (actual time=2137.153..2352.849rows=2 loops=1)830 Buffers: shared hit=6 92374, temp read=1865 written=1870831 -> GroupAggregate (cost=45 252.84..45441.00 rows=1 width=53) (actual time=2137.148..2352.840rows=2 loops=1)823 -> Materialize (cost=45305.94..45495.13 rows=2 width=53) (actual time=2279.186..2493.682 rows=2 loops=1) 824 Buffers: shared hit=689774, temp read=1858 written=1863 825 -> GroupAggregate (cost=45305.94..45495.11 rows=2 width=53) (actual time=2279.181..2493.674 rows=2 loops=1) 832 826 Group Key: b.owner_id 833 Buffers: shared hit=6 92374, temp read=1865 written=1870834 -> Sort (cost=45 252.84..45299.88 rows=18815 width=78) (actual time=2137.081..2313.817 rows=167315 loops=1)827 Buffers: shared hit=689774, temp read=1858 written=1863 828 -> Sort (cost=45305.94..45353.23 rows=18915 width=78) (actual time=2279.104..2454.786 rows=166665 loops=1) 835 829 Sort Key: b.owner_id, b.booking_id 836 Sort Method: external merge Disk: 14 920kB837 Buffers: shared hit=6 92374, temp read=1865 written=1870838 -> Nested Loop (cost=1520. 69..43917.01 rows=18815 width=78) (actual time=40.128..1500.355 rows=167315 loops=1)839 Buffers: shared hit=6 92374840 -> Nested Loop (cost=1520.2 7..25875.93 rows=18815 width=74) (actual time=40.077..281.535 rows=167315 loops=1)830 Sort Method: external merge Disk: 14864kB 831 Buffers: shared hit=689774, temp read=1858 written=1863 832 -> Nested Loop (cost=1520.72..43962.29 rows=18915 width=78) (actual time=43.262..1631.393 rows=166665 loops=1) 833 Buffers: shared hit=689774 834 -> Nested Loop (cost=1520.29..25876.96 rows=18915 width=74) (actual time=43.200..340.897 rows=166665 loops=1) 841 835 Buffers: shared hit=23114 842 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.0 16..0.018rows=1 loops=1)843 -> Bitmap Heap Scan on bookings b (cost=1520.2 7..25687.76 rows=18815 width=78) (actual time=40.052..253.659 rows=167315 loops=1)836 -> CTE Scan on params p (cost=0.00..0.02 rows=1 width=8) (actual time=0.020..0.022 rows=1 loops=1) 837 -> Bitmap Heap Scan on bookings b (cost=1520.29..25687.79 rows=18915 width=78) (actual time=43.171..311.549 rows=166665 loops=1) 844 838 Recheck Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 845 839 Filter: ((status)::text = 'Completed'::text) 846 Rows Removed by Filter: 83 2702840 Rows Removed by Filter: 833352 847 841 Heap Blocks: exact=22223 848 842 Buffers: shared hit=23114 849 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=3 5.462..35.463rows=1000017 loops=1)843 -> Bitmap Index Scan on idx_bookings_date_owner (cost=0.00..1515.57 rows=111114 width=0) (actual time=38.503..38.504 rows=1000017 loops=1) 850 844 Index Cond: ((date_from >= p.start_date) AND (date_from < p.end_date)) 851 845 Buffers: shared hit=891 852 -> Index Scan using idx_payments_booking_id on payments pay (cost=0.42..0.96 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=16 7315)846 -> Index Scan using idx_payments_booking_id on payments pay (cost=0.42..0.96 rows=1 width=41) (actual time=0.007..0.007 rows=1 loops=166665) 853 847 Index Cond: ((booking_id)::text = (b.booking_id)::text) 854 Buffers: shared hit=66 9260855 -> Materialize (cost=8 8147.15..88153.31 rows=1 width=311) (actual time=9090.628..9090.652rows=2 loops=1)848 Buffers: shared hit=666660 849 -> Materialize (cost=87930.14..87942.48 rows=2 width=311) (actual time=8566.266..8566.290 rows=2 loops=1) 856 850 Buffers: shared hit=4015978 read=7205 857 -> Subquery Scan on service_counts (cost=8 8147.15..88153.31 rows=1 width=311) (actual time=9090.623..9090.645rows=2 loops=1)851 -> Subquery Scan on service_counts (cost=87930.14..87942.47 rows=2 width=311) (actual time=8566.261..8566.283 rows=2 loops=1) 858 852 Filter: (service_counts.rank_num = 1) 859 853 Buffers: shared hit=4015978 read=7205 860 -> WindowAgg (cost=8 8147.15..88150.93 rows=190 width=327) (actual time=9090.616..9090.636rows=2 loops=1)854 -> WindowAgg (cost=87930.14..87937.72 rows=380 width=327) (actual time=8566.254..8566.274 rows=2 loops=1) 861 855 Run Condition: (row_number() OVER (?) <= 1) 862 856 Buffers: shared hit=4015978 read=7205 863 -> Sort (cost=8 8147.13..88147.61 rows=190 width=319) (actual time=9090.592..9090.598rows=5 loops=1)857 -> Sort (cost=87930.12..87931.07 rows=380 width=319) (actual time=8566.230..8566.236 rows=5 loops=1) 864 858 Sort Key: b_1.owner_id, (count(bs.service_id)) DESC 865 859 Sort Method: quicksort Memory: 25kB 866 860 Buffers: shared hit=4015978 read=7205 867 -> HashAggregate (cost=8 8138.04..88139.94 rows=190 width=319) (actual time=9090.565..9090.573rows=5 loops=1)861 -> HashAggregate (cost=87910.04..87913.84 rows=380 width=319) (actual time=8566.200..8566.209 rows=5 loops=1) 868 862 Group Key: b_1.owner_id, s.type 869 Batches: 1 Memory Usage: 40kB863 Batches: 1 Memory Usage: 37kB 870 864 Buffers: shared hit=4015978 read=7205 871 -> Hash Join (cost=1558.04..87 304.69 rows=111114 width=348) (actual time=40.705..8657.362rows=1000017 loops=1)865 -> Hash Join (cost=1558.04..87076.68 rows=111114 width=348) (actual time=41.196..8143.541 rows=1000017 loops=1) 872 866 Hash Cond: ((bs.service_id)::text = (s.service_id)::text) 873 867 Buffers: shared hit=4015978 read=7205 874 -> Nested Loop (cost=1543.77..86 992.23 rows=111114 width=74) (actual time=40.620..8294.445rows=1000017 loops=1)868 -> Nested Loop (cost=1543.77..86764.23 rows=111114 width=74) (actual time=41.122..7819.032 rows=1000017 loops=1) 875 869 Buffers: shared hit=4015977 read=7205 876 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=74) (actual time=4 0.489..464.589rows=1000017 loops=1)870 -> Nested Loop (cost=1543.34..26544.21 rows=111114 width=74) (actual time=41.009..461.885 rows=1000017 loops=1) 877 871 Buffers: shared hit=23114 878 -> CTE Scan on params p_1 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.00 3rows=1 loops=1)879 -> Bitmap Heap Scan on bookings b_1 (cost=1543.34..25433.05 rows=111114 width=78) (actual time=40. 468..260.041 rows=1000017 loops=1)872 -> CTE Scan on params p_1 (cost=0.00..0.02 rows=1 width=8) (actual time=0.001..0.004 rows=1 loops=1) 873 -> Bitmap Heap Scan on bookings b_1 (cost=1543.34..25433.05 rows=111114 width=78) (actual time=40.988..259.041 rows=1000017 loops=1) 880 874 Recheck Cond: ((date_from >= p_1.start_date) AND (date_from < p_1.end_date)) 881 875 Heap Blocks: exact=22223 … … 887 881 Index Cond: ((booking_id)::text = (b_1.booking_id)::text) 888 882 Buffers: shared hit=3992863 read=7205 889 -> Hash (cost=11.90..11.90 rows=190 width=364) (actual time=0.0 54..0.055rows=4 loops=1)883 -> Hash (cost=11.90..11.90 rows=190 width=364) (actual time=0.042..0.043 rows=4 loops=1) 890 884 Buckets: 1024 Batches: 1 Memory Usage: 9kB 891 885 Buffers: shared hit=1 892 -> Seq Scan on services s (cost=0.00..11.90 rows=190 width=364) (actual time=0.0 41..0.043rows=4 loops=1)886 -> Seq Scan on services s (cost=0.00..11.90 rows=190 width=364) (actual time=0.030..0.031 rows=4 loops=1) 893 887 Buffers: shared hit=1 894 -> GroupAggregate (cost=1. 75..2.15 rows=23 width=98) (actual time=0.109..0.122 rows=7loops=1)888 -> GroupAggregate (cost=1.48..1.76 rows=16 width=98) (actual time=0.096..0.105 rows=6 loops=1) 895 889 Group Key: pets.owner_id 896 890 Buffers: shared hit=1 897 -> Sort (cost=1. 75..1.81 rows=23 width=180) (actual time=0.084..0.088 rows=23loops=1)891 -> Sort (cost=1.48..1.52 rows=16 width=180) (actual time=0.060..0.062 rows=16 loops=1) 898 892 Sort Key: pets.owner_id 899 Sort Method: quicksort Memory: 2 7kB893 Sort Method: quicksort Memory: 26kB 900 894 Buffers: shared hit=1 901 -> Seq Scan on pets (cost=0.00..1. 23 rows=23 width=180) (actual time=0.049..0.056 rows=23loops=1)895 -> Seq Scan on pets (cost=0.00..1.16 rows=16 width=180) (actual time=0.029..0.034 rows=16 loops=1) 902 896 Buffers: shared hit=1 903 897 -> Hash (cost=15.80..15.80 rows=580 width=51) (actual time=0.046..0.047 rows=12 loops=1) … … 907 901 Buffers: shared hit=1 908 902 Planning: 909 Buffers: shared hit= 163read=6 dirtied=2910 Planning Time: 2.821ms903 Buffers: shared hit=706 read=6 dirtied=2 904 Planning Time: 5.877 ms 911 905 JIT: 912 906 Functions: 84 913 907 Options: Inlining false, Optimization false, Expressions true, Deforming true 914 Timing: Generation 4.125 ms (Deform 1.641 ms), Inlining 0.000 ms, Optimization 1.547 ms, Emission 49.279 ms, Total 54.951 ms 915 Execution Time: 11502.974 ms 916 917 }}} 918 919 '''Execution time:''' 11502.974 ms 920 921 '''Conclusion:''' The execution time heavily degraded from ~5 seconds to ~11.5 seconds. This represents the "Nested Loop / Index Scan Trap". Creating foreign key indexes actually decreased the query performance. 922 923 Because Sequential Scans and Hash Joins are better at processing millions of rows in large analytical reporting, we '''drop''' these indexes: 908 Timing: Generation 5.747 ms (Deform 2.377 ms), Inlining 0.000 ms, Optimization 2.047 ms, Emission 53.180 ms, Total 60.973 ms 909 Execution Time: 11158.152 ms 910 }}} 911 912 '''Execution time:''' 11158.152 ms 913 914 '''Conclusion:''' The execution time increased from 5.3 seconds to 11.1 seconds. Creating foreign key indexes actually decreased the query performance. 915 916 Because Sequential Scans and Hash Joins are better for processing millions of rows in large analytical reporting workloads, we '''drop''' these trap indexes to return to the previous performance state: 924 917 {{{ 925 918 #!sql 926 919 DROP INDEX project.idx_booking_services_booking_id; 920 DROP INDEX project.idx_payments_booking_id; 927 921 DROP INDEX project.idx_bookings_date_sitter; 928 }}} 922 DROP INDEX project.idx_bookings_date_owner; 923 }}} 924 929 925 930 926
