[英]Performance of VALUES(col_name) function in the UPDATE clause
The question is about SQL
legacy code for MySQL
database. 问题是关于
MySQL
数据库的SQL
遗留代码。
It is known, that when doing INSERT ... ON DUPLICATE KEY UPDATE
statement VALUES(col_name)
function can be used to refer to column values from the INSERT portion instead of passing there exact values: 众所周知,在执行
INSERT ... ON DUPLICATE KEY UPDATE
语句时, 可以使用 VALUES(col_name)
函数来引用INSERT部分中的列值,而不是向其中传递确切值:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE b=VALUES(b), c=VALUES(c)
My legacy code contains a lot of huge inserts in parametrized style (they are used in batch-inserts): 我的遗留代码包含许多参数化样式的巨大插入(它们在批处理插入中使用):
INSERT INTO table (a,b,c, <...dozens of params...>) VALUES (?,?,?,<...dozens of values...>)
ON DUPLICATE KEY UPDATE b=?, c=?, <...dozens of params...>
The question is: would it increase performance of batch-inserts if I will change all these queries to use VALUES(col_name)
function (in UPDATE
portion)? 问题是:如果我将所有这些查询更改为使用
VALUES(col_name)
函数(在UPDATE
部分中),是否会提高批量插入的性能?
My queries are executed from java code using jdbc
driver. 我的查询是使用
jdbc
驱动程序从Java代码执行的。 So, what I guess, is that for long text values it should significantly reduce size of queries. 因此,我猜想是对于长文本值,它将大大减少查询的大小。 What about
MySQL
it self? MySQL
本身如何呢? Would it really in general give me increasing of speed? 总的来说,这真的能使我提高速度吗?
Batched inserts can may run 10 times as fast and one row at a time. 分批插入的速度可以快10倍,一次可以运行一行。 The reason for this is all the network, etc, overhead.
其原因是所有网络等的开销。
Another technique is to change from a single batched IODKU into two statements -- one to insert the new rows, one to do the updates. 另一种技术是从单个批处理的IODKU更改为两个语句-一个用于插入新行,一个用于进行更新。 (I don't know if that will run any faster.) Here is a discussion of the two steps, in the context of "normalization".
(我不知道这样做是否会更快。) 这是在“规范化”的背景下讨论的两个步骤。
Another thing to note: If there is an AUTO_INCREMENT
involved (not as one of the columns mentioned), then IODKU may "burn" ids for the cases where it does an 'update'. 需要注意的另一件事:如果涉及到
AUTO_INCREMENT
(而不是其中提到的一列),则IODKU可能会在执行“更新”的情况下“刻录” ID。 That is, the IODKU (and INSERT IGNORE
and a few others) get all the auto_incs that it might need, then proceeds to use the ones it does need and waste the others. 也就是说,IODKU(和
INSERT IGNORE
和其他几个人)得到所有可能需要的auto_incs,然后继续使用它需要的人,也浪费了其他人。
You get into "diminishing returns" if you try to insert more than a few hundred rows in a batch. 如果您尝试在批次中插入几百行以上的内容,则会陷入“收益递减”的状态。 And you stress the rollback log.
您会强调回滚日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.