准备好的查询“持续”多久? 在服务器端?

发布于 2024-07-27 16:48:24 字数 189 浏览 1 评论 0原文

我只是想知道准备好的查询是如何工作的。 我正在使用 PHP 和 MySQL。

我知道准备好的查询会编译查询,然后重复使用只需更改参数,因此节省了时间。 但是编译后的查询会产生多长时间的效果呢? 什么时候必须重新评估? 只要PHP脚本运行就可以吗? 或者只要存在与数据库的连接就可以吗? 那么,持久连接会对它有什么影响吗?

I was just wondering how prepared queries work. I am using PHP and MySQL.

I know that prepared queries compile the query and then repeated uses only have to change the parameters, so it saves time. But how long does that compiled query have an effect? At which point does it have to be reevaluated? Is it just as long as the PHP script is running? Or is it as long as the connection to the database is present? In that case, would a persistent connection have any effect on it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌︼了一个春 2024-08-03 16:48:24

ryeguy,它们会持续整个连接时间。 根据 MySQL 手册:

准备好的语句特定于创建它的会话。 如果您终止会话而不释放先前准备的语句,服务器会自动释放它。

如果您没有使用持久连接,那么当您的脚本完成执行时(或者您显式地释放它,或关闭连接),这将被释放。 如果使用持久连接,那么它将在使用相同持久连接的多个 PHP 会话中持久存在。

ryeguy, they last for the length of the connection. Per the MySQL manual:

A prepared statement is specific to the session in which it was created. If you terminate a session without deallocating a previously prepared statement, the server deallocates it automatically.

If you are not using persistent connections, then this will be deallocated when your script finishes executing (or you explicitly deallocate it, or close the connection). If using persistent connections, then it will persist across multiple PHP sessions using the same persistent connection.

初心未许 2024-08-03 16:48:24

据我所知,只要存储它的变量在范围内,准备好的查询就会“持续”。 我想可能有一些方法可以缓存准备好的查询以供以后使用,但我不知道 MySQL 是否这样做。

As far as I know, a prepared query will only "last" for as long as the variable storing it is within scope. I suppose there could be some ways to cache prepared queries for later use, but I don't know if MySQL does this.

酒中人 2024-08-03 16:48:24

hobodave 是对的,它们的持续时间与创建它们的会话一样长。

还有另一件事需要考虑:

7.5.5。 MySQL 查询缓存

注意

查询缓存不用于
准备好的陈述。 如果您正在使用
准备好的陈述,考虑到
这些陈述将不会得到满足
通过查询缓存。

hobodave is right, they just last as long as the session they were created in.

There's one other thing to consider, too:

7.5.5. The MySQL Query Cache

Note

The query cache is not used for
prepared statements. If you are using
prepared statements, consider that
these statements will not be satisfied
by the query cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文