当我说准备好的声明是预编译的时候意味着什么? [英] What does it mean when I say Prepared statement is pre-compiled?

查看:147
本文介绍了当我说准备好的声明是预编译的时候意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 中使用 MySQL 。我对 PreparedStatement 没有很好的理解。

我知道最好使用 PreparedStatement Statement 。原因是它被编译。

I am using MySQL in Java. I don't have a good understanding of PreparedStatement.
I know it is better to use PreparedStatement than Statement. The reason being it is compiled.

编译后我们的意思是什么?

What do we mean by compiled?

推荐答案

当你使用时准备好的语句(即预编译语句),一旦DB获得此语句,它就会编译它并对其进行缓存,以便它可以使用最后编译的语句来连续调用同一语句。所以它成为连续调用的预编译。

When you use prepared statement(i.e pre-compiled statement), As soon as DB gets this statement, it compiles it and caches it so that it can use the last compiled statement for successive call of same statement. So it becomes pre-compiled for successive calls.

您通常使用带有绑定变量的预准备语句,您可以在运行时提供变量。现在,对于连续执行预准备语句会发生什么,您可以提供与先前调用不同的变量。从DB的角度来看,它不必每次都编译语句,只会在朗姆酒时插入绑定变量。因此变得更快。

You generally use prepared statement with bind variables where you provide the variables at run time. Now what happens for successive execution of prepared statements, you can provide the variables which are different from previous calls. From DB point of view, it does not have to compile the statement every time, will just insert the bind variables at rum time. So becomes faster.

预准备语句的其他优点是: -

Other advantages of prepared statements are :-

1)防止SQL注入攻击

1)protection against SQL-injection attack

2)连续调用相同语句的速度更快

2) Faster for successive calls of same statements

工作原理: -


  1. 预编译由数据库完成。一些更简单的数据库根本不预编译语句。其他人可能会在prepareStatement调用上预先编译它,而其他人可能会在首次调用语句时执行此操作,在编译(创建计划)语句时将参数值考虑在内。

  1. Precompilation is done by the database. Some simpler databases don't precompile statements at all. Others might precompile it on the prepareStatement call, and yet others might do it when execute is first called on the statement, taking values of the parameters into account when compiling (creating a plan for) the statement.

执行预编译语句的数据库通常会缓存它们,所以很有可能ps1不会再次编译。一些JDBC驱动程序(例如Oracle)甚至缓存预处理语句,因此在调用ps.close()时它们实际上并没有关闭它。

Databases that do precompile statements usually cache them, so in all probability ps1 won't be compiled again. Some JDBC drivers (eg. Oracle's) even cache prepared statements, so they haven't actually closed it when ps.close() was called.

数据库通常会缓存语句,直到某些内容从缓存中逐出。

Databases generally cache statements until something evicts them from the cache.

详细信息请参阅 wiki 链接

这篇关于当我说准备好的声明是预编译的时候意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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