在 SQL 中,UPDATE 总是比 DELETE+INSERT 快吗? [英] In SQL, is UPDATE always faster than DELETE+INSERT?
问题描述
假设我有一个包含以下字段的简单表格:
Say I have a simple table that has the following fields:
- ID:整数,自动增量(身份),主键
- 名称:varchar(50),唯一,具有唯一索引
- 标签:int
我从不使用 ID 字段进行查找,因为我的应用程序始终基于使用 Name 字段.
I never use the ID field for lookup, because my application is always based on working with the Name field.
我需要不时更改标签值.我正在使用以下简单的 SQL 代码:
I need to change the Tag value from time to time. I'm using the following trivial SQL code:
UPDATE Table SET Tag = XX WHERE Name = YY;
我想知道是否有人知道上面的是否总是比:
I wondered if anyone knows whether the above is always faster than:
DELETE FROM Table WHERE Name = YY;
INSERT INTO Table (Name, Tag) VALUES (YY, XX);
再次 - 我知道在第二个示例中 ID 已更改,但这对我的应用程序无关紧要.
Again - I know that in the second example the ID is changed, but it does not matter for my application.
推荐答案
这个答案有点晚了,但是因为我遇到了类似的问题,所以我在同一台机器上用 JMeter 和 MySQL 服务器做了一个测试,我有使用:
A bit too late with this answer, but since I faced a similar question, I made a test with JMeter and a MySQL server on same machine, where I have used:
- 包含两个 JDBC 请求的事务控制器(生成父示例):删除和插入语句
- 包含更新语句的单独 JDBC 请求.
运行 500 次循环后,我得到了以下结果:
After running the test for 500 loops, I have obtained the following results:
DEL + INSERT - 平均:62 毫秒
DEL + INSERT - Average: 62ms
更新 - 平均:30 毫秒
Update - Average: 30ms
结果:
这篇关于在 SQL 中,UPDATE 总是比 DELETE+INSERT 快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!