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:

edn66 B
{:find    [?name]
 :where   [[?e "name" ?name]]
 :orderBy [?name]}
clojure186 B
(with-open [definition (sd/parse query-text :edn)
            query (sd/query db definition)
            ^java.io.Closeable rows (sd/run query nil {:explain? true})]
  (sd/explain rows))
Result / 1.1 kB
{:value {:load_definition   {:cpu         1074
                             :wall        1080
                             :arena_bytes 1680}
         :parameters        {:cpu         8
                             :wall        11
                             :arena_bytes 0}
         :run               {:cpu         9854
                             :wall        9952
                             :arena_bytes 7424}
         :total             {:cpu         10969
                             :wall        11076
                             :arena_bytes 9104}
         :arena_capacity    4194304
         :work              6712
         :rows              3
         :compile           275
         :plan              490
         :execute           0
         :group             0
         :order             0
         :memory_bytes      5995
         :memory_refusals   0
         :budget_bytes      10489856512
         :budget_used_bytes 10194
         :budget_peak_bytes 14267
         :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 next_after 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
cpuDurationHost duration (100 ns ticks at the ABI)Tick rangenilThread CPU time. The call measures this value. Independent calls can differ.
wallDurationHost duration (100 ns ticks at the ABI)Tick range—Wall time. The call measures this value. Independent calls can differ.
arena_bytesIntegerBytesHost 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_capacityIntegerBytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedCountCountStages 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_capacityIntegerBytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
document_bytesIntegerBytesUsed bytes of the native value, string and source buffers of the result. The call measures this value. Independent calls can differ.
stages_completedCountCountStages 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_capacityIntegerBytesHost type rangeBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
workDurationHost duration (100 ns ticks at the ABI)Tick rangeThread CPU duration consumed by the query. The call measures this value. Independent calls can differ.
rowsIntegerCountHost type rangeResult rows.
compileDurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
planDurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
executeDurationHost 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.
groupDurationHost duration (100 ns ticks at the ABI)Tick rangeAggregates, windows and computed columns. The call measures this value. Independent calls can differ.
orderDurationHost duration (100 ns ticks at the ABI)Tick rangeThe call measures this value. Independent calls can differ.
memory_bytesIntegerBytesHost 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_refusalsIntegerCountHost type rangeReservations of the run that the memory budget granted less than asked. The call measures this value. Independent calls can differ.
budget_bytesIntegerBytesHost type rangeThe database's memory budget (memory_budget_bytes).
budget_used_bytesIntegerBytesHost type rangeBytes every query of the database held reserved when run returned. The call measures this value. Independent calls can differ.
budget_peak_bytesIntegerBytesHost 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_refusalsIntegerCountHost type rangeReservations the database's budget granted less than asked since it opened. The call measures this value. Independent calls can differ.
retryDurationHost 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_completedCountCountHost type rangeStages that ran.
retriesCountCountHost 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_capacityIntegerBytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedCountCountStages that ran.
attemptsCountCountEvaluations. 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_capacityIntegerBytesBytes the scratch arena reserved. The call measures this value. Independent calls can differ.
stages_completedCountCountStages that ran.

Explain examples

The generator captured each result from the native library.

Inspect one measured cost stage

expression-text holds this document:

edn10 B
["+" ?x 1]
clojure147 B
(with-open [expression (sd/parse expression-text :edn)
            program (sd/build expression {:explain? true})]
  (:total (sd/explain program)))
Result / 75 B
{:value {:cpu         63
         :wall        69
         :arena_bytes 0}}

Explain expression compilation

expression-text holds this document:

edn10 B
["+" ?x 1]
clojure138 B
(with-open [expression (sd/parse expression-text :edn)
            program (sd/build expression {:explain? true})]
  (sd/explain program))
Result / 514 B
{:value {:prepare_expression {:cpu         7
                              :wall        13
                              :arena_bytes 0}
         :compile            {:cpu         40
                              :wall        40
                              :arena_bytes 0}
         :total              {:cpu         53
                              :wall        59
                              :arena_bytes 0}
         :arena_capacity     65536
         :stages_completed   2
         :cache_hit          true}}

Explain patch validation and commit

seed-text holds this document:

edn287 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"}}
clojure103 B
(with-open [patch (sd/parse seed-text :edn)]
  (sd/explain (sd/merge-patch db patch {:explain? true})))
Result / 484 B
{:value {:copy_patch       {:cpu         1543
                            :wall        1557
                            :arena_bytes 3096}
         :commit           {:cpu         647
                            :wall        34348
                            :arena_bytes 8008}
         :total            {:cpu         2356
                            :wall        36081
                            :arena_bytes 11118}
         :arena_capacity   4194304
         :stages_completed 2}}

Explain mutation execution

birthday-text holds this document:

edn82 B
{:query {:find  [?e]
         :where [[?e "name" "Ada"]]}
 :patch {:?e {:age 37}}}
clojure149 B
(with-open [source (sd/parse birthday-text :edn)
            mutation (sd/mutation db source)]
  (sd/explain (sd/run mutation nil {:explain? true})))
Result / 647 B
{:value {:load_definition  {:cpu         730
                            :wall        737
                            :arena_bytes 2320}
         :parameters       {:cpu         11
                            :wall        14
                            :arena_bytes 0}
         :run              {:cpu         13580
                            :wall        60228
                            :arena_bytes 43856}
         :total            {:cpu         14452
                            :wall        61120
                            :arena_bytes 46176}
         :arena_capacity   4194304
         :stages_completed 3
         :attempts         1}}

Explain expression evaluation

expression-text holds this document:

edn10 B
["+" ?x 1]

input-text holds this document:

edn7 B
{:x 41}
clojure229 B
(with-open [expression (sd/parse expression-text :edn)
            program (sd/build expression)
            input (sd/parse input-text :edn)
            output (sd/evaluate program input {:explain? true})]
  (sd/explain output))
Result / 638 B
{:value {:prepare_variables {:cpu         10
                             :wall        18
                             :arena_bytes 0}
         :evaluate          {:cpu         136
                             :wall        136
                             :arena_bytes 0}
         :adopt_document    {:cpu         6
                             :wall        6
                             :arena_bytes 0}
         :total             {:cpu         158
                             :wall        166
                             :arena_bytes 0}
         :arena_capacity    65536
         :document_bytes    42
         :stages_completed  3}}

Explain query execution

query-text holds this document:

edn66 B
{:find    [?name]
 :where   [[?e "name" ?name]]
 :orderBy [?name]}
clojure186 B
(with-open [definition (sd/parse query-text :edn)
            query (sd/query db definition)
            ^java.io.Closeable rows (sd/run query nil {:explain? true})]
  (sd/explain rows))
Result / 1.1 kB
{:value {:load_definition   {:cpu         512
                             :wall        584
                             :arena_bytes 1680}
         :parameters        {:cpu         7
                             :wall        9
                             :arena_bytes 0}
         :run               {:cpu         7049
                             :wall        7089
                             :arena_bytes 7424}
         :total             {:cpu         7590
                             :wall        7703
                             :arena_bytes 9104}
         :arena_capacity    4194304
         :work              4453
         :rows              3
         :compile           174
         :plan              428
         :execute           0
         :group             0
         :order             0
         :memory_bytes      5995
         :memory_refusals   0
         :budget_bytes      10489856512
         :budget_used_bytes 10194
         :budget_peak_bytes 14267
         :budget_refusals   0
         :retry             0
         :stages_completed  3
         :retries           0}}