Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Avoid no-op database query in Model::destroy() with empty ids #35294

Merged
merged 2 commits into from
Nov 19, 2020
Merged

[8.x] Avoid no-op database query in Model::destroy() with empty ids #35294

merged 2 commits into from
Nov 19, 2020

Conversation

spawnia
Copy link
Contributor

@spawnia spawnia commented Nov 19, 2020

I recently discovered that calling Model::destroy() with an empty array produces an unnecessary no-op database query.

select * from `tasks` where 0 = 1

This happens because the destroy() method calls Builder::whereIn(), which in turn turn an empty set of ids into the condition 0 = 1:

Since I do not know the reasons for adding the 0 = 1 condition, I went for a more cautious and minimal change.

Now, destroy() checks for an empty input and bails early, saving a database roundtrip for a query that is guaranteed to have no effect.

@driesvints driesvints changed the title Avoid no-op database query in Model::destroy() with empty ids [8.x] Avoid no-op database query in Model::destroy() with empty ids Nov 19, 2020
@taylorotwell taylorotwell merged commit 777e40a into laravel:8.x Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants