简体   繁体   English

批量更新与IN(x,x,x)

[英]Batch update versus IN (x,x,x)

I need to create an update of a field to a list of ids. 我需要创建一个字段更新到ID列表。 This list is variable in size. 此列表的大小可变。

What I'm wondering is, what of the next two possible options is better: 我想知道的是,接下来的两个可能的选择中哪个更好?

Option 1: Batch Update: 选项1:批量更新:

PreparedStatement update = connection.prepareStatement(" UPDATE table set field = value where id = ?");
for (id : ids){
    update.setInt(id);
    update.addBatch();
}
update.executeBatch();

Option 2: IN (x,x,x) 选项2:IN(x,x,x)

PreparedStatement list_update = connection.prepareStatement( "UPDATE table set field = value where id in ( " + comma_separated_ids(ids) + ")" );

private String comma_separated_ids(int[] ids){
     // receives [1,2,3] and returns "1,2,3"
}

I'm more inclined towards the second, but I don't like the where id in ( " + comma_separated_ids(ids) + ")" ); because of possible SQL Injection. 我更倾向于第二个,但由于可能的SQL注入,我不喜欢where id in ( " + comma_separated_ids(ids) + ")" );where id in ( " + comma_separated_ids(ids) + ")" );

So, what option is better? 那么,哪种选择更好呢? Number 1 or number 2. If number 2 were the case, how could I avoid SQL Injection? 1号或2号。如果是2号,如何避免SQL注入?

Option 1 is quite more elegant, but with Option 2 you will have better performance (even more if the number of parameters turns big at some point). 选项1相当优雅,但是使用选项2时,您将具有更好的性能(如果参数数量在某个时候变大,则性能会更高)。 Just add as many ? 只需添加多少? as IN parameters needed...then loop again and set them. 由于需要IN参数...然后再次循环并进行设置。 Do not set the values as-is in the IN clause, use the parametrized fashion (the thing with ? ). 不要在IN子句中按原样设置值,请使用参数化方式 (带有?的东西)。

Even having two loops that will be (way!) faster than executing the same number of queries against the database. 甚至有两个循环比对数据库执行相同数量的查询要快得多。 Moreover, although it is true that if you are using a PreparedStatement you will be executing all queries in the batch in one-go, the database server will executing as many UPDATE queries as number of batch statements you built. 而且,尽管确实如此,但是如果您正在使用PreparedStatement,则将一次执行批处理中的所有查询,但是数据库服务器将执行与您构建的批处理语句数量一样多的UPDATE查询。 Another point is transaction , it's up to you if you decide all or nothing , but that will add some more time on top of that anyway. 另一个要点是交易 ,由您决定是全部还是不决定,但这总会增加一些时间。 You can always measure both solutions. 您始终可以衡量两个解决方案。

NOTE: I've seen many answers here in StackOverflow where they state that several ? 注意:我在StackOverflow中看到了很多答案,其中指出了几个? doesn't works as parameters on IN clauses...that's not true, at least for: DB2, Oracle and MySQL using JDBC 4.0. 不能用作IN子句上的参数...至少在以下情况下是不正确的:使用JDBC 4.0的DB2,Oracle和MySQL。 It does works. 它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM