简体   繁体   English

PostgreSQL查询的执行时间很慢

[英]Getting very slow execution time for a postgresql query

I did the explain analyze for this query, it was giving 30ms, but if the data is more I will get an execution expired; 我为这个查询做了解释分析,它给出了30ms,但是如果数据更多,我将得到一个过期的执行。 Using PostgreSQL 10 使用PostgreSQL 10

For normal execution: https://explain.depesz.com/s/gSPP 正常执行: https//explain.depesz.com/s/gSPP

For slow execution: https://explain.depesz.com/s/bQN2 对于慢速执行: https : //explain.depesz.com/s/bQN2

SELECT inventory_histories.*, order_items.order_id as order_id FROM
"inventory_histories" LEFT JOIN order_items ON (order_items.id = 
inventory_histories.reference_id AND inventory_histories.reference_type = 4) 
WHERE "inventory_histories"."inventory_id" = 1313 AND 
(inventory_histories.location_id = 15) ORDER BY inventory_histories.id DESC 
LIMIT 10 OFFSET 0;

Indexes: 索引:

"inventory_histories_pkey" PRIMARY KEY, btree (id)
"inventory_histories_created_at_index" btree (created_at)
"inventory_histories_inventory_id_index" btree (inventory_id)
"inventory_histories_location_id_index" btree (location_id)

For this query: 对于此查询:

SELECT ih.*, oi.order_id as order_id
FROM "inventory_histories" ih LEFT JOIN
     order_items oi
     ON oi.id = ih.reference_id AND
        ih.reference_type = 4 
WHERE ih."inventory_id" = 1313 AND 
      ih.location_id = 15
ORDER BY ih.id DESC 
LIMIT 10 OFFSET 0;

For this query, you want composite indexes on inventory_histories(inventory_id, location_id, id, reference_id) and on order_items(id, reference_type, order_id) . 对于此查询,您需要在inventory_histories(inventory_id, location_id, id, reference_id)order_items(id, reference_type, order_id)上的复合索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM