PostgreSQL 改进查询
我有一个查询如下:
SELECT MAX(c."Sequence")
FROM "Cips" AS c
WHERE c."StoreNumber" IN (1, 2)
AND c."DataProvider" in ('MCIP' , 'SAM')
我还运行 explain analysis
并收到以下答案:
"Aggregate (cost=43628.91..43628.92 rows=1 width=8) (actual time=81.290..81.292 rows=1 loops=1)"
" -> Append (cost=0.43..43498.29 rows=52248 width=8) (actual time=0.090..75.045 rows=61163 loops=1)"
" -> Index Scan using ""a_StoreNumber_DataProvider_idx"" on a c (cost=0.43..43237.05 rows=52248 width=8) (actual time=0.089..67.541 rows=61163 loops=1)"
" Index Cond: ((""StoreNumber"" = ANY ('{-1,1}'::integer[])) AND (""DataProvider"" = ANY ('{MCIP,SAM}'::text[])))"
"Planning Time: 0.677 ms"
"Execution Time: 81.366 ms"
我在此表上仅定义了一个索引:
CREATE INDEX "idx_Cip_StoreNumber_Sequence_DataProvider"
ON public."Cips" USING btree
("StoreNumber" ASC NULLS LAST, "Sequence" ASC NULLS LAST, "DataProvider" COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
我的表行数约为 140 万行。我还尝试对表进行分区,但性能提高了 10%。我的问题是我需要将性能提高 90%,但我确实陷入了困境。
我想知道是否可以提高此查询的性能,或者我应该开始寻找其他方向,例如修改该字段周围的架构以及如何获得最大值?
I have a query as follows:
SELECT MAX(c."Sequence")
FROM "Cips" AS c
WHERE c."StoreNumber" IN (1, 2)
AND c."DataProvider" in ('MCIP' , 'SAM')
I also run the explain analyse
and I receive the following answer:
"Aggregate (cost=43628.91..43628.92 rows=1 width=8) (actual time=81.290..81.292 rows=1 loops=1)"
" -> Append (cost=0.43..43498.29 rows=52248 width=8) (actual time=0.090..75.045 rows=61163 loops=1)"
" -> Index Scan using ""a_StoreNumber_DataProvider_idx"" on a c (cost=0.43..43237.05 rows=52248 width=8) (actual time=0.089..67.541 rows=61163 loops=1)"
" Index Cond: ((""StoreNumber"" = ANY ('{-1,1}'::integer[])) AND (""DataProvider"" = ANY ('{MCIP,SAM}'::text[])))"
"Planning Time: 0.677 ms"
"Execution Time: 81.366 ms"
I have only one index defined on this table:
CREATE INDEX "idx_Cip_StoreNumber_Sequence_DataProvider"
ON public."Cips" USING btree
("StoreNumber" ASC NULLS LAST, "Sequence" ASC NULLS LAST, "DataProvider" COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
My table row count is somewhere around 1.4 mil rows. I also tried to partition the table, but with 10% better performance. My problem is that I need to improve the performance by 90% and I really am stuck.
I was wondering if I can improve the performance of this query or should I start to look in some other direction, for example to modify the architecture around this field and how to we get the max value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试不同的索引,其中的列按照您在该查询中需要的方式排序:
序列现在是索引中的最后一列,并且也按降序排序,但最后是 NULL。
这会改善查询计划吗?请使用 EXPLAIN(ANALYZE, VERBOSE, BUFFERS) 来获取完整的计划。
Try a different index, where you have the columns ordered in the way you need them for this query:
Sequence is now the last column in the index, and also sorted descending but NULLS last.
Does this improve the query plan? Please use EXPLAIN(ANALYZE, VERBOSE, BUFFERS) to get the complete plan.