473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Performance issues with frequently closing/opening mysql connections?

I have been faced a couple of times with the situation that I wanted to
write a script and was worried about a too frequent opening and closing
mysql connections.

To give some examples:

1)I am trying to develop a photoalbum (also posted a topic called "array /
mysql question"). Of course I can use some wonderful open source albums but
at the same time I want to get more familiar with all the ins and outs so my
idea is a list of filenames, where the database connection should be
opened, a statement like"select filenames from photos" , and, every filename
maybe with a thumbnail is a link to a second page and the whole is followed
by closing the connection. The second page could be something like opening
the database connection, a statement like "select description ( and other
fields) from photos where filename = $filename" , and closing the database
again. Let's say the visitor looks at the picture and its description,
clicks on a link to the first page with the thumbnails and agin the
connection is opened and closed to execute the first select statement, the
visitor chooses another picture etc. etc....

2)On www.tapperijbeekendonk.nl on the left column there is a menu item
"activiteiten". when there are special activities like a music performancein
the near future they are shown below that menu item. A select statement is
done where the first announcement date, and the last announcement date are
checked and the records where the current date is between those values are
selected (and shown in the menu bar of course). Again the connection is
opened and closed again immediately after the select statement. However,
this query is stored in "header.php" so on every page that is visited, the
connection is opened and closed. It would also be nice to make a link on the
"one-photo-page" to the previous and the next photo, which leads again to
the open-select-close "ritual".

I am not really a php guru (but not a newbie either) and unfortunately I
could not find many info on google about the performance issues that can be
involved when I decide to write such scripts. There were some vague
indications that it is better to open and close a connection frequently than
leave it open too long in order to execute more queries but I could not
really find good documentation about it.

I also thought to store the results of the query into a session variable but
I am not sure about the performance issues of sessions either.

Any ideas will be welcome.

Thanks,

Martien.

Dec 2 '06 #1
7 3226
Sorry made a weird error when copying and pasting in my first post. The last
sentence in the paragraph about De Tapperij belongs to example 1 :)
"Martien van Wanrooij" <in**@martienvanwanrooij.nlschreef in bericht
news:47***************************@news.chello.nl. ..
I have been faced a couple of times with the situation that I wanted to
write a script and was worried about a too frequent opening and closing
mysql connections.

To give some examples:

1)I am trying to develop a photoalbum (also posted a topic called "array /
mysql question"). Of course I can use some wonderful open source albums but
at the same time I want to get more familiar with all the ins and outs so my
idea is a list of filenames, where the database connection should be
opened, a statement like"select filenames from photos" , and, every filename
maybe with a thumbnail is a link to a second page and the whole is followed
by closing the connection. The second page could be something like opening
the database connection, a statement like "select description ( and other
fields) from photos where filename = $filename" , and closing the database
again. Let's say the visitor looks at the picture and its description,
clicks on a link to the first page with the thumbnails and agin the
connection is opened and closed to execute the first select statement, the
visitor chooses another picture etc. etc....However, this query is stored in
"header.php" so on every page that is visited, the connection is opened and
closed. It would also be nice to make a link on the "one-photo-page" to the
previous and the next photo, which leads again to the open-select-close
"ritual".

2)On www.tapperijbeekendonk.nl on the left column there is a menu item
"activiteiten". when there are special activities like a music performancein
the near future they are shown below that menu item. A select statement is
done where the first announcement date, and the last announcement date are
checked and the records where the current date is between those values are
selected (and shown in the menu bar of course). Again the connection is
opened and closed again immediately after the select statement.

I am not really a php guru (but not a newbie either) and unfortunately I
could not find many info on google about the performance issues that can be
involved when I decide to write such scripts. There were some vague
indications that it is better to open and close a connection frequently than
leave it open too long in order to execute more queries but I could not
really find good documentation about it.

I also thought to store the results of the query into a session variable
but I am not sure about the performance issues of sessions either.

Any ideas will be welcome.

Thanks,

Martien.

Dec 2 '06 #2
Martien van Wanrooij wrote :
[...]
Your problem is quite a common one. In fact, a request that is done on every
page may cause problem, and even more if not needed.

The idea to solve your "problem" would be to use cache. Instead of displaying
the HTML result, you put it in a file that you'll you include when needed.
If you're using a templating system, I'd suggest you to check wether it is able
to manage caching. Otherwise, you'll have to do your own system, that will
update the HTML cache file every time the data updates.
For example : if your page displays the nearest dates to the next performances,
you'll have to update the HTML cache file every time you add a performance
date.

But there's another solution : you can, on the page that displays the dates,
check the last modification time of the HTML cache file, and if it was
modified before one day ago, you update the cache ( by doing the SQL request ),
and then display the cache file ( since it has been updated... :D ).
That would give you : one SQL request a day. Not so many, eh? :)

--
Naixn
http://fma-fr.net
Dec 2 '06 #3

"naixn" <na***@won-fma.comschreef in bericht
news:45***********************@nan-newsreader-05.noos.net...
But there's another solution : you can, on the page that displays the
dates,
check the last modification time of the HTML cache file, and if it was
modified before one day ago, you update the cache ( by doing the SQL
request ),
and then display the cache file ( since it has been updated... :D ).
Thanks this is a wonderful suggestion. My hosting provider gives me the
right to give writing access to certain folders and I guess that in rough
lines, caching means: writing data to a file on the server when a request is
made from a client?
That would give you : one SQL request a day. Not so many, eh? :)
Enough info to study the topic seriously, one more question. You say, with
a smiley though, one SQL request a day so it could mean that the very first
visitor tonight at 0:00 generates a SQL request and that request writes
"something" to the caching file , when the second visitor shows up at 0:05,
he get his data from that recently update file? Sorry to ask it in such a
simplified way but with all due respect to all these wonderful cms'es I want
to understand the topic as thoroughly as possible.

Martien

Dec 2 '06 #4
Martien van Wanrooij wrote :
>
"naixn" <na***@won-fma.comschreef in bericht
news:45***********************@nan-newsreader-05.noos.net...
>But there's another solution : you can, on the page that displays the
dates,
check the last modification time of the HTML cache file, and if it was
modified before one day ago, you update the cache ( by doing the SQL
request ),
and then display the cache file ( since it has been updated... :D ).
Thanks this is a wonderful suggestion. My hosting provider gives me the
right to give writing access to certain folders and I guess that in
rough lines, caching means: writing data to a file on the server when a
request is made from a client?
>That would give you : one SQL request a day. Not so many, eh? :)
Enough info to study the topic seriously, one more question. You say,
with a smiley though, one SQL request a day so it could mean that the
very first visitor tonight at 0:00 generates a SQL request and that
request writes "something" to the caching file , when the second visitor
shows up at 0:05, he get his data from that recently update file? Sorry
to ask it in such a simplified way but with all due respect to all these
wonderful cms'es I want to understand the topic as thoroughly as possible.

Martien
Yep that's quite it. To simplify :
You get your site working.
Someone arrives on the page at 10 AM.
There is no file where datas where cached
-Your system queries the database, and write the adequate HTML depending on
the result into a file, as it would usually output.
Your system include the cache file, meaning it outputs the db results.
Another ppl arrives on the page at 11.05 AM.
A cache file exists.
It's modification time is less than one day
No update needed
Include the cache file.
[...]
Some guy arrives on the page at 10.30 AM the day after.
A cache file exists.
But it was modified more than 24h ago.
-Your system query the database...
Include cache file.
etc.

A cache file is no more than a text/html file in which you write the desired
HTML output.

--
Naixn
http://fma-fr.net
Dec 2 '06 #5

"naixn" <na***@won-fma.comschreef in bericht
news:45***********************@nan-newsreader-07.noos.net...
Yep that's quite it. To simplify :
Thank you Naixn that is really the kind of simplification I needed :)
Dec 2 '06 #6
Martien van Wanrooij wrote :
>
"naixn" <na***@won-fma.comschreef in bericht
news:45***********************@nan-newsreader-07.noos.net...
>Yep that's quite it. To simplify :
Thank you Naixn that is really the kind of simplification I needed :)

You're welcome.
Be aware that there are a lot of templating systems that manage caching.
For example : http://smarty.php.net , an official PHP Project :)

--
Naixn
http://fma-fr.net
Dec 2 '06 #7

"naixn" <na***@won-fma.comschreef in bericht
news:45***********************@nan-newsreader-05.noos.net...
Be aware that there are a lot of templating systems that manage caching.
For example : http://smarty.php.net , an official PHP Project :)
I will certainly use them as I am not up to re-inventing the wheel but
before using them I like to understand how they work :)
Thx again.

Dec 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gregory.Spencer | last post by:
Hi There, Been working in a PHP / MySQL project I took over code development for. Today when testing new changes suddenly started getting "Failed to connect to the database" errors. When...
16
by: David W. Fenton | last post by:
http://www.granite.ab.ca/access/performancefaq.htm I hope Tony doesn't mind my opening a discussion of some issues on his performance FAQ page here in the newsgroup. This is not meant as...
9
by: bluedolphin | last post by:
Hello All: I have been brought onboard to help on a project that had some performance problems last year. I have taken some steps to address the issues in question, but a huge question mark...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
4
by: | last post by:
I was just wondering what other people's opinions and experiences were in regards to using Database Connections throughout a website. Supposing that a single webpage accesses a database anywhere...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
3
by: KWilliams | last post by:
I'd like to get some good advice about our old ASP site. You can see our home page at: http://www.douglas-county.com/ ....and an example application page at:...
1
by: marcfischman | last post by:
Please help. I have a website running on a linux/apache/mysql/php server. I receive about 8,000-10,000 visitors a day with about 200,000 to 300,000 page views. The server is a RedHat Linux...
6
by: Andrea A | last post by:
Hi all, I have a question: what do you think about performance (only performance not style, functionality, and so on) comparing PHP 4 and PHP 5. Which version is better if one want to develop a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.