mysql连接 为什么不能复用_Mysql是不是不能复用执行计划 - 墨天轮问答

MySQL prepare语句的SQL语法

Prepare在execute阶段可以节省硬解析的时间。

如果sql只执行一次,且以prepare的方式执行,那么sql执行需两次与服务器交互(Prepare和execute),

而以普通(非prepare)方式,只需要一次交互。

这样使用prepare带来额外的网络开销,可能得不偿失。

如果是同一sql执行多次的情况,比如以prepare方式执行10次,那么只需要一次硬解析。

这时候额外的网络开销就显得微乎其微了。因此prepare适用于频繁执行的SQL。

也可以参考官方文档

prepare

prepared statement也是伴随会话的,会话消失,prepared statement效果也没有了

The scope of a prepared statement is the session within which it is created, which as several implications:

A prepared statement created in one session is not available to other sessions.

When a session ends, whether normally or abnormally, its prepared statements no longer exist. If auto-reconnect is enabled, the client is not notified that the connection was lost. For this reason, clients may wish to disable auto-reconnect. See Section 27.7.19, “C API Automatic Reconnection Control”.

A prepared statement created within a stored program continues to exist after the program finishes executing and can be executed outside the program later.

A statement prepared in stored program context cannot refer to stored procedure or function parameters or local variables because they go out of scope when the program ends and would be unavailable were the statement to be executed later outside the program. As a workaround, refer instead to user-defined variables, which also have session scope; see Section 9.4, “User-Defined Variables”.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值