Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
MySQL - When to open and close the database
Old 05-16-2008, 10:54 AM MySQL - When to open and close the database
dartiss's Avatar
Experienced Talker

Latest Blog Post:
November
Posts: 32
Name: David Artiss
Location: Nottingham, UK
Trades: 0
Hi,

A bit of a "school boy" question I suspect but... when is it most efficient to open and close a MySQL database?

For the site http://www.bmtg.org.uk then I'm generally doing it a number of times per page - each page is a number of included scripts and I generally tend to open and close the same database within each include. It occurs to me that opening the database at the top of each page and then closing it at the bottom is possibly the most efficient way.

Any thoughts, suggestions?

Many thanks,
David.
dartiss is offline
Reply With Quote
View Public Profile Visit dartiss's homepage!
 
 
Register now for full access!
Old 05-16-2008, 11:34 AM Re: MySQL - When to open and close the database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It depends...

Ha, you didn't thought of that one, right !?
Seriously, in PHP, you have 2 connections mode: Permanent or non-permanent

The non permanent is the traditional mysql_connect(), while the other is mysql_pconnect()

When you do a request with PHP in a NP (non permanent) mode, the engine will reuse the connection resources during your page processing.
As far as I know, a idle resource will be used for the answers of other peoples requests too.
If the number of opened connection is too low, then a new connection is opened to the server, until the server says that the max_connections_allowed is reached.
In that mode, you don't need to close the db connection. PHP does that alone when the engine shut down itself.

In a permanent mode, on the other hand, the only difference is that the PHP engine don't close the connection on it's shutdown. He keeps it opened to server other instances.

You have to find the balance between the open/close time, the limitation of opened connection and the memory consumption of those connections.

Every connection use a bit of memory that is used as a buffer.
So, with numerous NP connections, you could run out of memory if the number of threads goes up.
On the other end, it means that you have a lot of traffic, because you need a lot of concurrent db connections.

So, in my experience, with low to medium traffic, a NP connection is ok. Just remove your mysql_close() calls.

But if your site traffic reaches pick, a switch to P connections can help you to handle more precisely the memory consumption of your web app.

Reading myself back, I wonder if this is not too cryptic...
I hope so.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-16-2008 at 11:36 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-16-2008, 11:38 AM Re: MySQL - When to open and close the database
dartiss's Avatar
Experienced Talker

Latest Blog Post:
November
Posts: 32
Name: David Artiss
Location: Nottingham, UK
Trades: 0
Sounds good to me.

You say I could simply remove the closes - of course this would leave a number of opens of the database. Although this won't cause errors, won't it be better, performance wise, to remove these extra opens as well, and leave it to a single open and close?

David.
dartiss is offline
Reply With Quote
View Public Profile Visit dartiss's homepage!
 
Old 05-16-2008, 01:53 PM Re: MySQL - When to open and close the database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, the PHP engine recycle the already opened connections that are idle.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-16-2008, 03:27 PM Re: MySQL - When to open and close the database
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by tripy View Post
When you do a request with PHP in a NP (non permanent) mode, the engine will reuse the connection resources during your page processing.
As far as I know, a idle resource will be used for the answers of other peoples requests too.
If the connection string is identical, and there's a free connection in the pool, it should be reused. Bear in mind, tho, plucking a connection from the pool is still an expensive operation. It's far better to "manually reuse" this type of resource by not closing and opening it in the first place, when that's possible.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL - When to open and close the database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15378 seconds with 12 queries