Is it more efficient to always run a DELETE query by default whether an entry exists or not, for example to delete a user name after a certain period of time (DELETE * from table WHERE username='user'
), or should you first check if the rows to be deleted exist using a SELECT
query and checking mysql_num_rows
.
What uses more processor resources on the server side?
Obviously one approach contains more code, but I was wondering if certain mysql operations used a lot more CPU than others.
Delete is more efficient since the system takes as much time (and literally do exactly the same work) finding rows to delete as it would have on select.
However, if you wish to have special logic kick in if zero rows were deleted, you can use ROW_COUNT() function and check if it's zero after the delete.
Also, see the related answer here.
There is no need to check, because the MySQL will do that check for you anyway. Checking will be one more extra query which you don't really need, because of the way DELETE works it is already a checking query by itself and if found record will be deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With