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 ...

解决方案 »

  1.   

    感谢 1楼的回复 能告诉下为什么没区别吗 总感觉delete/insert需要更多的磁盘读写呢  难道是由于两次写 写磁盘的延迟  使得没了影响?
      

  2.   

    1. 如果未使用独立空间的 innodb 引擎的话,先删再插是没有必要的,因为innodb mysql的删除其实只是对数据进行一个“标记”,表示这条删除的数据是无用的,之后如果有新数据需要插入,会找到适合大小的标记为无效的空间插入
    2. 看你根据主键来判断是更新还是插入,建议直接使用 replace into 来处理吧,数据库自动支持的功能内部实现应该效率更高些
      

  3.   

    至于哪个快,则需要测试一下然后总结了。至于如何设计测试方法,则可以参考下贴。http://blog.csdn.net/acmain_chm/article/details/4192311
    ACCESS的真假:一、DROP删除表再重建比Delete from Table1快吗?http://blog.csdn.net/acmain_chm/article/details/4210633
    ACCESS的真假:二、检查记录有无再insert 或 update 比 不管有无直接 delete 再 insert 快吗?
      

  4.   

    晕 忘了给nicenight 分