Repository navigation
Description
Repeated execution of static SQL can avoid recognizing named parameters and reconstructing the same JDBC SQL on every execution.
For example:
INSERT INTO records (id, amount) VALUES (?id, ?amount)The SQL structure and parameter order stay constant while the parameter values change between rows.
Current behavior
CachedSqlTokenizer caches statement strings and injection offsets. Repeated execution through SqlExecutor still invokes SqlParserBase.handleStatement, which uses regex matching to recognize parameter names and builds the substituted SQL before consulting the prepared-statement cache.
This makes repeated static statements a candidate for caching more of the parsing work. The performance benefit needs to be measured; no speedup is assumed.
Profiling context
This issue arose from the Scriptella benchmark work. The internal doc: MariaDB JFR spike records the investigation; its optimization proposal contains the detailed stack analysis. These internal docs require repository access.
The exploratory recording used Scriptella 1.6, JDK 17, MariaDB 11.8.8, and the v3 million-row batch-1000 workload. Of 289 Scriptella execution samples, 26 ended in regex predicate execution through Matcher.find and SqlParserBase.handleStatement, and another 9 ended in Matcher.reset through the same parser path. This supports investigating repeated parsing, but one profiled pair does not establish an expected speedup.
The same analysis attributes the dominant array-growth samples to CSV parsing and identifies generic JDBC type dispatch separately. Those costs remain outside this issue.
For performance validation, rerun the unchanged v3 million-row MariaDB batch-1000 workload with an explicitly identified candidate artifact and an unchanged Scriptella 1.6 control. Use repeated unprofiled runs to compare elapsed time; profiler spans and sample percentages are diagnostic evidence, not benchmark timings or predicted gains.
Proposed improvement
Cache an immutable binding plan for eligible static statements containing simple named JDBC parameters. The plan would contain:
- The positional JDBC SQL, such as
INSERT INTO records (id, amount) VALUES (?, ?). - The ordered parameter names, retaining duplicates.
Each execution should resolve fresh values from the current parameter callback. Cache syntax only, never parameter values or callbacks, and retain the existing statement execution, JDBC conversion, batching, statistics, warning handling, cancellation, and error handling paths.
Keep the initial eligibility rule narrow and consistent with existing in-memory resource caching. Statements requiring dynamic textual substitutions, expression parameters, or unsupported forms should continue using the current parser. Preserve existing handling of comments and quoted strings, and the cache lifecycle after successful parsing and failures.
Acceptance criteria
- Existing SQL/parser and relevant JDBC tests pass.
- Repeated executions correctly bind changing values, including nulls and duplicate parameters.
- Parameter order, multiple statements, comments, and quoted strings behave correctly.
- Dynamic SQL and expression behavior remains unchanged through the fallback path.
- Batching, final partial batches, and error handling retain their existing behavior.
- Performance is evaluated using repeated, unprofiled runs against an unchanged Scriptella 1.6 baseline, with the benchmark workload and configuration held constant.
- Statements outside the optimized path show no functional or material performance regression.
CSV parsing allocation and JDBC type-dispatch costs are separate optimization candidates and are outside this issue's scope.
Activity
Cache binding plans for repeated static SQL (#71)