Index could be added on product_price_history

Bug #1301847 reported by Guewen Baconnier @ Camptocamp
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Financial controlling and costing
Fix Released
Undecided
Guewen Baconnier @ Camptocamp

Bug Description

Going through a performance / index analysis, I used this query (from http://stackoverflow.com/questions/3318727/postgresql-index-usage-analysis):

SELECT relname, seq_scan-idx_scan AS too_much_seq, case when seq_scan-idx_scan>0 THEN 'Missing Index?' ELSE 'OK' END, pg_relation_size(relname::regclass) AS rel_size, seq_scan, idx_scan
 FROM pg_stat_all_tables
 WHERE schemaname='public' AND pg_relation_size(relname::regclass)>80000 ORDER BY too_much_seq DESC;

And it appeared that product_price_history is always accessed using a seq scan.

I also noticed in the logs that the SELECT queries on this table is always in this form:

SELECT DISTINCT ON (product_id, name) datetime, product_id, name, amount FROM product_price_history WHERE product_id IN (5521) AND company_id =
 3 AND name IN ('standard_price') AND datetime <= '2014-05-03 09:34:30' ORDER BY product_id, name, datetime DESC, id DESC;

So, it could be nice to add an index on the 4 fields.

Explain plan before:

                                                                                    QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Unique (cost=329.59..329.60 rows=1 width=32) (actual time=4.858..4.861 rows=1 loops=1)
   -> Sort (cost=329.59..329.60 rows=1 width=32) (actual time=4.854..4.855 rows=1 loops=1)
         Sort Key: datetime, id
         Sort Method: quicksort Memory: 25kB
         -> Seq Scan on product_price_history (cost=0.00..329.58 rows=1 width=32) (actual time=0.020..4.779 rows=1 loops=1)
               Filter: ((datetime <= '2014-05-03 09:34:30'::timestamp without time zone) AND (product_id = 5521) AND ((name)::text = 'standard_price'::text) AND (company_id = 3))
 Total runtime: 4.905 ms

Explain plan after index
create index product_price_history_all_index on product_price_history (product_id, company_id, name, datetime);

                                                                                      QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Unique (cost=8.29..8.30 rows=1 width=32) (actual time=0.135..0.136 rows=1 loops=1)
   -> Sort (cost=8.29..8.29 rows=1 width=32) (actual time=0.133..0.134 rows=1 loops=1)
         Sort Key: datetime, id
         Sort Method: quicksort Memory: 25kB
         -> Index Scan using product_price_history_all_index on product_price_history (cost=0.00..8.28 rows=1 width=32) (actual time=0.117..0.119 rows=1 loops=1)
               Index Cond: ((product_id = 5521) AND (company_id = 3) AND ((name)::text = 'standard_price'::text) AND (datetime <= '2014-05-03 09:34:30'::timestamp without time zone))
 Total runtime: 0.190 ms

Related branches

Changed in margin-analysis:
assignee: nobody → Guewen Baconnier @ Camptocamp (gbaconnier-c2c)
status: New → In Progress
description: updated
Changed in margin-analysis:
status: In Progress → Fix Committed
Changed in margin-analysis:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.