什么时候使用报表而不是准备好的报表?[英] when to use Statement over Prepared Statement?

本文是小编为大家收集整理的关于什么时候使用报表而不是准备好的报表?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

何时使用语句代替准备的语句.我想语句在没有参数的查询中使用,但是为什么不使用准备好的语句?哪一个对于没有参数的查询更快.

推荐答案

我想语句在没有参数的查询中使用,但为什么不使用准备的语句?

那甚至还没有关闭.在插入,更新和删除返回结果集或更新计数的插入,更新和删除语句的情况下,使用了准备施工.它们将不适为Joachim指出的DDL语句工作,并且它们也不适用于应使用CallableStatement的存储过程的调用(这不是两类之间的区别).就没有绑定参数的查询而言,准备序列可能比语句更好(见下文).

哪一个更快地用于无参数的查询.

从长远来看,

准备的序列将会更快,而在单个连接中扩展使用.这是因为,尽管必须编译准备的准备序列,但这需要一些时间(这确实不是很多,所以不要将其视为缺点),但编译版本本质上是对SQL执行计划的引用数据库.编译后,准备立体将存储在特定于连接的缓存中,以便可以重复使用编译版本以实现性能提高.如果您使用的是JDBC批处理操作,则使用准备施加将使批量执行速度要比使用普通语句对象快得多,如果数据库必须这样做,则可能会一次又一次地准备计划.

其他推荐答案

这取决于您的要求.

如果您的SQL语句以循环或经常使用不同的参数运行,则准备序列是最好的候选者,因为它正在预先编译并缓存此参数化SQL查询的执行计划.每次从同一准备的对象运行时,都会使用缓存的执行计划并提供更好的性能.

也可以使用准备序列避免使用SQL注入.

但是,如果您确定仅运行SQL查询一次,有时语句将是最好的候选人,因为当您创建准备准备的对象时,有时会产生其他DB调用,如果驱动程序支持预编译,则方法将向数据库发送语句以进行预编译.

阅读下面的文章以了解"语句与准备策略"

本文地址:https://www.itbaoku.cn/post/597760.html

问题描述

When to use statement instead of prepared statement. i suppose statement is used in queries with no parameter but why not use prepared statement ? Which one is faster for queries with no params.

推荐答案

I suppose statement is used in queries with no parameter but why not use prepared statement ?

That's not even close. PreparedStatements are used in the case of INSERT, UPDATE and DELETE statements that return a ResultSet or an update count. They will not work for DDL statements as pointed out by Joachim, and neither will they work for invocation of stored procedures where a CallableStatement ought to be used (this is not a difference between the two classes). As far as queries with no bind parameters are concerned, PreparedStatements can turn out to be better than Statements (see below).

Which one is faster for queries with no params.

PreparedStatements will turn out to be faster in the long run, over extended use in a single connection. This is because, although PreparedStatements have to be compiled, which would take some time (this really isn't a lot, so don't see this as a drawback), the compiled version essentially holds a reference to the SQL execution plan in the database. Once compiled, the PreparedStatement is stored in a connection specific cache, so that the compiled version may be reused to achieve performance gains. If you are using JDBC Batch operations, using PreparedStatements will make the execution of the batch much faster than the use of plain Statement objects, where the plan may have to be prepared time and again, if the database has to do so.

其他推荐答案

That's depending on Your requirement.

If you have a SQL statement which runs in a loop or frequently with different parameters then PreparedStatement is the best candidate since it is getting pre-compiled and cache the execution plan for this parameterized SQL query. Each time it runs from the same PreparedStatement object it will use cached execution plan and gives the better performance.

Also SQL injection can be avoided using PreparedStatement .

But if you are sure that you run SQL query only once, sometimes Statement will be the best candidate since when you create PreparedStatement object sometimes it make additional db call, if the driver supports precompilation, the method Connection.prepareStatement(java.lang.String) will send the statement to the database for precompilation.

Read below article to understand "Statement Versus PreparedStatement"