I defined the join order with Leading(ba)
but PostgreSQL, or compatible database like YugabyteDB, still join from a
to 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)
There are two syntaxes for the Leading hint. One just lists the table in the order they should be considered by the query planner, like Leading ( ba )
, leaving the choice of swapping the inner and outer table to the optimizer. The other defines each join pair, with extra parenthesis, as Leading( (ba) )
where left/right item is (outer inner). With a third table it will be either Leading( ( (ba) c ) )
or Leading( ( c (ba) ) )
Generally, fixing an execution plan requires the second syntax, plus a join method hint for each join, and an access method hint for each table.
Examples here: https://dev.to/yugabyte/predictable-plans-with-pghintplan-full-hinting-1do3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.