我使用 Leading(b a)
定义了连接顺序,但 PostgreSQL 或 YugabyteDB 等兼容数据库仍然从 a
连接到 b
:
yugabyte=# explain (costs off, timing off)
/*+ Leading( b a ) */
select * from table_a a join table_b b using(id);
QUERY PLAN
--------------------------------------------------
Nested Loop
-> Seq Scan on table_a a
-> Index Scan using table_b_pkey on table_b b
Index Cond: (id = a.id)
最佳答案
前导提示有两种语法。只需按照查询规划器应考虑的顺序列出表,例如 Leading ( b a )
,将交换内表和外表的选择权留给优化器。另一个定义每个连接对,并带有额外的括号,如 Leading( (b a) )
,其中左/右项是(外部 内部)。对于第三个表,它将是 Leading( ( (b a) c ) )
或 Leading( ( c (b a) ) )
通常,修复执行计划需要第二种语法,加上每个连接的连接方法提示以及每个表的访问方法提示。
示例:https://dev.to/yugabyte/predictable-plans-with-pghintplan-full-hinting-1do3
关于sql - 为什么我的 pg_hint_plan 前导提示没有被使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72557184/