Skip to content

tdfs4ds.utils.explain_cost

tdfs4ds.utils.explain_cost

What a query will cost, read from its Teradata EXPLAIN text, before running it.

explain_cost(text) (pure) reads the optimizer's own estimates:

  • the estimated time of each step ("The estimated time for this step is 21 minutes and 58 seconds") and the total when the plan states one;
  • the size of each spool ("estimated with low confidence to be 2,526,469,696 rows (1,073,749,620,800 bytes)");
  • product joins and all-rows scans.

Lines are wrapped anywhere (a number can start on the next line), so the text is flattened first. A plan of trivial steps states no time at all: that reads as 0.

assess_explain(text, max_seconds, max_spool_bytes) adds the verdict. Step times are summed, which over-counts steps run in parallel: the gate errs on the cautious side. The estimates are the optimizer's (often "low confidence"), never a measurement.

_UNITS module-attribute

_UNITS = {'hour': 3600.0, 'minute': 60.0, 'second': 1.0}

_DURATION module-attribute

_DURATION = '((?:[\\d,.]+\\s+(?:hours?|minutes?|seconds?)(?:\\s*(?:,|and)\\s*)?)+)'

_STEP_TIME module-attribute

_STEP_TIME = compile('estimated time for this step is ' + _DURATION, IGNORECASE)

_TOTAL_TIME module-attribute

_TOTAL_TIME = compile('total estimated time is ' + _DURATION, IGNORECASE)

_PART module-attribute

_PART = compile('([\\d,.]+)\\s+(hour|minute|second)s?', IGNORECASE)

_SPOOL module-attribute

_SPOOL = compile('estimated(?: with (\\w+) confidence)? to be ([\\d,]+) rows?\\s*\\(\\s*([\\d,]+) bytes\\s*\\)', IGNORECASE)

_OVERFLOW module-attribute

_OVERFLOW = compile('estimated(?: with (\\w+) confidence)? to be \\*+ rows', IGNORECASE)

_STEP module-attribute

_STEP = compile('(?:^|\\s)(\\d+)\\)\\s')

_seconds

_seconds(text: str) -> float

_int

_int(text: str) -> int

explain_cost

explain_cost(text: str) -> dict

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

human_seconds

human_seconds(s: float) -> str

human_bytes

human_bytes(b: int) -> str

assess_explain

assess_explain(text: str, max_seconds: float = 60, max_spool_bytes: int = 10 * 1024 ** 3) -> dict

explain_cost plus ok and the reasons it is not. Pure.