简体   繁体   English

mysql UPDATE比INSERT INTO更快吗?

[英]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? 如果我正在运行插入新行的50,000个查询,以及更新这些行的50,000个查询,哪个会花费更少的时间?

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. 更新时,您可以一次更新多行,但不能将其应用于每个更新情况,并且通常您必须一次运行一个更新查询(更新特定ID时) - 并且在大表上这是非常的慢慢必须找到行,然后每次更新它。 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). 同样,这取决于某些因素 - 特别是(尚未提及)数据库引擎,例如InnoDB(现在在PHPMyAdmin中是标准的)。

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. 我同意其他所有人的意见 - 在个案基础上要考虑太多,因此你真的需要根据自己的需要运行自己的测试并从那里评估情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM