問題描述
我正在開發一個非常受 MySQL 數據庫驅動的網站.所以我有很多疑問.
I am working on a website which is very MySQL DB driven. So I have a lot of queries going on.
在 這個話題大家建議在頁面頂部連接DB,在頁面底部斷開連接.
In this topic everyone recommends to connect to the DB at the top of the page, and disconnect at the bottom of the page.
我想知道什么更有效,或者一般來說是最佳實踐:每頁建立一個數據庫連接,還是只在需要時連接?(或者沒有一般的答案,這取決于?)
I am wondering what's more efficient, or generally speaking best practice: Make a single db connection per page, or only connect as needed? (Or is there no general answer, and it depends?)
此外,我希望找出為什么這是最佳實踐,您是從哪個角度來看場景的(例如安全性、速度……我不知道還有什么數據庫連接可能會影響?!)
Additionally I am looking to find out WHY is this best practice, from which point of view are you looking at the scenario (e.g. security, speed, ... I don't know what else DB connections might affect?!)
我相信在 這里之前已經問過這個問題 - 但不是特別針對 PHP,因此我覺得它沒有幫助.
I believe this question has been asked before here - but not for PHP in specific, and therefore I didn't find it helpful.
我目前的做法是為我編寫的每個函數連接到每個 mysqli 的數據庫,并在函數結束時斷開連接,因為對我來說它看起來更干凈.這樣,如果頁面不調用需要訪問數據庫的函數,則永遠不會打開連接.但是,可能會發生每個頁面加載最多大約 10 個連接的情況,具體取決于用戶在站點上執行的操作.現在我認為這可能是資源的公平分配.如果我理解正確,則只能始終打開 1 個 DB 連接.因此,我假設所有連接請求都將排隊.因此,如果用戶有多個、長且復雜的查詢,則該用戶不會阻止所有流量,因為在每個查詢之間,可能會處理其他短查詢.但這只是我編造的東西,我不知道它是否真的會這樣......:D
My current practice has been to connect to the DB per mysqli for each function I write, and disconnect at the end of the function, because it seemed cleaner to me. This way, if a page doesn't call to a function which requires DB access, there will never be a connection opened. However it may happen, that there might be up to approximately 10 connections per page load, depending on what the user does on the site. Now I thought this might be a fair distribution of resources. If I understood it correctly there can only always be 1 DB connection opened. Therefore I assume all connection requests will be queued. So if a user has multiple, long and complicated queries, this user would not hold up all traffic, because in between each of the queries, other short queries could get processed. But that's just me making stuff up, I don't know if it would really work that way... :D
我也知道這里的很多開發人員都喜歡使用 PDO.剛開始開發的時候選擇了mysqli,暫時沒有換的打算.我希望我的問題適用于兩個圖書館.
Also I know that a lot of developers around here like to use PDO. I chose to use mysqli when I started developing, and I have no plans of switching. I hope my question can be applicable to both libraries.
謝謝:-)
推薦答案
通常,創建數據庫連接的成本很高.這就是為什么大多數人建議創建一次連接并重復使用它直到執行停止,如果數據庫客戶端庫允許,甚至更長時間.
Typically database connections are expensive to create. This is why most people recommend creating the connection once and reuse it until the execution has stopped, or even longer if the database client library allows it.
例如,PDO 允許創建持久連接,據說這可以提高性能,因為該連接將被重復用于連續處理多個請求.來自 http://php.net/manual/en/pdo.connections.php:
As an example, PDO permits creating persistent connections, which supposedly enhance performance because the connection would be reused for serving several requests in a row. From http://php.net/manual/en/pdo.connections.php:
許多 Web 應用程序將受益于與數據庫服務器的持久連接.持久連接不會在腳本結束時關閉,而是在另一個腳本使用相同憑據請求連接時被緩存和重新使用.持久連接緩存允許您避免每次腳本需要與數據庫通信時建立新連接的開銷,從而使 Web 應用程序更快.
Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.
這篇關于什么更有效,為什么:每頁一個數據庫連接或每個函數一個數據庫連接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!