简体   繁体   English

UPDATE子句中VALUES(col_name)函数的性能

[英]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.

相关问题 不使用“ where”子句,但出现错误:“未知列” <col_name> 在where子句中” - Not using `where` clause, but getting error:“unknown column <col_name> in where clause” 错误:在createSQLQuery中找不到列&#39;col_name&#39; - ERROR: Column 'col_name' not found with createSQLQuery 是否有任何理由使用INSERT INTO tbl_name()VALUES(); 语法,而不是SET col_name = col_value,…? - Is there any reason to use INSERT INTO tbl_name () VALUES(); syntax instead of SET col_name = col_value,…? mysql(5.1)插入语法&gt; col_name = value? - mysql (5.1) insert syntax > col_name=value? 如果列名存储在变量中,如何获取列(NEW。{{col_name}})-MySQL - How to get a column (NEW.{{col_name}}) if its name is stored in a variable - MySQL MySql SELECT * FROM table_name WHERE col_name IN(任何值) - MySql SELECT * FROM table_name WHERE col_name IN (any value) count(distinct col_name)与计算select distinct查询的行不同吗? - Is count(distinct col_name) different from counting the rows of a select distinct query? mysql : 选择 where with group by with GROUP_CONCAT(col_name SEPARATOR &#39;, &#39;) - mysql : select where with group by with GROUP_CONCAT(col_name SEPARATOR ', ') 如何在postgresql中的查询中写SUM(IF(boolean,integer,integer))col_name - How to write SUM(IF(boolean, integer, integer)) col_name in query in postgresql 如何使用SET col_name =语法使mysqldump输出 - How do I make mysqldump output using the SET col_name= syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM