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

ItemValue
Settingjoin_order = fixed (default) / planned / written. Change at runtime with SET JOIN_ORDER (admin)
fixedAfter 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
plannedChosen from statistics on every execution. Statistics move once per second, in steps of about ±30%
writtenIn the order written
Statements with subqueriesAlways the written order
Administrator planCREATE PLAN … VARIANT … ORDER (…) ACCESS … BUILD …. Takes priority over every setting
Statistics objectRow 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
ValueMeaning
order=capturedUsed the captured order
order=plan plan=<name>/<variant>Administrator plan
order=writtenWritten order
capture=first / deferredThis 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, or NOT …. 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 a CREATE PLAN statement 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

KDBSOracleSQL Server
What decides the orderEngine (statistics), then captureCBOOptimizer
Directives inside SQLNoneHints LEADING, USE_NLOPTION (FORCE ORDER)
Pinning outside SQLCREATE PLAN, captured orderSQL Plan Baseline, SQL ProfileQuery Store plan forcing
Default plan stabilityfixed: does not change on its ownMay change when statistics changeMay change at recompilation

Related pages