2

Like this way:

delete from `table` where id = 3;

insert into table (id, value) values (3, "aaa"), (3, "bbb"), (3, "ccc");

The count of value is hundreds, and a lot of value is the same compared with the last time, only a little records to add or delete.

I use this table to store person's property, and that property is repeated, so I insert many records in the table for one person. When update one's property, some records add or delete, and most records not changed, but when I got the new property set, I don't known which to add and which to delete. So I have to delete all the old records, and then insert the new ones, but it too slow for me, is there a faster way?

4

3 回答 3

1

我认为当每个人的记录数相对于整个表中的记录数较小时,您所做的可能是最快的方法,提高速度的唯一明显方法是在 id 列上创建非唯一索引。

如果您愿意进行一些非规范化,另一种方法可以做您想做的事情,就是将属性组合成逗号分隔的值。因此,您只需更新一行,而不是删除然后插入多行:

update table set id=3, values="aaa,bbb,ccc" where id=3;

有了这个,您将失去搜索的能力values,除非您手动维护反向索引,并且您values不能包含逗号(或您使用的任何终止字符)。使用此技术时可能有用的另一个技巧是用终止字符包围值:

update table set id=3, values=",aaa,bbb,ccc," where id=3;

这样,您仍然可以values通过用终止字符包围搜索词来进行全文搜索:select * from table where ",aaa," in values

此外,您失去了指定唯一约束的能力,因此您必须确保应用程序逻辑中的值没有重复条目。

于 2013-01-09T04:15:53.610 回答
0

一旦你用正确的行设置了一个人,你可以像这样将它们复制到另一个人,例如从 id 1 复制到 2

insert into table (id, value)
select 2, value
from table
where id = 1;
于 2013-01-09T03:48:06.870 回答
0

您是否打算使用主键更新多个表?那么你可以看看这个这个

于 2013-01-09T02:53:41.217 回答