PHP 使用 Mysqli 的 prepare 語句有什麼好處?

來源:互聯網
上載者:User

回複內容:

《High Performance MySQL》 一書的 225 -228 頁。講的非常詳細,我簡單整理下:

好處:
(1) Parse the query only once
(2) Perform some query optimization steps only once
(3) Sending parameters via the binary protocol is more efficient than sending them as ASCII text
比如 DATE,對於 Prepare 之後,發送 Date 只用 3 Bytes;如果沒有 Prepare, DATE 必須以 String 的形式發送,需要資料庫方再解析,這樣需要發送 10 Bytes。
(4) Only the parameters (not the entire query text) need to be sent for each execution
(5) MySQL stores the parameteres directly into buffers on the server
(6) Also helps with security, there is no need to escape or quote values.

壞處:
(1) Local to a connection, so another connection cannot re-use
(2) Cannot use MySQL query cache (5.1 版本之前)
(3) Not always more efficient, if you use it only once
(4) Cannot use inside a stored function (Stored procedure 是可以的)
(5) May lead to "leak" if you forget to deallocate it

基本上直接打原文,部分地方稍微做了下改變,以及適當的地方加了補充。1:安全方面,sql注入這類問題
2:傳輸體積最佳化[參數傳遞]
3:避免重複解析,mysql是非持繼串連函數而mysqli是永遠串連函數。

也就是說mysql每次連結都會開啟一個串連的進程而mysqli多次運行mysqli將使用同一串連進程,從而減少了伺服器的開銷
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.