jamis0n
jamis0n

Reputation: 3800

Laravel Update Record - Is this the most efficient with 2 MySQL calls?

So as a follow up to an earlier post, I'm curious if this is the most efficient way to update a record based on POST data.

Routes.php

//COLLECT POST DATA IN $thisOrder ARRAY
//DETERMINE THAT $thisOrderID is the primary key of the record to be updated
$updateOrder = Order::find($thisOrderID)->update($thisOrder);

To me it seems that this is performing 2 queries:

1 - SELECT * FROM tblOrder WHERE orderid=$thisOrderID
2 - UPDATE SET //myvalues = $thisOrder// WHERE orderid = $thisOrderID

I understand that this is not correct syntax, but it explains it quickly.

Am I understanding this correctly? Using the find() command is doing a SELECT and update() is doing an update.

Can't I just do an update directly if I know the ID?

Thanks!

Upvotes: 0

Views: 401

Answers (1)

Joe Green
Joe Green

Reputation: 364

Yes you are doing it correctly. By using the ORM you are abstracting yourself from the underlying SQL. Ie. you are trading efficiency for simplicity (arguably) and less code. This is not a bad thing and the abstraction here is perfectly legitimate.

I wouldn't worry about this. As long as you have an Index on the orderid column. Build a working app first and optimise second. There is no need to optimise unless you can explain why you need to. And if you need to the chances are this kind of thing will be the least of your worries. At that point, you find the bottleneck in your app and optimise. The chances are this ORM will not be the bottleneck.

This is an over simplification so here is a good read that goes into a little more detail

Upvotes: 1

Related Questions