Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.20     Use persistent connections

This is NOT the latest copy of this book; click here for the latest version.

If you connect to a database with each script, consider using a persistent connection rather than a normal connection. For MySQL users, that is the difference between using mysql_pconnect() rather than mysql_connect(). Persistent connections remain connected even after your script has ended, which means that the next time a script asks for a connection, it uses the one that is already open - this saves a lot time negotiating passwords and such that can otherwise be used to execute important code.

Switching to persistent connections does not require any other change than adding a "p" in the function name - the parameters are still the same. If your database server is not on the same machine as your web server, consider using CLIENT_COMPRESS as the fifth parameter to your mysql_connect() /mysql_pconnect() call - it allows MySQL to compress data to save space, can drastically lower network bandwidth and transfer speed, particularly when reading in lots of data.





<< 18.1.19 Cache your pages   18.1.21 Take advantage of new features >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Andrey - 29 Aug 2008

Use persistent connections with care because they can be dangerous (a reason they don't exist in the ext/mysqli extension). MySQL can handle a lot of open connections but even then the burded on context-switching and keeping a stack for every thread (MySQL is multithreaded) is big. With some thread library implementations MySQL handles up to 1024 connections (threads) but a higher number can be reached with others. However, a lot of memory is allocated for 3000 threads for example.

Also an Apache instance can server a simple HTML but the MySQL connection is cached in it and not used. With Apache 1.3.x the connections are kept in the every apache process - apache uses processes not threads.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow