donglankui1263 2018-01-28 17:29
浏览 139
已采纳

为什么模拟预处理语句不与数据库服务器通信?

I am learning to PHP PDO and confused about the emulated prepared statements. What are emulated prepared statements and native prepared statements.

The reference link says that:

Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.

Reference Link: Resource

  • 写回答

2条回答 默认 最新

  • dqluw20882 2018-01-28 19:36
    关注

    The statement "does not communicate with the server" may not be clear. It would be more clear to say "does not communicate with the server when you call prepare()."

    Normally during a prepare & execute sequence, the client sends the SQL as a string to the server when you call prepare(). The server parses the SQL syntax, and returns an error if the SQL syntax has mistakes, or references tables that don't exist, or other errors. If the query is valid, it saves the query on the server. If you had parameter placeholders like ? in your SQL string, these are noted by the server.

    Subsequently, you call execute(), and at that time pass any values to replace the placeholders. This might not involve string-replacement, because the query is no longer stored as a string after it has been parsed and analyzed. Each element of the query is stored as an object, including constant values and parameter placeholders.

    With emulated prepares, PDO skips sending the SQL string to the server during prepare(). It just saves the SQL string on the client-side, in memory in PDO code. This also skips checking the SQL query for errors, because the PDO client does not include a SQL parser or any knowledge of what tables exist, etc.

    When you execute() a query that was saved as an emulated-prepared query, PDO does string-replacement on the saved SQL string, then it submits the final SQL string to the server. This is the first time the server has seen that query, so it parses the syntax and optimizes the query at that time.

    There are at least three possible reasons for emulated prepares:

    1. To allow PDO to support prepare()/execute() for some brands of SQL database that don't natively support that usage, or don't support query parameters.

      But I can't think of any commonly-used brand of SQL database that lacks support for query parameters.

    2. To allow PDO to support both positional parameters (?) and named parameters (:user_id) for any brand of SQL database. Natively, MySQL supports only positional parameters. Oracle supports only named parameters. SQLite supports both styles.

      But PDO did find a way to do a mapping from one type of parameter to the other before prepare(), so you use named parameters with MySQL, even while using non-emulated prepares. PDO can rewrite the query string before sending it via prepare(), converting named parameters to positional parameters, and remembering which position corresponds to which name, so when you call execute() it is sure to send the values in the right order.

    3. PDO 0.1 alpha dates back to 2004. At that time, some people might have been worried that network overhead was too high if you had to communicate with the database server twice for every query. So using emulated prepared statements might have been created for the sake of reducing network trips.

      But if your app is that sensitive to performance, you should run the app and the database servers close to each other on a fast network, 1Gbps or 10Gbps. Any modern data center that is serious about performance should be using these fast networks internally.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?