search

php的pconnect

Jun 13, 2016 am 10:26 AM
mysqlphpexisthaveof


PHP在連結MYSQL時有兩個FUNCTIONCONNECT與PCONNECT這兩個函式的差別在哪裡?一般的標準答案都是:PCONNECT是持續性的連接,PHP會重複使用已經存在的資料庫連線
但是再追問下去,能說得出來的人就少了許多常見的疑問有:什麼時候要用CONNECT什麼時候用PCONNECT?mysql_close何時使用?為什麼DATABASE上有一堆SLEEP的PROCESS?明明用PCONNECT為何每次RESOURCE ID都不同?
先回歸到最簡單的解釋,pconnect會去檢查是否有已經存在的資料庫連線。若有,就使用舊的連線;若無,就新開啟一個連線但是這裡所說得檢查連線,是指這一個APACHE行程所開啟的資料庫連線而不是這一台WEB SERVER所開啟的資料庫連線。一台WEB SERVER上可能有上百個APAHE行程(2.0預設最大150個)也就是說 在最大情況下可能會有上百個PCONNECT造成的資料庫連線(實際上會遠少於此)所以同樣使用PCONNECT是會出現不同RESOURCE ID的因為他可能是在不同行程上面執行
而PCONNECT是不會在查詢完成後就關閉,而是會等待一定時間這個時間可以由MYSQL上的wait_timeout設定而且mysql_close並無法關閉pconnect所開啟的連線,只能關閉connect所開啟的資料庫連線。當然如果沒有CLOSE在經過TIMEOUT時間之後也是會被DB DROP掉。
PCONNECT必須要小心使用,在寫的不好的CODE中,會殘留一大批的DATABASE CONNECTION。最大值可以這樣計算每台SERVER的APACHE行程數 * 所有WEB SERVEWR數量當然不需要我來提醒,每個連線都會消耗DB的CPU TIME與MEMORY
寫WEB CODE時,不論使用何種語言,有兩點一定要再動手寫之前先有答案一是,要從DATABASE抓取什麼資料二是,要傳送什麼資料到USER端(其實這也不只用於WEB)DATABASE的連線是很慢的,能夠一次抓取完所需的資料,就不要分兩次

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.