简体   繁体   English

我是否应该在重复查询中使用prepareStatement,其中where子句谓词经常更改,从而导致所选计划的更改

[英]Should I use preparedStatement in a repetitive query in which where clause predicates change often causing change of plan chosen

I have a Java application which is executing queries on PostgreSQL 9.3 Server using JDBC. 我有一个Java应用程序,该应用程序正在使用JDBC在PostgreSQL 9.3 Server上执行查询。 In my java application, I had to execute same query many times(in thousands) with different arguments in 'where' clause predicates alone. 在我的Java应用程序中,我必须单独使用“ where”子句谓词中的不同参数多次(数千次)执行相同的查询。 I have been using Statement class till now. 到目前为止,我一直在使用Statement类。 I recently read about PreparedStatement class somewhere and I am thinking should I use it to speedup processing. 我最近在某个地方读到了PreparedStatement类,我在想应该使用它来加快处理速度。 But my doubt is this. 但是我的疑问是这样。 Since my query executes each time with different values in Where clause predicates, the selectivity will change and hence plan chosen by the db server will change. 由于我的查询每次在Where子句谓词中使用不同的值执行,因此选择性将发生变化 ,因此数据库服务器选择的计划将发生变化。 In that case, will using PreparedStatement speedup the processing? 在那种情况下,使用PreparedStatement可以加快处理速度吗? Is the plan chosen when Preparedstatement is created or plan is chosen only when execute is called on the preparedstatement object? 创建Preparedstatement时是选择计划还是仅当在preparestatement对象上调用execute时才选择计划 If plan is chosen when preparedstatement is created itself, how is it done since optimizer chooses plans based on selectivity calculated using actual predicate values. 如果在创建preparedstatement本身时选择了计划,那么由于优化程序会根据使用实际谓词值计算出的选择性来选择计划,因此如何进行计划。

My Query is a complex one involving many tables. 我的查询是一个涉及许多表的复杂查询。 Template is like, select something from tables where predicate1 and predicate2 and price < X and date < Y; 模板就像从表中选择谓词1和谓词2以及价格<X和日期<Y; where X and Y varies for each query . 其中X和Y对于每个查询都不同

From PostgreSQL doc : PostgreSQL doc

PREPARE creates a prepared statement. PREPARE创建一个准备好的语句。 A prepared statement is a server-side object that can be used to optimize performance. 准备好的语句是可用于优化性能的服务器端对象。 When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. 当执行PREPARE语句时,将解析,分析和重写指定的语句。 When an EXECUTE command is subsequently issued, the prepared statement is planned and executed. 随后发出EXECUTE命令时,将计划并执行准备好的语句。 This division of labor avoids repetitive parse analysis work, while allowing the execution plan to depend on the specific parameter values supplied. 这种分工避免了重复的解析分析工作,同时允许执行计划取决于所提供的特定参数值。

moe was right : preparing a query will only remove the overhead of reparsing it again and again. 萌对了:准备查询只会消除一次又一次地重新解析的开销。 The planing is done only when you will execute the prepared query with its parameters. 仅当您将使用其参数执行准备好的查询时,才进行计划。

In 9.3, it uses a heuristic. 在9.3版中,它使用了一种启发式方法。 It does something like planning the query with the specific bind values the first 5 times the prepared statement is executed. 它的作用类似于执行准备好的语句的前5次使用特定的绑定值计划查询。 If none of those plans turn out to be substantially better than the generic plan, then it stops the individual planning and justs uses the generic plan from then on. 如果这些计划中没有一个比通用计划要好得多,那么它将停止单个计划,并从那时开始使用通用计划。

But there is another wrinkle in that just because your code told the driver to use a prepared statement doesn't mean driver is actually doing so. 但是还有一个折衷,那就是仅仅因为您的代码告诉驱动程序使用准备好的语句并不意味着驱动程序实际上正在这样做。 A lot of drivers do weird things. 许多司机做奇怪的事情。

The real answer is test, test, test. 真正的答案是测试,测试,测试。

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

相关问题 如何在PreparedStatement中更改from子句的表名 - How to change table name of from clause in PreparedStatement 我应该首先关闭哪个,PreparedStatement还是Connection? - Which should I close first, the PreparedStatement or the Connection? 在Where子句中,PreparedStatement“为null”,没有条件(动态查询)或乱码 - PreparedStatement “is null” in Where clause without if conditional (dynamic query) or gibberish values 我应该更改此 JTextArea 的哪个参数? - Which parameter of this JTextArea should I change? Criteria query combine and predicates and or 谓词在 where 方法中 - Criteria query combine and predicates and or predicates in where method 我应该选择哪个DBMS来使用Java重新排序查询执行计划? - Which DBMS should i choose for reordering query execution plan using Java? 使用PreparedStatement构建查询 - Use PreparedStatement to build a query 我应该对Oracle JDBC数据库更改通知使用哪个缓存提供程序 - Which Cache provider should I use for Oracle JDBC Database change notifications 如何在参数化 SQL 查询中使用多个 WHERE 子句? - How do I use multiple WHERE clause in parameterized SQL query? 带有 Java 准备语句的 Postgres INSERT INTO where 子句 - Postgres INSERT INTO where clause with Java preparedstatement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM