#102354Extbase creates new prepared statement for each query
Description
The TYPO3 DBAL abstraction creates prepared statements for each query for security reasons. Usually that’s not a problem but with edge cases it is.
For example when bulk processing entities. Let’s say you load 100 entities with one releation to another entity each from the db. The query to fetch the related entity is the same for all 100 entities but 100 prepared statements are created. That’s not how prepared statements are supposed to be used. There should be one statement per Query and only the parameters should change. Somewhere inside the DBAL layer all prepared statements seem to be cached, so the RAM usage is growing heavily.
This could be solved by a runtime cache, caching prepared statements for each query and reusing the statements.
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 1
-
Alexander Schnitzler commented and changed status
NewClosed <span data-processor="relativetime" data-value="2026-10-17T19:19:00+00:00" > 10/17/26, 19:19 </span>
Coming back to my own report with measurements on main.
The first part holds: every Extbase query is prepared again, even if the SQL is identical. Loading 20 blogs and resolving their lazy relations prepares the same SQL once per blog: 80 prepares for 4 distinct SQL strings, each prepared statement is executed exactly once. This is how doctrine/dbal works: @Connection::executeQuery()@ prepares, binds and executes on every call, and Extbase runs each query through it (@Typo3DbBackend@). Neither DBAL nor TYPO3 nor doctrine/orm keeps a statement cache.
My assumption that the statements are cached somewhere and make the memory grow was wrong: 2000 Extbase queries in a loop do not increase memory usage, the statements are released. Memory growth in bulk processing comes from the identity map, the mapped objects or a query logger.
The prepare itself is cheap: about 2 microseconds per query on MariaDB and about 13 on SQLite, i.e. roughly 1 to 6 percent of a lazy relation query including mapping (about 200 microseconds). The real cost is the number of queries, one per parent object and relation, not their preparation.
A statement cache would add per-connection state, lifetime and invalidation questions for a small gain, and it would belong to the core database layer, not to Extbase. For bulk jobs, @QueryBuilder::prepare()@ can be reused explicitly, or the related records can be fetched in one query.
Closing my own report as works as designed.