记录一下
sql优化,在业务逻辑不变的情况下,主要就是减少io,减少查询次数
添加6000条数据
使用for循环,一次添加一条,sql如下
String sql = "insert into user_tree select ?, ?, name, parent_id, previous_id, carNum, type from "
+ "user_tree where user_id = ? and alias_id = ?";
this.hibernate.bulkUpdateSQL(sql, new Object[]{new_uid, alias_id, old_uid, alias_id});
耗时大概为30s, 效率较低
如果降低查询次数,是否会提高效率,改用下面的方法
String sql = "insert into user_tree select ?, alias_id, name, parent_id, previous_id, carNum, type from "
+ "user_tree where user_id = ? and alias_id in(?,?,?)";
this.hibernate.bulkUpdateSQL(sql, new Object[]{new_uid, old_uid, alias_id1,alias_id2,alias_id3});
结果,效率更低先把可能需要的数据都先查出来,再在内存中生成数据,使用hibernate插入数据
select ut.* from user u where u.user_id = ? and alias is not null
this.hibernate.saveall();
耗时约为6s