【问题标题】:Postgres seems to be using the wrong index?Postgres 似乎使用了错误的索引?
【发布时间】:2016-09-19 01:05:39
【问题描述】:

我正在使用 Postgres 9.4。这是我的桌子:

                                       Table "public.frontend_prescription"
      Column       |          Type           |                             Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
 id                | integer                 | not null default nextval('frontend_prescription_id_seq'::regclass)
 presentation_code | character varying(15)   | not null
 total_items       | integer                 | not null
 processing_date   | date                    | not null
 practice_id       | character varying(6)    | not null
Indexes:
    "frontend_prescription_pkey" PRIMARY KEY, btree (id)
    "frontend_prescription_6ea07fe3" btree (practice_id)
    "frontend_prescription_by_practice" btree (presentation_code, practice_id)
    "frontend_prescription_by_practice_and_code" btree (practice_id, presentation_code varchar_pattern_ops)
    "frontend_prescription_idx_date_and_code" btree (processing_date, presentation_code)

这是我的查询:

EXPLAIN (analyse, verbose) 
SELECT SUM(total_items) AS items, SUM(total_items) AS numerator 
FROM frontend_prescription 
WHERE ((presentation_code LIKE '0601012Z0%') OR (presentation_code LIKE '0601012X0%') OR (presentation_code LIKE '0601012V0%'))  
AND (practice_id='A81001')  
AND (processing_date='2016-01-01')

这是输出:

 Aggregate  (cost=12.26..12.27 rows=1 width=4) (actual time=16898.277..16898.277 rows=1 loops=1)
   Output: sum(total_items), sum(total_items)
   ->  Index Scan using frontend_prescription_idx_date_and_code on public.frontend_prescription  (cost=0.57..12.26 rows=1 width=4) (actual time=9220.091..16898.251 rows=6 loops=1)
         Output: id, presentation_code, presentation_name, total_items, net_cost, actual_cost, quantity, processing_date, price_per_unit, chemical_id, pct_id, practice_id, sha_id
         Index Cond: (frontend_prescription.processing_date = '2016-01-01'::date)
         Filter: (((frontend_prescription.practice_id)::text = 'A81001'::text) AND (((frontend_prescription.presentation_code)::text ~~ '0601012Z0%'::text) OR ((frontend_prescription.presentation_code)::text ~~ '0601012X0%'::text) OR ((frontend_prescription.presentation_code)::text ~~ '0601012V0%'::text)))
         Rows Removed by Filter: 10036400
 Planning time: 6.054 ms
 Execution time: 16898.366 ms

这里是link to the explain

谁能建议我如何加快这些查询的速度?我真的不明白为什么 Postgres 使用 frontend_prescription_idx_date_and_code 索引,而 frontend_prescription_by_practice_and_code 索引有一个 varchar 选项,这应该会使事情变得更快。

如果我制作一个三栏索引可能会有所帮助?

【问题讨论】:

  • 您希望在所有三列(日期、练习 ID、代码)上都有一个索引。
  • 您也可以通过在代码上添加一个范围来加快速度:AND presentation_code between '0601012' and '0601013'

标签: sql postgresql


【解决方案1】:

对于这个查询:

SELECT SUM(total_items) AS items, SUM(total_items) AS numerator 
FROM frontend_prescription 
WHERE ((presentation_code LIKE '0601012Z0%') OR (presentation_code LIKE '0601012X0%') OR (presentation_code LIKE '0601012V0%')
       ) AND
       (practice_id = 'A81001')  AND
       (processing_date = '2016-01-01');

最佳索引是frontend_prescription(practice_id, processing_date, presentation_code, total_items) 上的复合索引。最后一列不是绝对必要的。它使索引成为查询的覆盖索引;换句话说,查询所需的所有信息都在索引中,因此不需要数据页。

前两列可以按任意顺序排列,因为where 子句使用相等。

【讨论】:

  • 非常感谢!鉴于我有一个LIKE 查询,使用varchar_ops 是否有意义,所以使用create index myindex on frontend_prescription(practice_id, processing_date, presentation_code varchar_pattern_ops, total_items)
猜你喜欢
  • 2014-03-26
  • 2015-01-18
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多