Explain and cost

Explain output shows the work that a call completed. The example executes the names query with an explain record and reads its completed stages.

Example

Explain a query

query_text holds this document:

dust58 B
find    [?name]
where   [[?e name ?name]]
orderBy [?name]
odin282 B
definition := stardust.parse(query_text, .DUST) or_return
defer stardust.close(&definition)

query := stardust.query(db, definition) or_return
defer stardust.close(&query)

result: stardust.Query_Explain
rows := stardust.run(query, explain = &result) or_return
stardust.close(&rows)
Result / 818 B
{load_definition   {cpu         45
                    wall        50
                    arena_bytes 1104}
 parameters        {cpu         3
                    wall        4
                    arena_bytes 0}
 run               {cpu         2764
                    wall        2764
                    arena_bytes 7440}
 total             {cpu         2832
                    wall        2837
                    arena_bytes 8544}
 arena_capacity    12288
 work              753
 rows              3
 compile           127
 plan              242
 execute           0
 group             0
 order             0
 memory_bytes      79
 memory_refusals   0
 budget_bytes      10489856512
 budget_used_bytes 159
 budget_peak_bytes 2985
 budget_refusals   0
 retry             0
 stages_completed  3
 retries           0}

Failure output

A failed call can still supply explain output for the stages that completed.

  1. Read stages_completed.
  2. Then read the fields of the stages that completed.

The record tables below give the fields, absent values, and time units of each operation.

Stored plan decisions

A call's cost record describes that execution. It does not load stored plan history. Use the plan operation in Definitions with the explain action to read accepted plans and their history. Each revision reports its version, source and reason. Trial decisions include candidate proposals and measured verdicts.

Set page_size and max_bytes to bound one diagnostic response. The response includes the current accepted transaction, the complete plan state and a history page from one snapshot. Pass a nonzero nextAfter as the next page's argument.

The adaptive query planner can retry a capped plan with the statistics plan. Query explain reports those attempts in retry and retries.

cost

This record measures one explain stage. The cpu and wall fields are durations. The ABI gives 100-ns ticks. Thus, a time less than one tick is zero.

FieldTypeUnitRangeAbsentMeaning
cputime.DurationHost duration (100 ns ticks at the ABI)Tick range-1Thread CPU time. The call measures this value. Independent calls can differ.
walltime.DurationHost duration (100 ns ticks at the ABI)Tick range—Wall time. The call measures this value. Independent calls can differ.
arena_bytesbuiltin.i64BytesHost type range—Bytes the stage allocated from the scratch arena of the call. Heap-owned outputs and the LuaJIT heap are not included. The call measures this value. Independent calls can differ.

build_explain

This record contains explain output for build.

FieldTypeUnitMeaning
prepare_expressionCostNone—
compileCostNone—
totalCostNoneThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacitybuiltin.i64BytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedbuiltin.i64CountStages that ran.
cache_hitboolNoneThe leased runtime holds a compiled copy of the expression. The call measures this value. Independent calls can differ.

eval_explain

This record contains explain output for evaluate.

FieldTypeUnitMeaning
prepare_variablesCostNone—
evaluateCostNone—
adopt_documentCostNone—
totalCostNoneThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacitybuiltin.i64BytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
document_bytesbuiltin.i64BytesUsed bytes of the native value, string and source buffers of the result. The call measures this value. Independent calls can differ.
stages_completedbuiltin.i64CountStages that ran.

query_explain

This record contains explain output for query run. The compile, plan, execute, group and order fields divide the wall time of run into stages.

FieldTypeUnitRangeMeaning
load_definitionCostNoneHost type range—
parametersCostNoneHost type range—
runCostNoneHost type rangePlanning, execution and aggregation. Pages render later, in next.
totalCostNoneHost type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacitybuiltin.i64BytesHost type rangeBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
worktime.DurationHost duration (100 ns ticks at the ABI)Tick rangeThread CPU duration consumed by the query. The call measures this value. Independent calls can differ.
rowsbuiltin.i64CountHost type rangeResult rows.
compiletime.DurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
plantime.DurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
executetime.DurationHost duration (100 ns ticks at the ABI)Tick rangeProduction of solutions. A streamed result produces solutions while pages render. Thus, execute measures only the solutions that run reads. The call measures this value. Independent calls can differ.
grouptime.DurationHost duration (100 ns ticks at the ABI)Tick rangeAggregates, windows and computed columns. The call measures this value. Independent calls can differ.
ordertime.DurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
memory_bytesbuiltin.i64BytesHost type rangeThe most working memory the run held reserved in the database's memory budget at once. The call measures this value. Independent calls can differ.
memory_refusalsbuiltin.i64CountHost type rangeReservations of the run that the memory budget granted less than asked. The call measures this value. Independent calls can differ.
budget_bytesbuiltin.i64BytesHost type rangeThe database's memory budget (memory_budget_bytes).
budget_used_bytesbuiltin.i64BytesHost type rangeBytes every query of the database held reserved when run returned. The call measures this value. Independent calls can differ.
budget_peak_bytesbuiltin.i64BytesHost type rangeThe most bytes the database's queries held reserved at once since it opened. The call measures this value. Independent calls can differ.
budget_refusalsbuiltin.i64CountHost type rangeReservations the database's budget granted less than asked since it opened. The call measures this value. Independent calls can differ.
retrytime.DurationHost duration (100 ns ticks at the ABI)Tick rangeThread CPU duration of the attempts given up because the planner's plan passed its cap. work includes it. The call measures this value. Independent calls can differ.
stages_completedbuiltin.i64CountHost type rangeStages that ran.
retriesbuiltin.i64CountHost type rangeAttempts given up because the planner's plan passed its cap; the query then ran the statistics' plan within the rest of its budget.

mutation_explain

This record contains explain output for mutation run.

FieldTypeUnitMeaning
load_definitionCostNone—
parametersCostNone—
runCostNone—
totalCostNoneThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacitybuiltin.i64BytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedbuiltin.i64CountStages that ran.
attemptsbuiltin.i64CountEvaluations. More than one when a concurrent write forced a retry.

patch_explain

This record contains explain output for merge_patch.

FieldTypeUnitMeaning
copy_patchCostNone—
commitCostNoneValidation and the commit.
totalCostNoneThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacitybuiltin.i64BytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedbuiltin.i64CountStages that ran.

Explain examples

The generator captured each result from the native library.

Inspect one measured cost stage

expression_text holds this document:

dust9 B
[+ ?x 1]
odin243 B
expression := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&expression)

explain: stardust.Build_Explain
program := stardust.build(expression, &explain) or_return
defer stardust.close(&program)

result := explain.total
Result / 47 B
{cpu         17
 wall        18
 arena_bytes 0}

Explain expression compilation

expression_text holds this document:

dust9 B
[+ ?x 1]
odin237 B
expression := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&expression)

explain: stardust.Build_Explain
program := stardust.build(expression, &explain) or_return
defer stardust.close(&program)

result := explain
Result / 395 B
{prepare_expression {cpu         2
                     wall        2
                     arena_bytes 0}
 compile            {cpu         10
                     wall        10
                     arena_bytes 0}
 total              {cpu         14
                     wall        14
                     arena_bytes 0}
 arena_capacity     65536
 stages_completed   2
 cache_hit          true}

Explain patch validation and commit

seed_text holds this document:

dust232 B
#_ada {name Ada
       role engineer
       age  36}
#_bob {name   Bob
       role   engineer
       age    41
       mentor {#link #_ada}}
#_cy  {name Cy
       role designer
       age  29}
#_tx  {author smoke
       reason seed}
odin224 B
patch := stardust.parse(seed_text, .DUST) or_return
defer stardust.close(&patch)

explain: stardust.Patch_Explain
commit := stardust.merge_patch(db, patch, &explain) or_return
defer stardust.close(&commit)

result := explain
Result / 373 B
{copy_patch       {cpu         515
                   wall        521
                   arena_bytes 2520}
 commit           {cpu         334
                   wall        30754
                   arena_bytes 10056}
 total            {cpu         973
                   wall        31405
                   arena_bytes 12590}
 arena_capacity   4194304
 stages_completed 2}

Explain mutation execution

birthday_text holds this document:

dust68 B
query {find  [?e]
       where [[?e name Ada]]}
patch {?e {age 37}}
odin319 B
source := stardust.parse(birthday_text, .DUST) or_return
defer stardust.close(&source)

mutation := stardust.mutation(db, source) or_return
defer stardust.close(&mutation)

explain: stardust.Mutation_Explain
commit := stardust.run(mutation, explain = &explain) or_return
defer stardust.close(&commit)

result := explain
Result / 498 B
{load_definition  {cpu         4840
                   wall        4845
                   arena_bytes 1744}
 parameters       {cpu         7
                   wall        8
                   arena_bytes 0}
 run              {cpu         7358
                   wall        32056
                   arena_bytes 43984}
 total            {cpu         12278
                   wall        36983
                   arena_bytes 45728}
 arena_capacity   4194304
 stages_completed 3
 attempts         1}

Explain expression evaluation

expression_text holds this document:

dust9 B
[+ ?x 1]

input_text holds this document:

dust5 B
x 41
odin404 B
expression := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&expression)

program := stardust.build(expression) or_return
defer stardust.close(&program)

input := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&input)

explain: stardust.Eval_Explain
output := stardust.evaluate(program, input, &explain) or_return
defer stardust.close(&output)

result := explain
Result / 484 B
{prepare_variables {cpu         4
                    wall        6
                    arena_bytes 0}
 evaluate          {cpu         30
                    wall        31
                    arena_bytes 0}
 adopt_document    {cpu         4
                    wall        4
                    arena_bytes 0}
 total             {cpu         41
                    wall        44
                    arena_bytes 0}
 arena_capacity    65536
 document_bytes    42
 stages_completed  3}

Explain query execution

query_text holds this document:

dust58 B
find    [?name]
where   [[?e name ?name]]
orderBy [?name]
odin309 B
definition := stardust.parse(query_text, .DUST) or_return
defer stardust.close(&definition)

query := stardust.query(db, definition) or_return
defer stardust.close(&query)

explain: stardust.Query_Explain
rows := stardust.run(query, explain = &explain) or_return
defer stardust.close(&rows)

result := explain
Result / 819 B
{load_definition   {cpu         48
                    wall        59
                    arena_bytes 1104}
 parameters        {cpu         3
                    wall        4
                    arena_bytes 0}
 run               {cpu         3326
                    wall        3359
                    arena_bytes 7440}
 total             {cpu         3402
                    wall        3446
                    arena_bytes 8544}
 arena_capacity    12288
 work              1033
 rows              3
 compile           147
 plan              360
 execute           0
 group             0
 order             0
 memory_bytes      79
 memory_refusals   0
 budget_bytes      10489856512
 budget_used_bytes 159
 budget_peak_bytes 2985
 budget_refusals   0
 retry             0
 stages_completed  3
 retries           0}