SQL中有没有快速更新多条记录的方法?

发布于 2024-08-05 04:29:24 字数 414 浏览 3 评论 0原文

我需要用我根据 CodeID 创建的新名称替换 20 000 多个名称。

例如:我必须用“cat”更新包含“dog”(其 CodeID 为 1)的所有行,并用“bird”更新包含“horse”(其 CodeID 为 2)的所有行,等等。

第一个 SQL 语句: UPDATE AnimalTable SET cDescription = "cat" WHERE CodeID = 1

第二个 SQL 语句: UPDATE AnimalTable SET cDescription = "bird" WHERE CodeID = 2

这些语句有效,但我需要一种更快的方法来执行此操作,因为我有超过 20 000 个名称。

先感谢您。

I need to replace more than 20 000 names with new names i created given the CodeID.

For example: I must update all rows that contain "dog" (which has a CodeID of 1) with "cat", and update all rows that contain "horse" (which has a CodeID of 2) with "bird", etc.

1st SQL statement: UPDATE animalTable SET cDescription = "cat" WHERE CodeID = 1

2nd SQL statement: UPDATE animalTable SET cDescription = "bird" WHERE CodeID = 2

These statements work, but i need a faster way to do this because i have over 20 000 names.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深空失忆 2024-08-12 04:29:24

这是最快的方法。

或者您想在单个命令中更新所有记录?

您可以使用连接进行更新(固定语法...有一段时间没有使用过这个)

UPDATE animalTable 
INNER JOIN CodeTable ON animalTable.CodeID = CodeTable.ID 
SET animalTable.cDescription = CodeTable.Description_1;

另一种选择是将更新分成较小的批次,这将减少表被锁定的时间...但是总时间更新将花费更长的时间(这只是预期性能的改进)您可以通过仅更新每批中的某些 ID 范围来做到这一点。

您也可以将该数据放在单独的表中。由于数据未标准化。将其移开,使其更加标准化。

Thats the fastest way you can do it.

Or do you want update all records in a single command?

you can do a update with a join (Fixed Syntax... Havent used this one in a while)

UPDATE animalTable 
INNER JOIN CodeTable ON animalTable.CodeID = CodeTable.ID 
SET animalTable.cDescription = CodeTable.Description_1;

Another option is to split the updates into smaller batches, this will reduce the time the table is locked... But the total time of the updates will take longer (Its just an improvement of precieved Performance) You can do that by updating only certain ID ranges in each batch.

Also you could have that data in a separate table. Since the data is not normalized. Move it away so its more normalized.

孤独陪着我 2024-08-12 04:29:24

您可能想要创建一个临时表来保存翻译值并根据该值进行更新。

例如:

create table #TRANSLATIONS
(
    from   varchar(32),
    to     varchar(32)
)

然后,插入翻译值:

insert into #TRANSLATIONS (from,to) values ('cat','dog')

最后,根据该值进行更新:(

update MYTABLE
set    myvalue = t.to
where  myvalue = t.from
from   MYTABLE m,
       #TRANSLATIONS t

未经测试,超出我的想象)。

You might want to create a temporary table that holds the translation values and update based on that.

For example:

create table #TRANSLATIONS
(
    from   varchar(32),
    to     varchar(32)
)

Then, insert the translation values:

insert into #TRANSLATIONS (from,to) values ('cat','dog')

Finally, update based on that:

update MYTABLE
set    myvalue = t.to
where  myvalue = t.from
from   MYTABLE m,
       #TRANSLATIONS t

(Untested, off the top of my head).

街角迷惘 2024-08-12 04:29:24

您可以使用 CASE 语句来更新它:

UPDATE animaltable SET cDescription = CASE codeID WHEN 1 THEN 'cat' WHEN 2 THEN 'bird'.... END

You could use a CASE statement to update it:

UPDATE animaltable SET cDescription = CASE codeID WHEN 1 THEN 'cat' WHEN 2 THEN 'bird'.... END
风筝有风,海豚有海 2024-08-12 04:29:24

假设您有一个这样的文件:

1,cat
2,bird
...

我会编写一个脚本来读取该文件并为每一行执行更新。

在 PHP 中:

$f = fopen("file.csv","r")
while($row = fgetcsv($f, 1024)) {
    $sql = "update animalTable set cDescription = '".$row[1]."' where CodeID = ".$row[0];
    mysql_query($sql);
}
fclose($f);

Supposind you have a file like this:

1,cat
2,bird
...

I would write a script which reads that file and executes an update for each row.

In PHP:

$f = fopen("file.csv","r")
while($row = fgetcsv($f, 1024)) {
    $sql = "update animalTable set cDescription = '".$row[1]."' where CodeID = ".$row[0];
    mysql_query($sql);
}
fclose($f);
勿挽旧人 2024-08-12 04:29:24

为了加快速度,您可以做的就是仅更新那些尚未具有您想要分配的值的记录。

UPDATE animalTable SET cDescription = "cat" WHERE CodeID = 1 AND cDescription != "cat"

这种方法使得命令只更新那些还不是“cat”的记录。

免责声明:我讨厌猫。

What you could do to speed it up is to only update those records that don't already have the value you want to assign.

UPDATE animalTable SET cDescription = "cat" WHERE CodeID = 1 AND cDescription != "cat"

This approach makes the command only update those records that are not already 'cat'.

Disclaimer: I hate cats.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文