[12.x] Add QueriesExecuted to expose cumulative query information #54674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DB::whenQueryingForLongerThan()
and requiresDB::enableQueryLog()
to be called.Background
While investigating long-running queries using
DB::whenQueryingForLongerThan()
, I discovered that the method only provides information about individual queries. This can be misleading since the threshold is actually triggered by cumulative query time across the request lifecycle, not just a single long query.For example, if you set a threshold of 500ms:
Changes
This PR introduces:
QueriesExecuted
class that replacesQueryExecuted
in thewhenQueryingForLongerThan
callbackWhy This Matters
While Laravel does provide
getQueryLog()
andtotalQueryDuration()
, developers new to the framework may not discover these methods without diving into the source code. Having comprehensive query information directly in the method from the event makes the feature more intuitive and useful.Breaking Changes
whenQueryingForLongerThan()
now passesQueriesExecuted
instead ofQueryExecuted
DB::enableQueryLog()
for this feature to functionNote
This PR builds upon and extends the groundwork laid in #42744. Many thanks to @timacdonald for implementing the query duration monitoring feature that made this enhancement possible.