Changes between Version 4 and Version 5 of OtherTopics


Ignore:
Timestamp:
09/11/26 10:17:03 (7 hours ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OtherTopics

    v4 v5  
    6969
    7070}}}
     71
     72== Scenario 2 - Slow Moving Products
     73
     74==== Without indexes
     75
     76The query was tested on approximately 25,017 orders and 75,021 order-product records. Before indexing, PostgreSQL used a sequential scan on {{{orders}}} to find orders from the last 6 months with a completed status.
     77
     78{{{
     79Seq Scan on orders
     80rows=2549
     81Rows Removed by Filter: 22468
     82}}}
     83
     84The existing primary-key index on {{{order_products}}} was already used:
     85
     86{{{
     87Index Only Scan using order_products_pk on order_products
     88}}}
     89
     90The query was executed 10 times and the average execution time without indexes was: **12.125 ms**
     91
     92==== Indexes
     93
     94{{{
     95CREATE INDEX idx_orders_status_purchase_date
     96ON project.orders (status, purchase_date, order_id);
     97
     98ANALYZE project.orders;
     99}}}
     100
     101The index targets the {{{status}}} and {{{purchase_date}}} filters and also includes {{{order_id}}} for the join with {{{order_products}}}.
     102
     103==== With indexes
     104
     105After indexing, PostgreSQL used:
     106
     107{{{
     108Index Only Scan using idx_orders_status_purchase_date on orders
     109Heap Fetches: 0
     110}}}
     111
     112This replaced the sequential scan on {{{orders}}} and reduced the number of pages that had to be read.
     113
     114The existing {{{order_products_pk}}} index continued to be used for the join with {{{order_products}}}.
     115
     116The query was again executed 10 times and the average execution time with indexes was: **8.894 ms**
     117
     118==== Performance comparison and conclusion
     119
     120{{{
     121Without indexes: 12.125 ms
     122With indexes: 8.894 ms
     123Improvement: 26.65%
     124}}}
     125
     126{{{idx_orders_status_purchase_date}}} was successfully used as an Index Only Scan and improved the filtering of orders by status and purchase date.
     127The query improved by approximately 26.65%, while the existing {{{order_products_pk}}} index continued to support the join efficiently.
    71128
    72129== Security