Postgresql 博士后成绩不佳联盟所有+;内连接
在以下场景中,Postgres查询的执行速度非常慢。有人能帮我找出问题或这种行为的原因吗 我有3个不同的平台,每个平台使用两个相同结构的表:Postgresql 博士后成绩不佳联盟所有+;内连接,postgresql,performance,indexing,Postgresql,Performance,Indexing,在以下场景中,Postgres查询的执行速度非常慢。有人能帮我找出问题或这种行为的原因吗 我有3个不同的平台,每个平台使用两个相同结构的表: 订单(每个平台的所有订单信息) 订单\产品(每个订单中购买的所有产品=每个平台的客户篮子) 还有一个全局产品表,其中包含所有平台的产品信息: 产品 创建表格产品( 产品标识字符变化(64), 产品名称字符变化(512) 我还使用UNION all将所有平台上的所有order和order_产品表组合在一起 CREATE VIEW order_product_
CREATE VIEW order_product_all_platforms AS
SELECT "platform", "order_id", "product_id"
FROM "public"."order_product_1"
UNION ALL
SELECT "platform", "order_id", "product_id"
FROM "public"."order_product_2"
UNION ALL
SELECT "platform", "order_id", "product_id"
FROM "public"."order_product_3" ;
当我想将这三个表合并到一个查询中时,问题就出现了,如下所示:
SELECT "t1.platform", "t1.order_id", t2.product_id, t3.product_name
FROM order_all_platforms t1
INNER JOIN order_product_all_platforms t2 ON t1.platform = t2.platform and t1.order_id = t2.order_id
INNER JOIN product t3 ON t2.product_id = t3.product_id
WHERE purchase_date >= '2018-01-01'
我认为查询计划中有问题的部分如下所示。Postgres正在对product_id执行顺序扫描,尽管product_id上存在索引:
-> Hash Join (cost=23068.58..1963856.51 rows=37164213 width=67)"
Hash Cond: ((t2.product_id)::text = (t3.product_id)::text)"
-> Append (cost=0.00..1104593.13 rows=37164213 width=29)"
-> Seq Scan on order_product_table1 (cost=0.00..1066147.91 rows=35935991 width=29)"
-> Seq Scan on order_product_table2 (cost=0.00..35008.37 rows=1126337 width=32)"
-> Seq Scan on order_product_table3 (cost=0.00..3436.85 rows=101885 width=30)"
-> Hash (cost=16209.37..16209.37 rows=548737 width=47)"
-> Seq Scan on product t3 (cost=0.00..16209.37 rows=548737 width=47)"
我能做些什么来加快速度
这些是索引。第一个索引也存在于订单表中,第二个索引也存在于产品表中:
CREATE INDEX order_product_table1_index1
ON order_product_table1
USING btree
(platform_name COLLATE pg_catalog."default", order_id);
CREATE INDEX mat_ order_product_table1_index2
ON order_product_table1
USING btree
(product_id COLLATE pg_catalog."default");
如果只使用一个表而不使用视图,该索引是否有效?您是否尝试将它们改为分区?据我所知,Postgres正在执行哈希连接,并且在连接全局乘积表时根本不使用索引。但如何提高这种查询的性能呢?
CREATE INDEX order_product_table1_index1
ON order_product_table1
USING btree
(platform_name COLLATE pg_catalog."default", order_id);
CREATE INDEX mat_ order_product_table1_index2
ON order_product_table1
USING btree
(product_id COLLATE pg_catalog."default");