Skip to content

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:

  • slower first 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_SECONDS or more) counts when the step that dominates each plan's time was estimated with confidence. A proposed plan estimated partly with low confidence is still slower on such a signal, with a note;
  • faster / same from the estimated times and the plan shapes;
  • not_comparable when 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.

VERDICTS module-attribute

VERDICTS = (FASTER, SAME, SLOWER, NOT_COMPARABLE)

TIME_TOLERANCE module-attribute

TIME_TOLERANCE = 0.15

TIME_FLOOR_SECONDS module-attribute

TIME_FLOOR_SECONDS = 1.0

LARGE_SCAN_ROWS module-attribute

LARGE_SCAN_ROWS = 1000000

METRICS module-attribute

METRICS = ('est_seconds', 'spool_gb', 'steps', 'product_joins', 'all_rows_scans', 'partitions_read', 'confidence')

_ALLOWED module-attribute

_ALLOWED = ('EXPLAIN ', 'DYNAMIC EXPLAIN ')

_STEP_SPLIT module-attribute

_STEP_SPLIT = re.compile('(?:^|\\s)(?=\\d+\\)\\s)')

_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

_ROWS = re.compile('estimated(?: with \\w+ confidence)? to be ([\\d,]+) rows?', re.IGNORECASE)

_PARTS module-attribute

_PARTS = re.compile('\\bfrom\\s+(\\d[\\d,]*)\\s+partitions? of\\b', re.IGNORECASE)

_SINGLE_PART module-attribute

_SINGLE_PART = re.compile('\\bfrom\\s+a\\s+single\\s+partition\\s+of\\b', re.IGNORECASE)

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'

explain_cost

explain_cost(text: str) -> dict

The optimizer's estimates in an EXPLAIN text. Pure.

human_seconds

human_seconds(s: float) -> str

_err

_err(exc) -> str

large_scans

large_scans(text: str, min_rows: int = LARGE_SCAN_ROWS) -> List[dict]

[{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

partitions_read(text: str) -> Optional[int]

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

plan_confidence(text: str) -> str

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

plan_metrics(text: str) -> dict

The metrics of one plan (explain_cost plus large scans and partitions). Pure.

_as_plan

_as_plan(p) -> dict

A plan given as text, or as {text, ok, error}.

_delta

_delta(a, b)

compare_plans

compare_plans(old_plan, new_plan, reference: Optional[dict] = None) -> dict

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:

  1. a failed plan gives not_comparable;
  2. 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 slower whatever 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;
  3. 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);
  4. otherwise the estimated times decide (slower / faster beyond :data:TIME_TOLERANCE, one plan at :data:TIME_FLOOR_SECONDS or more), then the plan shapes (faster), else same.

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.

_pct

_pct(rel: float) -> str

_check

_check(sql: str) -> str

_plan

_plan(run_sql: Callable, sql: str) -> dict

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.