SQL中有没有快速更新多条记录的方法?
我需要用我根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是最快的方法。
或者您想在单个命令中更新所有记录?
您可以使用连接进行更新(固定语法...有一段时间没有使用过这个)
另一种选择是将更新分成较小的批次,这将减少表被锁定的时间...但是总时间更新将花费更长的时间(这只是预期性能的改进)您可以通过仅更新每批中的某些 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)
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.
您可能想要创建一个临时表来保存翻译值并根据该值进行更新。
例如:
然后,插入翻译值:
最后,根据该值进行更新:(
未经测试,超出我的想象)。
You might want to create a temporary table that holds the translation values and update based on that.
For example:
Then, insert the translation values:
Finally, update based on that:
(Untested, off the top of my head).
您可以使用
CASE
语句来更新它:You could use a
CASE
statement to update it:假设您有一个这样的文件:
我会编写一个脚本来读取该文件并为每一行执行更新。
在 PHP 中:
Supposind you have a file like this:
I would write a script which reads that file and executes an update for each row.
In PHP:
为了加快速度,您可以做的就是仅更新那些尚未具有您想要分配的值的记录。
这种方法使得命令只更新那些还不是“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.
This approach makes the command only update those records that are not already 'cat'.
Disclaimer: I hate cats.