[12.x] Query builder PDO fetch modes #54443
Draft
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.
Hi, this is not the first time I've requested this idea but I would like you to reconsider the following addition for Laravel 12.x since I think it opens up many many performance improvements. This PR has simplified the premise a little bit by only relying on a single new
$query->fetchArgs()->...
function.PDO fetch modes allow developers to control the way how database statements are retrieved/used in PHP. Currently in Laravel, the default mode is
PDO::FETCH_OBJ
which returns a php object for every row in the result set.There are however various other fetch modes that can offer greater efficiency of the code, e.g. retrieving a dictionary of 2 columns with
PDO::FETCH_KEY_PAIR
(which is like the query builderpluck()
function) or keying a resultset instantly by some other column (PDO::FETCH_UNIQUE
). Making these options available to the query builder will mean faster code since a lot of processing that was traditionally done using Laravel's Collection methods, can now be done using a simple query modifier.TLDR
This PR adds the following:
fetchArgs(...$args)
function for a new way to modify the format of the query result.pluck()
to use this new PDO mode to remove unneeded code and speed up the execution.array $fetchArgs = []
argument (possibly Breaking Change).onceWithColumns
because it behaves weirdly (it only actually uses your arguments if no columns have been assigned). 2 internal references have been simplified to not use this function anymore.Usage examples
Return key-value pairs
By selecting only two columns and using
PDO::FETCH_KEY_PAIR
, the result is automatically returned as a dictionary, requiring no further processing:This PR rewrites the
pluck()
method to use this idea, and because of that a slew of internal parsing methods have now been removed since the resultset is correct from the get-go.Benchmark (click)
Key a collection by a given column
The first column in the select-statement is consumed as the key for the output array, so it essentially functions as a
$collection->keyBy('id')
but instead it is returned directly from PDO (so no more$collection->keyBy()
required):This will come in handy when some algorithm needs to retrieve users by key, since that is an
O(1)
(efficient) operation.Fetch single column as list
Group
username
byrole
A bit of an odd one, but say you have a column
role
which contains either'admin','moderator','user'
and you want to retrieve usernames for each of the groups. Traditionally, you'd have to loop over the array in PHP and build these groups yourself. There is however a much easier way:Cursors
All of the above will also be available for the
$query->cursor()
function so you can lazily process your data.Conclusion
Keep in mind that the whole reason for this PR is to run code as efficiently as possible. The more we can ask from PDO to build the correct data structure, the less we have to rely on Laravel Collection methods to reprocess our data.
All of the modes mentioned above can help tremendously when building efficient algorithms to process data from your database. @taylorotwell I urge you to look carefully at this PR and communicate any issues you might see with this implementation.
Some other resouces to have a look at if you want to know more: