简体   繁体   English

(My)SQL-批量更新

[英](My)SQL - batch update

Hey, I have a table with "id", "name", and "weight" columns. 嘿,我有一个带有“ id”,“ name”和“ weight”列的表。 Weight is an unsigned small int. 重量是一个无符号的小整数。

I have a page that displays items ordered by "weight ASC". 我有一个页面,显示按“重量ASC”排序的项目。 It'll use drag-n-drop, and once the order is changed, will pass out a comma-separated string of ids (in the new order). 它将使用拖放操作,更改顺序后,将传递以逗号分隔的ID字符串(在新顺序中)。

Let's say there's 10 items in that table. 假设该表中有10个项目。 Here's what I have so far: 这是我到目前为止的内容:

Sample input: 输入样例:

5,6,2,9,10,4,8,1,3,7

Sample PHP handler (error handlers & security stuff excluded): 示例PHP处理程序(不包括错误处理程序和安全性内容):

<?php
$weight = 0;
$id_array = explode(',', $id_string);

foreach ($id_array as $key => $val)
{
    mysql_query("UPDATE tbl SET weight = '$weight' where id = '$val' LIMIT 1");
    $weight++;
}
?>

When I make a change to column order, will my script need to make 10 separate UPDATE queries, or is there a better way? 当我更改列顺序时,我的脚本是否需要进行10个单独的UPDATE查询,还是有更好的方法?

You could create a temporary table with the new data in it (ie, id and weight are the columns), then update the table with this data. 您可以创建一个包含新数据的临时表(即id和weight为列),然后使用此数据更新表。

create temporary table t (id int, weight float);

insert into t(id, weight) values (1, 1.0), (2, 27), etc

update tbl inner join t on t.id = tbl.id
set tbl.weight = t.weight;

So, you have one create statement, one insert statement, and one update statement. 因此,您有一个create语句,一个insert语句和一个update语句。

You can only specify one where clause in a single query -- which means, in your case, that you can only update one row at a time. 您只能在单个查询中指定一个where子句-这意味着,在您的情况下,一次只能更新一行。


With 10 items, I don't know if I would go through that kind of troubles (it means re-writing some code -- even if that's not that hard) , but, for more, a solution would be to : 有10个项目,我不知道我是否会遇到这种麻烦(这意味着要重写一些代码-即使那不是那么困难) ,但是,更多的解决方案是:

  • delete all the rows delete所有行
  • insert s them all back insert它们全部insert
  • doing all that in a transaction , of course. 当然,在交易中进行所有操作。

The nice point is that you can do several insert s in a single query ; 令人高兴的一点是,您可以在单个查询中执行多个insert don't know for 10 items, but for 25 or 50, it might be quite nice. 不知道有10个项目,但对于25个或50个项目,可能会很不错。

Here is an example, from the insert page of the MySQL manual (quoting) : 这是一个示例,来自MySQL手册插入页面 (引用)

INSERT statements that use VALUES syntax can insert multiple rows. 使用VALUES语法的INSERT语句可以插入多行。 To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. 为此,请包括多个列值列表,每个列值括在括号内并用逗号分隔。
Example: 例:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

Of course, you should probably not insert "too many" items in a single insert query -- an insert per 50 items might be OK, though (to find the "right" number of items, you'll have to benchmark, I suppose ^^ ) 当然,您可能不应该在单个插入查询中插入“太多”项目-每50个项目插入一次就可以了,尽管(要想找到“正确”数量的项目,您必须进行基准测试,我想^^)

Yes, you would need to do 10 updates. 是的,您将需要进行10次更新。 There are ways to batch up multiple queries in a single call to mysql_query, but it's probably best to avoid that. 有多种方法可以在对mysql_query的单个调用中对多个查询进行批处理,但是最好避免这种情况。

If it's performance you are worried about, make sure you try it first before worrying about that. 如果您担心它的性能,请确保在尝试之前先尝试一下。 I suspect that doing 10 (or even 20 or 30) updates will be plenty fast. 我怀疑进行10次(甚至20或30次)更新会很快。

10 updates is the simplest way conceptually. 从概念上讲,10个更新是最简单的方法。 if you've got a bazillion rows that need to be updated, then you might have to try something different, such as creating a temporary table and using a JOIN in your UPDATE statement or a subquery with a row constructor. 如果您有成千上万的行需要更新,那么您可能必须尝试不同的方法,例如创建临时表并在UPDATE语句中使用JOIN或具有行构造函数的子查询。

将记录存储在带有批处理插入的临时表中,然后从tbl中删除记录,然后从temp表中将批处理插入到tbl中

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

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