开发者

Database: get and delete or just delete?

开发者 https://www.devze.com 2023-04-02 06:04 出处:网络
Performance/good practice question - should I check if object exists or ju开发者_Python百科st delete it?

Performance/good practice question - should I check if object exists or ju开发者_Python百科st delete it?

So:

obj = getObj(someid);
if(obj != null) {
    deleteObj(someId);
}

Or just:

deleteObj(someId);

?


From a performance standpoint you will likely be better off just making an attempt at deleting a record based on id versus trying to fetch the record and then going back to the database to delete it. It's always good practice to limit the number of transactions on your database.

DELETE FROM TableName
WHERE Id = @Id

You will know if you deleted any records based on the row count that is returned from a query similar to the one above.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号