Tutorial :Is mysql UPDATE faster than INSERT INTO?


This is more of a theory question.

If I'm running 50,000 queries that insert new rows, and 50,000 queries that updates those rows, which one will take less time?


Insert would be faster because with update you need to first search for the record that you are going to update and then perform the update.

Though this hardly seems like a valid comparison as you never have a choice whether to insert or update as the two fill two completely different needs.

EDIT: I should add too that this is with the assumption that there are no insert triggers or other situations that could cause potential bottlenecks.


Insert Operation : Create  -> Store    Update Operation : Retrieve -> Modify -> Store  

Insert Operation faster.


With an insert into the same table, you can always insert all the rows with one query, making it much faster than inserting one by one. When updating, you can update several rows at a time, but you cannot apply this to every update situation, and often you have to run one update query at a time (when updating a specific id) - and on a big table this is very slow having to find the row and then update it every time. It is also slower even if you have indexed the table, by my experience.


As an aside here, don't forget that by doing loads more inserts than updates, you have more rows when you come to select, so you'll slow down the read operation.

So the real question then becomes - what do you care about more, a quick insert or a speedy read. Again, this is dependant on certain factors - particularly (and not yet mentioned) DB engine, such as InnoDB (which is now standard in PHPMyAdmin incidentally).

I agree with everyone else though - there's too much to consider on a case-by-case basis and therefore you really need to run your own tests and assess the situation from there based on your needs.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »