Skip to content

tdfs4ds.impact_reference

tdfs4ds.impact_reference

The original process's execution log as the cost reference of an impact study. Read only.

An EXPLAIN estimate and a measured SELECT say little on their own: is 9 seconds a lot for this process? The process's own history answers it. :func:read_execution_reference reads, for one process:

  • its follow-up rows (FS_V_FOLLOW_UP): the durations of its last :data:REFERENCE_RUNS COMPLETED runs (a run groups the segments / dates of one call, :func:tdfs4ds.process_store.process_followup.execution_run_ids);
  • when the optional history table FS_EXECUTION_RESOURCES holds them, the CPU seconds, I/O and largest spool of the same runs (DBQL figures harvested by :mod:tdfs4ds.utils.execution_resources).

:func:reference_from_log (pure) turns the rows into {process_id, n_runs, duration_s: {median, max, last}, cpu_s, io, spool_gb, source: follow_up | resources | none, note}.

A logged duration covers the WHOLE run: computing the view, staging, the MERGE into the feature tables, statistics. It is a reference for the order of magnitude, never the time of the SELECT alone (see :data:REFERENCE_NOTE).

The reference is used by the efficiency comparison (the proposed plan's estimate is stated against it, :func:compare_with_reference), by the gate of the measured comparison (the limits grow to :data:REFERENCE_FACTOR times the logged median when that is above the generic limits, :func:effective_limits), and after a measurement (:func:measured_reference).

READ ONLY: the only statements sent are SELECTs, checked before they are sent. Fail-open: a failed read gives source: 'none' with the reason, never an exception.

REFERENCE_RUNS module-attribute

REFERENCE_RUNS = 10

REFERENCE_FACTOR module-attribute

REFERENCE_FACTOR = 3

MAX_ROWS module-attribute

MAX_ROWS = 5000

REFERENCE_NOTE module-attribute

REFERENCE_NOTE = 'Logged durations cover the whole run of the process (computing the view, staging, the MERGE into the feature tables, statistics): a reference for the order of magnitude, not the time of the SELECT alone.'

NO_LOG_NOTE module-attribute

NO_LOG_NOTE = 'no COMPLETED run of the process is logged: the generic limits apply (SAMPLE_MAX_SECONDS / SAMPLE_MAX_SPOOL_GB)'

NOT_A_PROCESS_NOTE module-attribute

NOT_A_PROCESS_NOTE = 'not a registered process: no execution log, the generic limits apply (SAMPLE_MAX_SECONDS / SAMPLE_MAX_SPOOL_GB)'

_err

_err(exc) -> str

_num

_num(x) -> Optional[float]

_median

_median(values: List[float]) -> Optional[float]

_r

_r(x, nd=3)

empty_reference

empty_reference(process_id=None, note: str = NO_LOG_NOTE) -> dict

A reference with nothing logged.

_frame

_frame(rows)

_completed_runs

_completed_runs(df, n_runs: int)

[(start, end, row_index_list)] of the last n_runs runs whose every row COMPLETED, newest first.

reference_from_log

reference_from_log(rows, resources=None, process_id=None, n_runs: int = REFERENCE_RUNS) -> dict

The cost reference of a process from its follow-up rows (rows: dicts or a frame with RUN_ID, PROCESS_ID, START_DATETIME, END_DATETIME, STATUS) and, when given, its FS_EXECUTION_RESOURCES rows (resources: the same columns plus AMP_CPU_SEC, TOTAL_IO, MAX_SPOOL_BYTES). Pure.

Only runs whose every row COMPLETED count (a run that failed part-way says nothing about the cost); the most recent n_runs are kept. Durations: one per COMPLETED follow-up row (one execution of the process: a date or a segment), newest first, in seconds.

_human

_human(s) -> str

reference_line

reference_line(reference: Optional[dict]) -> str

One sentence on the logged runs, or '' when nothing is logged. Pure.

compare_with_reference

compare_with_reference(reference: Optional[dict], est_seconds=None, est_spool_gb=None) -> Optional[str]

The proposed plan's estimate stated against the logged history, or None when nothing is logged. Pure.

e.g. "the original process takes about 6 s per run (median of 10 logged run(s), ...); the proposed plan is estimated at 9.3 s (1.5 times the logged median)".

effective_limits

effective_limits(max_seconds: float, max_spool_gb: float, reference: Optional[dict] = None, factor: float = REFERENCE_FACTOR) -> dict

The limits of the measured comparison: max(generic, factor x logged median) for the time and the spool. {seconds, spool_gb, note}; with no log the generic limits and a note saying so. Pure.

measured_reference

measured_reference(reference: Optional[dict], old: Optional[dict], new: Optional[dict]) -> dict

{logged, measured_old_s, measured_new_s, text, note}: the measured seconds of both SELECTs beside the logged runs. Pure.

_check

_check(sql: str) -> str

followup_sql

followup_sql(process_id) -> str

The follow-up rows of one process, the most recent first. Pure.

resources_sql

resources_sql(process_id) -> str

The stored resource rows of one process (FS_EXECUTION_RESOURCES), the most recent first. Pure.

_rows

_rows(run_sql: Callable, sql: str) -> List[dict]

read_execution_reference

read_execution_reference(process_id, run_sql: Optional[Callable] = None) -> dict

The cost reference of process process_id from its execution log. Never raises for a database error: a failed follow-up read gives source: 'none' with the reason; an unreadable FS_EXECUTION_RESOURCES (optional table) is skipped. run_sql (default teradataml.execute_sql) gets each SELECT and returns a cursor (description + fetchall).