什么时候使用语句准备语句? [英] when to use Statement over Prepared Statement?

查看:170
本文介绍了什么时候使用语句准备语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时使用语句而不是准备语句。我认为语句是用于查询没有参数,但为什么不使用预备语句?

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 ?

PreparedStatements用于返回ResultSet或更新计数的INSERT,UPDATE和DELETE语句。它们不适用于Joachim指出的DDL语句,它们也不能用于调用应该使用CallableStatement的存储过程(这不是两个类之间的区别)。对于没有绑定参数的查询,PreparedStatements可能会比语句更好(见下文)。

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从长远来看,这是因为,虽然PreparedStatements必须被编译,这将需要一些时间(这真的不是很多,所以不认为这是一个缺点),编译版本本质上持有对SQL执行计划的引用数据库。一旦编译,PreparedStatement存储在连接专用缓存中,以便可以重新使用编译版本以实现性能增益。如果使用JDBC Batch操作,则使用PreparedStatements将使批处理的执行比使用纯Statement对象更快,如果数据库必须这样做,那么可能必须一次又一次准备计划。

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.

这篇关于什么时候使用语句准备语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆