我有这段代码,非常标准的东西。它采用 java 对象中的一些过滤器,并返回与过滤器匹配的所有记录:
class MarkFilters {
Integer idFilter;
Integer userIDFilter;
Integer claimIDFilter;
}
List<Integer> findMarksMatchingFilters(MarkFilters filters) {
String sql = "select id from Mark where 1";
// This has the values to substitute in for the ?'s in the SQL
List<Object> parameters = new LinkedList<Object>();
if (filters.idFilter != null) {
sql += " and id = ?";
parameters.add(filters.idFilter);
}
if (filters.userIDFilter != null) {
sql += " and userID = ?";
parameters.add(filters.userIDFilter);
}
if (filters.claimIDFilter != null) {
sql += " and claimID = ?";
parameters.add(filters.claimIDFilter);
}
return executeSQLWithParameters(sql, parameters);
}
我找到了一种应该做同样事情的方法:
List<Integer> findMarksMatchingFilters(MarkFilters filters) {
return executeSQLWithParameters(
"select * from Mark " +
"where (? = NULL or id = ?) " +
"and (? = NULL or userID = ?) " +
"and (? = NULL or claimID = ?) ",
filters.idFilter, filters.idFilter,
filters.userIDFilter, filters.userIDFilter,
filters.claimIDFilter, filters.claimIDFilter);
}
例如,如果我输入一个新的 MarkFilters(null, null, 3),则 sql 语句将是:
select * from Mark
where (NULL = NULL or id = NULL)
and (NULL = NULL or userID = NULL)
and (3 = NULL or claimID = 3)
理论上将优化为:
select * from Mark where claimID = 3;
我的具体问题:这种优化真的会发生吗?
我更广泛的问题:这是一个很好的模式,还是有我看不到的缺点?
谢谢!