tdfs4ds.impact_efficiency¶
tdfs4ds.impact_efficiency
¶
Is the proposed SQL faster or slower than the current one? EXPLAIN only, nothing is run.
:func:collect_efficiency asks the database for the plans of the current object (SELECT * FROM
<view>) and of the proposed SELECT, with EXPLAIN and, when it can, DYNAMIC EXPLAIN under
forced incremental planning (the plan built with the values a manager scalar returns, as the
workflow assessment reads it). :func:compare_plans (pure) reads both plans with
:func:tdfs4ds.utils.explain_cost.explain_cost and gives a verdict:
slowerfirst from a cost signal the plan shows even with low confidence: a new product join, a new all-rows scan of a large table, a spool too large to print. A slower estimated time (more than :data:TIME_TOLERANCE, one plan at :data:TIME_FLOOR_SECONDSor more) counts when the step that dominates each plan's time was estimated with confidence. A proposed plan estimated partly with low confidence is stillsloweron such a signal, with a note;faster/samefrom the estimated times and the plan shapes;not_comparablewhen a plan could not be read, or when, with no cost signal, the step that dominates the time of either plan was estimated with low or no confidence (statistics missing).
Confidence is weighed on the dominant-time step (the step with the largest estimated time): a
low-confidence spool elsewhere in the plan makes the plan partial, not low.
When the edited object is a registered process, reference holds its execution log
(:mod:tdfs4ds.impact_reference: the durations of its last COMPLETED runs, CPU / I/O / spool when
harvested) and the proposed plan's estimate is stated against it in the notes. Logged durations cover
the whole run (ingestion included): a reference, not the SELECT alone.
The estimates are the optimizer's, never a measurement (see :mod:tdfs4ds.impact_measure for that).
READ ONLY: the only statements sent are EXPLAIN and DYNAMIC EXPLAIN (checked before they are
sent), the SELECTs of the execution log (checked by :mod:tdfs4ds.impact_reference), plus the query
band that :func:tdfs4ds.utils.query_band.forced_incremental_planning sets and restores for the
session around the DYNAMIC EXPLAIN.
METRICS
module-attribute
¶
METRICS = ('est_seconds', 'spool_gb', 'steps', 'product_joins', 'all_rows_scans', 'partitions_read', 'confidence')
_SCAN_FROM
module-attribute
¶
_SCAN_FROM = re.compile('\\bfrom\\s+(?:a single partition of\\s+|\\d[\\d,]*\\s+partitions of\\s+)?([A-Za-z_\\"][\\w$#.\\"]*)\\s+by way of an all-rows scan', re.IGNORECASE)
_ROWS
module-attribute
¶
_PARTS
module-attribute
¶
_SINGLE_PART
module-attribute
¶
PARTLY_LOW_NOTE
module-attribute
¶
PARTLY_LOW_NOTE = 'proposed estimate partly low-confidence (statistics missing): the times are uncertain, the cost signal above is not'
large_scans
¶
[{table, rows}]: the all-rows scans of a table (not of a spool) whose step is estimated at
min_rows rows or more. Pure.
partitions_read
¶
The partitions the plan reads from partitioned tables (from N partitions of, a single
partition of), or None when the plan names none. Pure.
plan_confidence
¶
ok (every estimate made with confidence), partial (some step estimated with low or no
confidence, but not the step with the largest estimated time) or low (the dominant-time step was,
or no step is timed and some estimate was). Pure.
plan_metrics
¶
The metrics of one plan (explain_cost plus large scans and partitions). Pure.
compare_plans
¶
Compare two EXPLAIN plans (text, or {text, ok, error}). Pure.
Returns {verdict, reason, metrics: {name: {old, new, delta}}, notes, reference_comparison}.
Order of the rules:
- a failed plan gives
not_comparable; - a cost signal of the proposed plan (a new product join, a new all-rows scan of a large table, a new
spool too large to print) gives
slowerwhatever the confidence of the estimates (the plan shows them even when estimated with low confidence), with a note when the proposed estimate is partly or wholly low-confidence; - when the dominant-time step of either plan was estimated with low or no confidence, the times cannot
be compared:
not_comparable(the metrics are still filled); - otherwise the estimated times decide (
slower/fasterbeyond :data:TIME_TOLERANCE, one plan at :data:TIME_FLOOR_SECONDSor more), then the plan shapes (faster), elsesame.
reference (:func:tdfs4ds.impact_reference.read_execution_reference): the proposed estimate is
stated against the process's logged runs, in reference_comparison and the notes.
collect_efficiency
¶
collect_efficiency(old_select: str, new_select: str, *, run_sql: Optional[Callable] = None, dynamic: bool = True, planning=None, process_id: Optional[str] = None, reference: Optional[dict] = None) -> dict
The efficiency block of the impact model: plans of both SELECTs and their comparison.
run_sql (default teradataml.execute_sql) gets each statement; planning is a context
manager factory that forces incremental planning for the DYNAMIC EXPLAIN (default
:func:tdfs4ds.utils.query_band.forced_incremental_planning when run_sql is the default; none
with a given run_sql). process_id: the edited process, whose execution log is read as the cost
reference (:func:tdfs4ds.impact_reference.read_execution_reference, SELECTs only); reference
gives that reference instead of reading it. Fail-open: a refused DYNAMIC EXPLAIN falls back to the
static plans; a failed EXPLAIN gives not_comparable and an unverified entry; an unreadable
log gives a reference with source: 'none'. Never raises for a database error.