Join Order and Plans (CREATE PLAN)
In KDBS, the step chain decides access methods through the catalog, and the one thing the engine chooses is the table order of an inner join block. When choosing, it looks at the statement shape, the catalog (which step is a tuple-key lookup, which columns have an index or a Cabin, which literal narrows which table), and plan statistics (the approximate size of each table, and the moving average of the number of rows the join produced in previous executions of the same statement).
Key Specifications
| Item | Value |
|---|---|
| Setting | join_order = fixed (default) / planned / written. Change at runtime with SET JOIN_ORDER (admin) |
fixed | After the sizes of all tables in the join are published, captures the first chosen order for the statement shape (the statement without literals) and keeps using it. Stored in sys.plans, kept across restarts |
planned | Chosen from statistics on every execution. Statistics move once per second, in steps of about ±30% |
written | In the order written |
| Statements with subqueries | Always the written order |
| Administrator plan | CREATE PLAN … VARIANT … ORDER (…) ACCESS … BUILD …. Takes priority over every setting |
| Statistics object | Row count counted by GATHER STATISTICS ON t. Once published, it is used instead of the engine's own estimate. Pinned with LOCK STATISTICS |
The planned Line in ANALYZE
planned order=written gen=0 switch=1 est=0:1000,1:1000 capture=deferred
| Value | Meaning |
|---|---|
order=captured | Used the captured order |
order=plan plan=<name>/<variant> | Administrator plan |
order=written | Written order |
capture=first / deferred | This choice was captured / deferred because some table's size is not yet published |
est= | Estimated row count per step |
Administrator Plans
Unlike Oracle hints, you do not change the SQL; you attach a plan to the statement shape. Different variants can be chosen depending on the literal values.
CREATE PLAN p_loans FOR
SELECT u.name, l.amount FROM users AS u JOIN loans AS l ON l.user_id = u.id
WHERE u.id BETWEEN 100 AND 164
VARIANT narrow WHEN ARG(2) - ARG(1) <= 64 ORDER (u, l) ACCESS l INDEX loans_user_idx
VARIANT wide ELSE ORDER (l, u) ACCESS l SCAN, u TUPLE_KEY;
ARG(k)is the k-th literal of the statement. The same applies when it is a bind value.ACCESS <table> <pin>:TUPLE_KEY,INDEX [name],CABIN [(column)],SCAN, orNOT …. It does not force an access method the statement cannot use.BUILD <table> ON|OFF: whether to read the inner side of the join once and keep it in memory.ADVISE PLAN FOR <select>: returns the order the engine would choose now, the cost ratio against the currently running plan (cost_ratio), and aCREATE PLANstatement that can be used as is. It changes nothing.ALTER PLAN … DISABLE|ENABLE|REPLACE VARIANT,DROP PLAN. All require admin and run outside a transaction.
Whichever plan is used, the rows returned are the same. A plan changes only the amount of work.
In the Sandbox
The sandbox account is readonly, so it cannot run CREATE PLAN, SET JOIN_ORDER, or GATHER STATISTICS. The load script gathered and locked the statistics of the three tables (LOCK STATISTICS), so the order chosen by the default fixed is the same on every execution. The tuning scenarios change the steps through the statement shape (join keys, conditions, LIMIT) instead of through plans.
Comparison with Other DBMSs
| KDBS | Oracle | SQL Server | |
|---|---|---|---|
| What decides the order | Engine (statistics), then capture | CBO | Optimizer |
| Directives inside SQL | None | Hints LEADING, USE_NL | OPTION (FORCE ORDER) |
| Pinning outside SQL | CREATE PLAN, captured order | SQL Plan Baseline, SQL Profile | Query Store plan forcing |
| Default plan stability | fixed: does not change on its own | May change when statistics change | May change at recompilation |