tdfs4ds.utils.learned_reads¶
tdfs4ds.utils.learned_reads
¶
Learned reads: a page reads ahead what it read the last time (0.3.8.2).
A page of the admin app reads the same statements every time it is built: the same catalogs, the
same SHOW VIEW of the same objects, one date per manager. At 0.3 to 0.5 s per round trip the wait
is the NUMBER of statements. learned(key) records the reads a page issues; the next time the page is
built it reads them all at the start in a few grouped requests (:mod:tdfs4ds.utils.grouped_read), and
every reader that then sends one of those statements is answered from the rows. No reader changes: a
statement that was not predicted exactly runs by itself, as before, so the page returns what it always
returned (a changed store only costs the statements that changed).
What is replayed:
SELECTandSHOW VIEW/SHOW TABLEstatements that SUCCEEDED the last time, alone in their request, and that do not depend on the session (no query band, no session table, noSELECT CURRENT_...). A statement that failed is not replayed (the reader sends it and sees its own error).EXPLAIN SELECT * FROM v(the compile check of a view). Several EXPLAINs cannot share a request (measured live: Error 3706), butEXPLAIN SELECT * FROM v1; SELECT * FROM v2; ...is ONE request whose plan covers every statement, so its success proves every view compiles. It is bisected on an error, down to the view that fails alone, whose own EXPLAIN then runs and returns its own error (:func:compile_group, consulted bydependencies.explain_viewonly).
The record lives in memory and in ~/.tdfs4ds/cache/learned_reads (one JSON file per user and page,
statement texts only, discarded when the package version changes). TDFS4DS_LEARNED_READS=off turns it
off. Rows are read at the start of the page, seconds before the readers would have read them; a write
sent through teradataml.execute_sql forgets them (:func:tdfs4ds.utils.grouped_read.forget).
_RECORDER
module-attribute
¶
_COMPILE_OK
module-attribute
¶
_SESSION_DEPENDENT
module-attribute
¶
_SESSION_DEPENDENT = re.compile('GETQUERYBAND|SESSIONINFO|\\bSESSION\\b|^SELECT\\s+(?:CURRENT_|USER\\b|DATABASE\\b|DATE\\b|TIME\\b)|\\bSAMPLE\\b|\\bRANDOM\\b', re.IGNORECASE)
_COMPILE_START
module-attribute
¶
_ABSENT_ERROR
module-attribute
¶
_ABSENT_ERROR = re.compile("\\[Error 3807\\].*?Object '([^']+)' does not exist", re.IGNORECASE | re.DOTALL)
_WRONG_KIND_ERROR
module-attribute
¶
_SHOW_OBJECT
module-attribute
¶
replayable
¶
True when sql can be read ahead: a single SELECT or SHOW that does not depend on the session.
record
¶
Called by the execute_sql wrapper for a statement that succeeded (served from a prefetch or not).
record_failure
¶
Called by the execute_sql wrapper for a statement that raised. Two answers that stay true until
an object is created or dropped are kept: "the object does not exist" (Error 3807) and "not a view" /
"not a table" (3854 / 3853, a SHOW VIEW on a table: the code tries one then the other). The
replay checks the dictionary once before it believes them (see :func:learned).
compile_ok
¶
True when EXPLAIN SELECT * FROM v was shown to compile by a grouped check in this context.
compile_group
¶
The statements EXPLAIN SELECT * FROM <view> that compile, in as few requests as possible
(see the module docstring). Returns the normalised statements known to compile.
_existence_sql
¶
One dictionary read that says which of the objects named by recorded failures exist now, and what.
_inject_absent
¶
Answer a recorded failure by its error when the dictionary still says what made it fail: the object is not listed ("absent"), or it is listed and is not a view / not a table. Anything else (the object exists now, an unreadable dictionary) is left to its reader.
forget_learned
¶
Drop the recorded reads of key (all of them when None), in memory (tests, a store switch).
learned
¶
Read ahead what the page key read last time, and record what it reads this time.
A nested learned block does nothing (the outer one records and replays). The replay never
raises: when it fails, the readers run one by one. Yields {"replayed": n, "compiles_ok": n}.