我假设这是(批量删除):
for(int i=0;i<10;i++){
updateToStatus1(i, 1); //update table set status = 1 where id = :i
}
deleteAllStatus1();//delete from table where status = 1
比这更快(逐一删除):
for(int i=0;i<10;i++){
delete(i); //delete from table where id = i
}
无论我在“表”上创建什么索引,我的假设都是正确的吗?我认为逐一删除时会涉及很多锁定。
此外,如果有人可以向我推荐有关此类 SQL 权衡的好 Material ,我将非常感激。
最佳答案
I'm assuming that this (batch deleting):
for(int i=0;i<10;i++){
updateToStatus1(i, 1); //update table set status = 1 where id = :i
}
deleteAllStatus1();//delete from table where status = 1
比这更快(逐一删除):
for(int i=0;i<10;i++){
delete(i); //delete from table where id = i
}
我只能假设第一个 T1 > T2
所花费的时间(第二个所花费的时间)
- 在loop1中update(通过id查找并更新staus)是一个写操作,类似于loop2中的删除(通过id查找并删除行)
- 如果您对
status
建立索引,则与之前相比,更新的成本将变得更高(因为它会更改索引键的值)。 - 索引状态也会对基于状态的删除产生不利影响。
I'm thinking there would be a lot of locking involved when deleting one-by-one.
- 逐一更新也是如此
所以您的时间比较是:
一一更新+全部删除
vs 一一删除
对相同数据的单个事务提交比在多个事务中提交花费的时间要少。但这里存在更新开销。
Also, if anyone can recommend me good regarding materials regarding such SQL trade-offs
jdbc提示:
http://shop.oreilly.com/product/9780596000882.do
对于索引(尽管对于 DB2 仍然有用):Indexing
我还建议您为您正在使用的数据库搜索 SQL 查询和数据导入/导出的性能调整。这些通常可以在引用手册中找到。例如:
MySQL sql optimization
InnoDB performance tuning
关于java - 逐个删除文件与一次删除同一组文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820030/