innodb的表,记录会过百万或千万,表上有索引,需要对表进行频繁的更新 怎么做效率高?
create table t_user
(
kid bigint not null auto_increment,
user_id bigint not null,
user_xx int not null,
user_yy int not null,
isvalid int not null,
primary key(kid)
)engine = innodb;
create index index_user on t_user(user_id);每个user_id 对应 几十条记录 主键不一定连续 每次更新的记录数和更新的字段都有变化下面两个效率有区别吗 本来我以为直接更新的会效率高 觉得没有索引的重建,但用十万左右的记录 更新 随机和连续的 几千条记录 感觉没啥大的差别 这是为啥。。1. 先删再插
delete from t_user where user_id = xxx;
insert into t_user() values();2.先置为无效再更新
update set isvalid = 0 where user_id = xxx;
存储过程
procedure
select user_id to temp_id from t_user where user_id = xxx;
if temp_id = xxx then
update ....
else
insert ...
create table t_user
(
kid bigint not null auto_increment,
user_id bigint not null,
user_xx int not null,
user_yy int not null,
isvalid int not null,
primary key(kid)
)engine = innodb;
create index index_user on t_user(user_id);每个user_id 对应 几十条记录 主键不一定连续 每次更新的记录数和更新的字段都有变化下面两个效率有区别吗 本来我以为直接更新的会效率高 觉得没有索引的重建,但用十万左右的记录 更新 随机和连续的 几千条记录 感觉没啥大的差别 这是为啥。。1. 先删再插
delete from t_user where user_id = xxx;
insert into t_user() values();2.先置为无效再更新
update set isvalid = 0 where user_id = xxx;
存储过程
procedure
select user_id to temp_id from t_user where user_id = xxx;
if temp_id = xxx then
update ....
else
insert ...
2. 看你根据主键来判断是更新还是插入,建议直接使用 replace into 来处理吧,数据库自动支持的功能内部实现应该效率更高些
ACCESS的真假:一、DROP删除表再重建比Delete from Table1快吗?http://blog.csdn.net/acmain_chm/article/details/4210633
ACCESS的真假:二、检查记录有无再insert 或 update 比 不管有无直接 delete 再 insert 快吗?