selectOne should use fetch directly instead of array_shift #49245
Unanswered
haidubogdan
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I always found this thing very peculiar.
If we want to fetch one row, the default laravel framework logic is:
we execute a query -> fetchAll rows -> and shift the first element of the array.
I see this having some performance issues and risks. Imagine we have a table with 10 000 rows and a beginner is thinking, that we have the
selectOne
method just to take just the first row from it.Yet by using the existing method he will probably get a memory exception or a very big latency issue as we are filling an array with 10 000 rows, just to get the first result.
Pdo already has the
fetch
method which will return just one row from the query.https://www.php.net/manual/en/pdostatement.fetch.php
So maybe it can be used in the following laravel updates.
I've avoided to label this Idea an issue as it all boils down to how the user uses his application.
And I'm surprised that nobody complained about this yet.
How to reproduce
It's easy to reproduce a memory exhaustion exception. Fill in a table with 10.000 rows and use
selectOne
with a raw query.Beta Was this translation helpful? Give feedback.
All reactions