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]
c670 B
stardust_document *definition = NULL;
stardust_db_query *query = NULL;
stardust_db_rows *rows = NULL;
stardust_db_query_explain explain = {0};
int64_t status = stardust_parse(query_text, STARDUST_DUST, &definition);
stardust_db_source source = {.kind = STARDUST_SOURCE_DOCUMENT, .document = definition};
if (status == STARDUST_OK) status = stardust_db_query_open(db, &source, &query);
if (status == STARDUST_OK) status = stardust_db_query_run(query, NULL, NULL, &explain, &rows);
stardust_db_rows_release(rows);
stardust_db_query_release(query);
stardust_document_release(definition);
if (status != STARDUST_OK) return status;
stardust_db_query_explain result = explain;
Result / 819 B
{load_definition   {cpu         62
                    wall        73
                    arena_bytes 1104}
 parameters        {cpu         4
                    wall        5
                    arena_bytes 0}
 run               {cpu         3979
                    wall        4027
                    arena_bytes 7440}
 total             {cpu         4076
                    wall        4136
                    arena_bytes 8544}
 arena_capacity    12288
 work              1332
 rows              3
 compile           177
 plan              462
 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.

stardust_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.

FieldC typeUnitRangeMeaning
cpu_ticksint64_t100 ns ticksSigned i64Thread CPU time. The call measures this value. Independent calls can differ.
wall_ticksint64_t100 ns ticksSigned i64Wall time. The call measures this value. Independent calls can differ.
arena_bytesint64_tBytesSigned i64Bytes 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.

stardust_expr_build_explain

This record contains explain output for build.

FieldC typeUnitRangeMeaning
prepare_expressionstardust_costNoneC type range—
compilestardust_costNoneC type range—
totalstardust_costNoneC type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacityint64_tBytesSigned i64Bytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedint64_tCountSigned i64Stages that ran.
cache_hitint64_tNoneSigned i64The leased runtime holds a compiled copy of the expression. The call measures this value. Independent calls can differ.

stardust_expr_eval_explain

This record contains explain output for evaluate.

FieldC typeUnitRangeMeaning
prepare_variablesstardust_costNoneC type range—
evaluatestardust_costNoneC type range—
adopt_documentstardust_costNoneC type range—
totalstardust_costNoneC type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacityint64_tBytesSigned i64Bytes the scratch arena reserved. The call measures this value. Independent calls can differ.
document_bytesint64_tBytesSigned i64Used bytes of the native value, string and source buffers of the result. The call measures this value. Independent calls can differ.
stages_completedint64_tCountSigned i64Stages that ran.

stardust_db_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.

FieldC typeUnitRangeMeaning
load_definitionstardust_costNoneC type range—
parametersstardust_costNoneC type range—
runstardust_costNoneC type rangePlanning, execution and aggregation. Pages render later, in next.
totalstardust_costNoneC type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacityint64_tBytesSigned i64Bytes the scratch arena reserved. The call measures this value. Independent calls can differ.
work_ticksint64_t100 ns ticksSigned i64Thread CPU duration consumed by the query. The call measures this value. Independent calls can differ.
rowsint64_tCountSigned i64Result rows.
compile_ticksint64_t100 ns ticksSigned i64The call measures this value. Independent calls can differ.
plan_ticksint64_t100 ns ticksSigned i64The call measures this value. Independent calls can differ.
execute_ticksint64_t100 ns ticksSigned i64Production 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.
group_ticksint64_t100 ns ticksSigned i64Aggregates, windows and computed columns. The call measures this value. Independent calls can differ.
order_ticksint64_t100 ns ticksSigned i64The call measures this value. Independent calls can differ.
memory_bytesint64_tBytesSigned i64The 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_refusalsint64_tCountSigned i64Reservations of the run that the memory budget granted less than asked. The call measures this value. Independent calls can differ.
budget_bytesint64_tBytesSigned i64The database's memory budget (memory_budget_bytes).
budget_used_bytesint64_tBytesSigned i64Bytes every query of the database held reserved when run returned. The call measures this value. Independent calls can differ.
budget_peak_bytesint64_tBytesSigned i64The most bytes the database's queries held reserved at once since it opened. The call measures this value. Independent calls can differ.
budget_refusalsint64_tCountSigned i64Reservations the database's budget granted less than asked since it opened. The call measures this value. Independent calls can differ.
retry_ticksint64_t100 ns ticksSigned i64Thread 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_completedint64_tCountSigned i64Stages that ran.
retriesint64_tCountSigned i64Attempts given up because the planner's plan passed its cap; the query then ran the statistics' plan within the rest of its budget.

stardust_db_mutation_explain

This record contains explain output for mutation run.

FieldC typeUnitRangeMeaning
load_definitionstardust_costNoneC type range—
parametersstardust_costNoneC type range—
runstardust_costNoneC type range—
totalstardust_costNoneC type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacityint64_tBytesSigned i64Bytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedint64_tCountSigned i64Stages that ran.
attemptsint64_tCountSigned i64Evaluations. More than one when a concurrent write forced a retry.

stardust_db_patch_explain

This record contains explain output for merge_patch.

FieldC typeUnitRangeMeaning
copy_patchstardust_costNoneC type range—
commitstardust_costNoneC type rangeValidation and the commit.
totalstardust_costNoneC type rangeThe cost of all stages. arena_bytes records the peak scratch-arena allocation.
arena_capacityint64_tBytesSigned i64Bytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedint64_tCountSigned i64Stages 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]
c445 B
stardust_document *expression = NULL;
stardust_expr_build_explain explain = {0};
stardust_expr_program *program = NULL;
int64_t status = stardust_parse(expression_text, STARDUST_DUST, &expression);
if (status == STARDUST_OK) status = stardust_expr_build(expression, &explain, &program);
stardust_expr_program_release(program);
stardust_document_release(expression);
if (status != STARDUST_OK) return status;
stardust_cost result = explain.total;
Result / 47 B
{cpu         18
 wall        25
 arena_bytes 0}

Explain expression compilation

expression_text holds this document:

dust9 B
[+ ?x 1]
c453 B
stardust_document *expression = NULL;
stardust_expr_build_explain explain = {0};
stardust_expr_program *program = NULL;
int64_t status = stardust_parse(expression_text, STARDUST_DUST, &expression);
if (status == STARDUST_OK) status = stardust_expr_build(expression, &explain, &program);
stardust_expr_program_release(program);
stardust_document_release(expression);
if (status != STARDUST_OK) return status;
stardust_expr_build_explain result = explain;
Result / 395 B
{prepare_expression {cpu         1
                     wall        2
                     arena_bytes 0}
 compile            {cpu         11
                     wall        11
                     arena_bytes 0}
 total              {cpu         15
                     wall        15
                     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}
c425 B
stardust_document *patch = NULL;
stardust_db_patch_explain explain = {0};
stardust_result *written = NULL;
int64_t status = stardust_parse(seed_text, STARDUST_DUST, &patch);
if (status == STARDUST_OK) status = stardust_db_merge_patch(db, patch, NULL, &explain, &written);
stardust_result_release(written);
stardust_document_release(patch);
if (status != STARDUST_OK) return status;
stardust_db_patch_explain result = explain;
Result / 373 B
{copy_patch       {cpu         167
                   wall        178
                   arena_bytes 2520}
 commit           {cpu         186
                   wall        27204
                   arena_bytes 10056}
 total            {cpu         421
                   wall        27451
                   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}}
c710 B
stardust_document *definition = NULL;
stardust_db_mutation *mutation = NULL;
stardust_result *written = NULL;
stardust_db_mutation_explain explain = {0};
int64_t status = stardust_parse(birthday_text, STARDUST_DUST, &definition);
stardust_db_source source = {.kind = STARDUST_SOURCE_DOCUMENT, .document = definition};
if (status == STARDUST_OK) status = stardust_db_mutation_open(db, &source, &mutation);
if (status == STARDUST_OK) status = stardust_db_mutation_run(mutation, NULL, NULL, &explain, &written);
stardust_result_release(written);
stardust_db_mutation_release(mutation);
stardust_document_release(definition);
if (status != STARDUST_OK) return status;
stardust_db_mutation_explain result = explain;
Result / 495 B
{load_definition  {cpu         134
                   wall        140
                   arena_bytes 1744}
 parameters       {cpu         6
                   wall        6
                   arena_bytes 0}
 run              {cpu         7318
                   wall        32639
                   arena_bytes 43984}
 total            {cpu         7544
                   wall        32872
                   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
c761 B
stardust_document *expression = NULL;
stardust_document *input = NULL;
stardust_expr_program *program = NULL;
stardust_result *output = NULL;
stardust_expr_eval_explain explain = {0};
int64_t status = stardust_parse(expression_text, STARDUST_DUST, &expression);
if (status == STARDUST_OK) status = stardust_parse(input_text, STARDUST_DUST, &input);
if (status == STARDUST_OK) status = stardust_expr_build(expression, NULL, &program);
if (status == STARDUST_OK) status = stardust_expr_evaluate(program, input, &explain, &output);
stardust_result_release(output);
stardust_expr_program_release(program);
stardust_document_release(input);
stardust_document_release(expression);
if (status != STARDUST_OK) return status;
stardust_expr_eval_explain result = explain;
Result / 484 B
{prepare_variables {cpu         3
                    wall        4
                    arena_bytes 0}
 evaluate          {cpu         24
                    wall        24
                    arena_bytes 0}
 adopt_document    {cpu         3
                    wall        3
                    arena_bytes 0}
 total             {cpu         33
                    wall        34
                    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]
c670 B
stardust_document *definition = NULL;
stardust_db_query *query = NULL;
stardust_db_rows *rows = NULL;
stardust_db_query_explain explain = {0};
int64_t status = stardust_parse(query_text, STARDUST_DUST, &definition);
stardust_db_source source = {.kind = STARDUST_SOURCE_DOCUMENT, .document = definition};
if (status == STARDUST_OK) status = stardust_db_query_open(db, &source, &query);
if (status == STARDUST_OK) status = stardust_db_query_run(query, NULL, NULL, &explain, &rows);
stardust_db_rows_release(rows);
stardust_db_query_release(query);
stardust_document_release(definition);
if (status != STARDUST_OK) return status;
stardust_db_query_explain result = explain;
Result / 819 B
{load_definition   {cpu         45
                    wall        53
                    arena_bytes 1104}
 parameters        {cpu         3
                    wall        4
                    arena_bytes 0}
 run               {cpu         3471
                    wall        3496
                    arena_bytes 7440}
 total             {cpu         3539
                    wall        3572
                    arena_bytes 8544}
 arena_capacity    12288
 work              1231
 rows              3
 compile           171
 plan              417
 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}