zidance's avatar

UPDATE or INSERT statement better for this case?

I have a simple table to store data just for reading. For example, It consists of id, payload, user_id and timestamps.

I am doing sync every day for this table and just renew the data in payload column.

Is it better to use UPDATE or INSERT statement? If I am using INSERT statement means I need to delete the rows and re-insert again. Which may caused the increment ID grow faster.

From my point of view, UPDATE statement will run perform better right? CMIIW.

0 likes
4 replies
Tray2's avatar

If you have the record already in the table you should update it instead of removing it and recreating it. Update is one transaction while deleting and recreating needs two.

1 like
MohamedTammam's avatar

How about updateOrCreate, which will update the record if it exists, otherwise it will insert new record.

For performance doing only insert will be better, but are you sure you want to keep deleting your records?

Check the docs for more details: https://laravel.com/docs/8.x/eloquent#upserts

1 like
zidance's avatar

@MohamedTammam Yeah, right. Because the info will just last for 1 day or few days maybe. So the record will be overwritten with latest info.

Please or to participate in this conversation.