| 270 | | SELECT c.name, SUM(si.quantity * si.unit_price_at_sale) |
| 271 | | FROM stock_management.sale_item si |
| 272 | | JOIN stock_management.product p ON si.product_id = p.product_id |
| 273 | | JOIN stock_management.category c ON p.category_id = c.category_id |
| 274 | | GROUP BY c.category_id, c.name; |
| 275 | | }}} |
| 276 | | |
| 277 | | {{{ |
| 278 | | "Finalize GroupAggregate (cost=7589.33..7600.28 rows=50 width=47) (actual time=624.660..643.857 rows=50.00 loops=1)" |
| 279 | | " Group Key: c.category_id" |
| 280 | | " Buffers: shared hit=2054" |
| 281 | | " -> Gather Merge (cost=7589.33..7599.02 rows=85 width=47) (actual time=624.642..643.771 rows=100.00 loops=1)" |
| 282 | | " Workers Planned: 1" |
| 283 | | " Workers Launched: 1" |
| 284 | | " Buffers: shared hit=2054" |
| 285 | | " -> Sort (cost=6589.32..6589.45 rows=50 width=47) (actual time=507.965..507.972 rows=50.00 loops=2)" |
| 286 | | " Sort Key: c.category_id" |
| 287 | | " Sort Method: quicksort Memory: 30kB" |
| 288 | | " Buffers: shared hit=2054" |
| 289 | | " Worker 0: Sort Method: quicksort Memory: 30kB" |
| 290 | | " -> Partial HashAggregate (cost=6587.29..6587.91 rows=50 width=47) (actual time=507.781..507.904 rows=50.00 loops=2)" |
| 291 | | " Group Key: c.category_id" |
| 292 | | " Batches: 1 Memory Usage: 40kB" |
| 293 | | " Buffers: shared hit=2047" |
| 294 | | " Worker 0: Batches: 1 Memory Usage: 40kB" |
| 295 | | " -> Hash Join (cost=181.62..4822.93 rows=176436 width=25) (actual time=5.394..368.931 rows=149971.00 loops=2)" |
| 296 | | " Hash Cond: (p.category_id = c.category_id)" |
| 297 | | " Buffers: shared hit=2047" |
| 298 | | " -> Hash Join (cost=179.50..4318.40 rows=176436 width=14) (actual time=4.975..314.892 rows=149971.00 loops=2)" |
| 299 | | " Hash Cond: (si.product_id = p.product_id)" |
| 300 | | " Buffers: shared hit=2045" |
| 301 | | " -> Parallel Seq Scan on sale_item si (cost=0.00..3675.36 rows=176436 width=14) (actual time=0.704..156.201 rows=149971.00 loops=2)" |
| 302 | | " Buffers: shared hit=1911" |
| 303 | | " -> Hash (cost=117.00..117.00 rows=5000 width=8) (actual time=4.200..4.200 rows=5000.00 loops=2)" |
| 304 | | " Buckets: 8192 Batches: 1 Memory Usage: 260kB" |
| 305 | | " Buffers: shared hit=134" |
| 306 | | " -> Seq Scan on product p (cost=0.00..117.00 rows=5000 width=8) (actual time=0.610..3.091 rows=5000.00 loops=2)" |
| 307 | | " Buffers: shared hit=134" |
| 308 | | " -> Hash (cost=1.50..1.50 rows=50 width=15) (actual time=0.403..0.404 rows=50.00 loops=2)" |
| 309 | | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| 310 | | " Buffers: shared hit=2" |
| 311 | | " -> Seq Scan on category c (cost=0.00..1.50 rows=50 width=15) (actual time=0.371..0.378 rows=50.00 loops=2)" |
| 312 | | " Buffers: shared hit=2" |
| | 277 | SELECT |
| | 278 | TO_CHAR(date_trunc('month', s.date_time), 'YYYY-MM') AS sales_month, |
| | 279 | w.name AS warehouse_name, |
| | 280 | c.name AS category_name, |
| | 281 | sup.name AS supplier_name, |
| | 282 | COUNT(DISTINCT s.sale_id) AS total_order_count, |
| | 283 | SUM(si.quantity) AS total_units_sold, |
| | 284 | SUM(si.quantity * si.unit_price_at_sale) AS total_gross_revenue |
| | 285 | FROM sale s |
| | 286 | JOIN sale_item si ON s.sale_id = si.sale_id |
| | 287 | JOIN product p ON si.product_id = p.product_id |
| | 288 | LEFT JOIN category c ON p.category_id = c.category_id |
| | 289 | LEFT JOIN supplier sup ON p.supplier_id = sup.supplier_id |
| | 290 | JOIN warehouse w ON s.warehouse_id = w.warehouse_id |
| | 291 | WHERE s.date_time >= date_trunc('month', CURRENT_DATE) - INTERVAL '11 months' |
| | 292 | GROUP BY sales_month, w.name, c.name, sup.name |
| | 293 | ORDER BY sales_month DESC, total_gross_revenue DESC; |
| | 294 | }}} |
| | 295 | |
| | 296 | {{{ |
| | 297 | "Incremental Sort (cost=18709.65..51808.79 rows=139071 width=321) (actual time=2444.202..7628.612 rows=54435.00 loops=1)" |
| | 298 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC, (sum(((si.quantity)::numeric * si.unit_price_at_sale))) DESC" |
| | 299 | " Presorted Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text))" |
| | 300 | " Full-sort Groups: 12 Sort Method: quicksort Average Memory: 30kB Peak Memory: 30kB" |
| | 301 | " Pre-sorted Groups: 12 Sort Method: quicksort Average Memory: 568kB Peak Memory: 615kB" |
| | 302 | " Buffers: shared hit=3632, temp read=1176 written=1179" |
| | 303 | " -> GroupAggregate (cost=18708.99..48041.17 rows=139071 width=321) (actual time=2116.416..7458.422 rows=54435.00 loops=1)" |
| | 304 | " Group Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)), w.name, c.name, sup.name" |
| | 305 | " Buffers: shared hit=3632, temp read=1176 written=1179" |
| | 306 | " -> Incremental Sort (cost=18708.99..42478.33 rows=139071 width=287) (actual time=2116.373..6967.664 rows=137806.00 loops=1)" |
| | 307 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC, w.name, c.name, sup.name, s.sale_id" |
| | 308 | " Presorted Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text))" |
| | 309 | " Full-sort Groups: 12 Sort Method: quicksort Average Memory: 30kB Peak Memory: 30kB" |
| | 310 | " Pre-sorted Groups: 12 Sort Method: quicksort Average Memory: 1262kB Peak Memory: 1387kB" |
| | 311 | " Buffers: shared hit=3632, temp read=1176 written=1179" |
| | 312 | " -> Nested Loop (cost=18708.54..38710.70 rows=139071 width=287) (actual time=2093.902..2972.887 rows=137806.00 loops=1)" |
| | 313 | " Buffers: shared hit=3632, temp read=1176 written=1179" |
| | 314 | " -> Gather Merge (cost=18708.38..34558.39 rows=139071 width=49) (actual time=2093.791..2485.802 rows=137806.00 loops=1)" |
| | 315 | " Workers Planned: 1" |
| | 316 | " Workers Launched: 1" |
| | 317 | " Buffers: shared hit=3626, temp read=1176 written=1179" |
| | 318 | " -> Sort (cost=17708.37..17912.89 rows=81806 width=49) (actual time=1937.274..1979.269 rows=68903.00 loops=2)" |
| | 319 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC" |
| | 320 | " Sort Method: external merge Disk: 3672kB" |
| | 321 | " Buffers: shared hit=3626, temp read=1176 written=1179" |
| | 322 | " Worker 0: Sort Method: external merge Disk: 5736kB" |
| | 323 | " -> Hash Left Join (cost=3417.20..8236.54 rows=81806 width=49) (actual time=374.832..1517.447 rows=68903.00 loops=2)" |
| | 324 | " Hash Cond: (p.supplier_id = sup.supplier_id)" |
| | 325 | " Buffers: shared hit=3618" |
| | 326 | " -> Hash Left Join (cost=3415.08..8001.47 rows=81806 width=41) (actual time=372.663..1142.797 rows=68903.00 loops=2)" |
| | 327 | " Hash Cond: (p.category_id = c.category_id)" |
| | 328 | " Buffers: shared hit=3616" |
| | 329 | " -> Hash Join (cost=3412.95..7766.40 rows=81806 width=34) (actual time=368.131..1095.264 rows=68903.00 loops=2)" |
| | 330 | " Hash Cond: (si.product_id = p.product_id)" |
| | 331 | " Buffers: shared hit=3614" |
| | 332 | " -> Parallel Hash Join (cost=3233.45..7371.99 rows=81806 width=30) (actual time=364.282..986.295 rows=68903.00 loops=2)" |
| | 333 | " Hash Cond: (si.sale_id = s.sale_id)" |
| | 334 | " Buffers: shared hit=3480" |
| | 335 | " -> Parallel Seq Scan on sale_item si (cost=0.00..3675.36 rows=176436 width=18) (actual time=0.035..126.327 rows=149971.00 loops=2)" |
| | 336 | " Buffers: shared hit=1911" |
| | 337 | " -> Parallel Hash (cost=2892.53..2892.53 rows=27274 width=16) (actual time=358.216..358.220 rows=22972.00 loops=2)" |
| | 338 | " Buckets: 65536 Batches: 1 Memory Usage: 2688kB" |
| | 339 | " Buffers: shared hit=1569" |
| | 340 | " -> Parallel Seq Scan on sale s (cost=0.00..2892.53 rows=27274 width=16) (actual time=1.149..298.091 rows=22972.00 loops=2)" |
| | 341 | " Filter: (date_time >= (date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone) - '11 mons'::interval))" |
| | 342 | " Rows Removed by Filter: 27028" |
| | 343 | " Buffers: shared hit=1569" |
| | 344 | " -> Hash (cost=117.00..117.00 rows=5000 width=12) (actual time=3.779..3.780 rows=5000.00 loops=2)" |
| | 345 | " Buckets: 8192 Batches: 1 Memory Usage: 279kB" |
| | 346 | " Buffers: shared hit=134" |
| | 347 | " -> Seq Scan on product p (cost=0.00..117.00 rows=5000 width=12) (actual time=0.463..1.723 rows=5000.00 loops=2)" |
| | 348 | " Buffers: shared hit=134" |
| | 349 | " -> Hash (cost=1.50..1.50 rows=50 width=15) (actual time=4.508..4.509 rows=50.00 loops=2)" |
| | 350 | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| | 351 | " Buffers: shared hit=2" |
| | 352 | " -> Seq Scan on category c (cost=0.00..1.50 rows=50 width=15) (actual time=4.441..4.453 rows=50.00 loops=2)" |
| | 353 | " Buffers: shared hit=2" |
| | 354 | " -> Hash (cost=1.50..1.50 rows=50 width=16) (actual time=1.980..1.982 rows=50.00 loops=2)" |
| | 355 | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| | 356 | " Buffers: shared hit=2" |
| | 357 | " -> Seq Scan on supplier sup (cost=0.00..1.50 rows=50 width=16) (actual time=1.936..1.946 rows=50.00 loops=2)" |
| | 358 | " Buffers: shared hit=2" |
| | 359 | " -> Memoize (cost=0.15..0.17 rows=1 width=222) (actual time=0.001..0.001 rows=1.00 loops=137806)" |
| | 360 | " Cache Key: s.warehouse_id" |
| | 361 | " Cache Mode: logical" |
| | 362 | " Hits: 137803 Misses: 3 Evictions: 0 Overflows: 0 Memory Usage: 1kB" |
| | 363 | " Buffers: shared hit=6" |
| | 364 | " -> Index Scan using warehouse_pkey on warehouse w (cost=0.14..0.16 rows=1 width=222) (actual time=0.026..0.026 rows=1.00 loops=3)" |
| | 365 | " Index Cond: (warehouse_id = s.warehouse_id)" |
| | 366 | " Index Searches: 3" |
| | 367 | " Buffers: shared hit=6" |
| 331 | | {{{CREATE INDEX idx_product_category_id ON stock_management.product(category_id);}}} |
| 332 | | |
| 333 | | {{{ |
| 334 | | "Finalize GroupAggregate (cost=7589.33..7600.28 rows=50 width=47) (actual time=969.751..977.654 rows=50.00 loops=1)" |
| 335 | | " Group Key: c.category_id" |
| 336 | | " Buffers: shared hit=2054" |
| 337 | | " -> Gather Merge (cost=7589.33..7599.02 rows=85 width=47) (actual time=969.729..977.531 rows=100.00 loops=1)" |
| 338 | | " Workers Planned: 1" |
| 339 | | " Workers Launched: 1" |
| 340 | | " Buffers: shared hit=2054" |
| 341 | | " -> Sort (cost=6589.32..6589.45 rows=50 width=47) (actual time=792.046..792.057 rows=50.00 loops=2)" |
| 342 | | " Sort Key: c.category_id" |
| 343 | | " Sort Method: quicksort Memory: 30kB" |
| 344 | | " Buffers: shared hit=2054" |
| 345 | | " Worker 0: Sort Method: quicksort Memory: 30kB" |
| 346 | | " -> Partial HashAggregate (cost=6587.29..6587.91 rows=50 width=47) (actual time=790.873..790.904 rows=50.00 loops=2)" |
| 347 | | " Group Key: c.category_id" |
| 348 | | " Batches: 1 Memory Usage: 40kB" |
| 349 | | " Buffers: shared hit=2047" |
| 350 | | " Worker 0: Batches: 1 Memory Usage: 40kB" |
| 351 | | " -> Hash Join (cost=181.62..4822.93 rows=176436 width=25) (actual time=3.506..493.595 rows=149971.00 loops=2)" |
| 352 | | " Hash Cond: (p.category_id = c.category_id)" |
| 353 | | " Buffers: shared hit=2047" |
| 354 | | " -> Hash Join (cost=179.50..4318.40 rows=176436 width=14) (actual time=3.070..303.720 rows=149971.00 loops=2)" |
| 355 | | " Hash Cond: (si.product_id = p.product_id)" |
| 356 | | " Buffers: shared hit=2045" |
| 357 | | " -> Parallel Seq Scan on sale_item si (cost=0.00..3675.36 rows=176436 width=14) (actual time=0.055..93.742 rows=149971.00 loops=2)" |
| 358 | | " Buffers: shared hit=1911" |
| 359 | | " -> Hash (cost=117.00..117.00 rows=5000 width=8) (actual time=2.952..2.954 rows=5000.00 loops=2)" |
| 360 | | " Buckets: 8192 Batches: 1 Memory Usage: 260kB" |
| 361 | | " Buffers: shared hit=134" |
| 362 | | " -> Seq Scan on product p (cost=0.00..117.00 rows=5000 width=8) (actual time=0.279..1.620 rows=5000.00 loops=2)" |
| 363 | | " Buffers: shared hit=134" |
| 364 | | " -> Hash (cost=1.50..1.50 rows=50 width=15) (actual time=0.418..0.419 rows=50.00 loops=2)" |
| 365 | | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| 366 | | " Buffers: shared hit=2" |
| 367 | | " -> Seq Scan on category c (cost=0.00..1.50 rows=50 width=15) (actual time=0.377..0.387 rows=50.00 loops=2)" |
| 368 | | " Buffers: shared hit=2" |
| | 386 | {{{ |
| | 387 | CREATE INDEX idx_sale_warehouse_id ON stock_management.sale(warehouse_id); |
| | 388 | CREATE INDEX idx_product_supplier_id ON stock_management.product(supplier_id); |
| | 389 | }}} |
| | 390 | |
| | 391 | {{{ |
| | 392 | "Incremental Sort (cost=18709.65..51808.79 rows=139071 width=321) (actual time=1565.854..5424.998 rows=54435.00 loops=1)" |
| | 393 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC, (sum(((si.quantity)::numeric * si.unit_price_at_sale))) DESC" |
| | 394 | " Presorted Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text))" |
| | 395 | " Full-sort Groups: 12 Sort Method: quicksort Average Memory: 30kB Peak Memory: 30kB" |
| | 396 | " Pre-sorted Groups: 12 Sort Method: quicksort Average Memory: 568kB Peak Memory: 615kB" |
| | 397 | " Buffers: shared hit=3632, temp read=1177 written=1180" |
| | 398 | " -> GroupAggregate (cost=18708.99..48041.17 rows=139071 width=321) (actual time=1329.729..5304.172 rows=54435.00 loops=1)" |
| | 399 | " Group Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)), w.name, c.name, sup.name" |
| | 400 | " Buffers: shared hit=3632, temp read=1177 written=1180" |
| | 401 | " -> Incremental Sort (cost=18708.99..42478.33 rows=139071 width=287) (actual time=1328.886..4726.267 rows=137806.00 loops=1)" |
| | 402 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC, w.name, c.name, sup.name, s.sale_id" |
| | 403 | " Presorted Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text))" |
| | 404 | " Full-sort Groups: 12 Sort Method: quicksort Average Memory: 30kB Peak Memory: 30kB" |
| | 405 | " Pre-sorted Groups: 12 Sort Method: quicksort Average Memory: 1262kB Peak Memory: 1387kB" |
| | 406 | " Buffers: shared hit=3632, temp read=1177 written=1180" |
| | 407 | " -> Nested Loop (cost=18708.54..38710.70 rows=139071 width=287) (actual time=1314.716..2114.599 rows=137806.00 loops=1)" |
| | 408 | " Buffers: shared hit=3632, temp read=1177 written=1180" |
| | 409 | " -> Gather Merge (cost=18708.38..34558.39 rows=139071 width=49) (actual time=1314.525..1652.546 rows=137806.00 loops=1)" |
| | 410 | " Workers Planned: 1" |
| | 411 | " Workers Launched: 1" |
| | 412 | " Buffers: shared hit=3626, temp read=1177 written=1180" |
| | 413 | " -> Sort (cost=17708.37..17912.89 rows=81806 width=49) (actual time=1141.709..1184.523 rows=68903.00 loops=2)" |
| | 414 | " Sort Key: (to_char(date_trunc('month'::text, s.date_time), 'YYYY-MM'::text)) DESC" |
| | 415 | " Sort Method: external merge Disk: 3184kB" |
| | 416 | " Buffers: shared hit=3626, temp read=1177 written=1180" |
| | 417 | " Worker 0: Sort Method: external merge Disk: 6232kB" |
| | 418 | " -> Hash Left Join (cost=3417.20..8236.54 rows=81806 width=49) (actual time=272.961..914.606 rows=68903.00 loops=2)" |
| | 419 | " Hash Cond: (p.supplier_id = sup.supplier_id)" |
| | 420 | " Buffers: shared hit=3618" |
| | 421 | " -> Hash Left Join (cost=3415.08..8001.47 rows=81806 width=41) (actual time=269.015..793.730 rows=68903.00 loops=2)" |
| | 422 | " Hash Cond: (p.category_id = c.category_id)" |
| | 423 | " Buffers: shared hit=3616" |
| | 424 | " -> Hash Join (cost=3412.95..7766.40 rows=81806 width=34) (actual time=265.863..761.031 rows=68903.00 loops=2)" |
| | 425 | " Hash Cond: (si.product_id = p.product_id)" |
| | 426 | " Buffers: shared hit=3614" |
| | 427 | " -> Parallel Hash Join (cost=3233.45..7371.99 rows=81806 width=30) (actual time=242.285..649.734 rows=68903.00 loops=2)" |
| | 428 | " Hash Cond: (si.sale_id = s.sale_id)" |
| | 429 | " Buffers: shared hit=3480" |
| | 430 | " -> Parallel Seq Scan on sale_item si (cost=0.00..3675.36 rows=176436 width=18) (actual time=0.020..203.345 rows=149971.00 loops=2)" |
| | 431 | " Buffers: shared hit=1911" |
| | 432 | " -> Parallel Hash (cost=2892.53..2892.53 rows=27274 width=16) (actual time=236.811..236.814 rows=22972.00 loops=2)" |
| | 433 | " Buckets: 65536 Batches: 1 Memory Usage: 2688kB" |
| | 434 | " Buffers: shared hit=1569" |
| | 435 | " -> Parallel Seq Scan on sale s (cost=0.00..2892.53 rows=27274 width=16) (actual time=12.339..207.382 rows=22972.00 loops=2)" |
| | 436 | " Filter: (date_time >= (date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone) - '11 mons'::interval))" |
| | 437 | " Rows Removed by Filter: 27028" |
| | 438 | " Buffers: shared hit=1569" |
| | 439 | " -> Hash (cost=117.00..117.00 rows=5000 width=12) (actual time=23.432..23.434 rows=5000.00 loops=2)" |
| | 440 | " Buckets: 8192 Batches: 1 Memory Usage: 279kB" |
| | 441 | " Buffers: shared hit=134" |
| | 442 | " -> Seq Scan on product p (cost=0.00..117.00 rows=5000 width=12) (actual time=1.523..20.547 rows=5000.00 loops=2)" |
| | 443 | " Buffers: shared hit=134" |
| | 444 | " -> Hash (cost=1.50..1.50 rows=50 width=15) (actual time=3.117..3.118 rows=50.00 loops=2)" |
| | 445 | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| | 446 | " Buffers: shared hit=2" |
| | 447 | " -> Seq Scan on category c (cost=0.00..1.50 rows=50 width=15) (actual time=3.062..3.090 rows=50.00 loops=2)" |
| | 448 | " Buffers: shared hit=2" |
| | 449 | " -> Hash (cost=1.50..1.50 rows=50 width=16) (actual time=2.975..2.976 rows=50.00 loops=2)" |
| | 450 | " Buckets: 1024 Batches: 1 Memory Usage: 11kB" |
| | 451 | " Buffers: shared hit=2" |
| | 452 | " -> Seq Scan on supplier sup (cost=0.00..1.50 rows=50 width=16) (actual time=2.935..2.944 rows=50.00 loops=2)" |
| | 453 | " Buffers: shared hit=2" |
| | 454 | " -> Memoize (cost=0.15..0.17 rows=1 width=222) (actual time=0.001..0.001 rows=1.00 loops=137806)" |
| | 455 | " Cache Key: s.warehouse_id" |
| | 456 | " Cache Mode: logical" |
| | 457 | " Hits: 137803 Misses: 3 Evictions: 0 Overflows: 0 Memory Usage: 1kB" |
| | 458 | " Buffers: shared hit=6" |
| | 459 | " -> Index Scan using warehouse_pkey on warehouse w (cost=0.14..0.16 rows=1 width=222) (actual time=0.049..0.049 rows=1.00 loops=3)" |
| | 460 | " Index Cond: (warehouse_id = s.warehouse_id)" |
| | 461 | " Index Searches: 3" |
| | 462 | " Buffers: shared hit=6" |