Current location: Hot Scripts Forums » Programming Languages » PHP » How many mySQL queries is too much?


How many mySQL queries is too much?

Reply
  #1 (permalink)  
Old 06-23-03, 12:40 PM
extremesportsonly extremesportsonly is offline
The Extreme Programmer
 
Join Date: Jun 2003
Location: Atascocita, Texas
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
How many mySQL queries is too much?

I have a rather large web site programmed in PHP and was wondering how many mySQL queries is too many for a single page?

I realize this question is rather vague and would depend on what is being queried, server resources, etc., but I am tying to streamline the programming so it builds the pages more efficiently.

An example: Currently, there are 161 queries on the home page.

Site is at http://www.extremesportsonly.com
__________________
<b>Mark Givens</b>
<a href="http://www.extremesportsonly.com/">ExtremeSportsOnly.com</a><p>"Never rest, never follow, never settle. Always imagine, always strive, always desire more."
Reply With Quote
  #2 (permalink)  
Old 06-23-03, 03:27 PM
Man Down Man Down is offline
HS Staff
 
Join Date: Jun 2003
Location: Maryland
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
I checked out your site and it loaded completely in about 5 seconds. I don't think 161 queries is to many as long as the website loads fine. I'm not sure how many queries is to many and it would be impossible to tell since it varies depending on specs of the server being used.
__________________
Man Down
Reply With Quote
  #3 (permalink)  
Old 06-24-03, 01:53 AM
Zmodem Zmodem is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
161!!! good grief

Perhaps it's time to re-evaluate your code, and optimize optimize optimize.

I think 161 is extremely high for a single page. The *nukes (postnuke, phpnuke, etc) average around 50 queries per page, and that is pretty high.

Your average forum (phpbb, invisionboard) run 12-18 queries per page.

5 seconds to load is not a problem, provided it didn't take 5 whole seconds JUST to query the server. If so, your host is gonna smack you upside the head with a MySQL clue bat. If it took 5 seconds because of Internet lag or browser rendering...fine...

But it's not my site. This is just my 2 cents from a half wit

--cheers
Reply With Quote
  #4 (permalink)  
Old 06-24-03, 05:00 AM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
that is allot, what are you doing to use that many querys?

my aim for all my projects is 10 or less no matter what for display (admins whould be a bit diffrent), your database might be faily small at the moment but think if you did a few thousand posts you page whould take a few secounds to render per user that is un-needed over head.

to try and reduce query calls do some pre-rendering where possible even if it's just some of those side lists, if your using mysql templates do some template caching so you dont load the same template twice, i whould aim for 50 querys at first but try and get that down to 25 since your site does does have a quite on screen.

cache your results if you use them more then once like whats said in the mysql documentation, a database is a bottleneck and you should try your very best to only use it when you realy need to.

nice site thow.
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Reply With Quote
  #5 (permalink)  
Old 06-24-03, 07:42 AM
Chris Boulton Chris Boulton is offline
Wannabe Coder
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 208
Thanks: 0
Thanked 0 Times in 0 Posts
157 queries used. Page build time: 0.2291270 seconds !

Please tell me you arent using a lot of queries inside while loops..thats a killer. You can join then with the JOIN statements..or even better, trick it like
PHP Code:

$query1 mysql_query("SELECT * FROM blah");

while(
$blah mysql_fetch_array($query2)) {
 
$blahcache[$blah[id]] = $blah;
}
$query2 mysql_query("SELECT * FROM blah2");
while(
$blah2 mysql_fetch_array($quer2y)) {
 if(
$blahcache[$blah2[blah_id]]) {
  echo 
"we cached this!";
 } else {
 echo 
"there was no item to cache for this";

Instead of:
PHP Code:

$query mysql_query("SELECT * FROM blah");

while(
$blah mysql_fetch_array($query)) {
 
$query2 mysql_query("SELECT * FROM blah2 WHERE blah_id='$blah[id]");
 
$blah2 mysql_fetch_array($query);
 echo 
$blah2[blah_id];

(Ignore the code...its messy..its late...)

Now, if you are using templates (as mentioned above) you can cache them in something like $templatecache[title] = $contents And then a simple if statement when fetching them will make it so they are not fetched again.

There are lots of other options to consider.. You have to really look into this on the MySQL website.

I usually aim for a maximum of 15 queries (if i am desparate) per page. Most pages of mine use about 11.

(Like the layout as well!)

Chris
__________________
Chris Boulton
SurfiOnline!
MyBulletinBoard
Reply With Quote
  #6 (permalink)  
Old 06-24-03, 05:56 PM
jv2222 jv2222 is offline
The Freshmaker
 
Join Date: Jun 2003
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Something to consider is a web cache that serves only the HTML page - unless the database has been updated in some way.

What I have done in the past is to create a special caching table that has three fields.

1) html_page TEXT
2) rebuild BOOL
3) page_id INT

Then the logic is this.

------

User connects...

1) No cache exists for this page or the rebuild flag is set to true.

Then build HTML page into a $str buffer and insert it into the caching table. Echo the buffer.

2) Cache exists for this page and rebuild flag is false.

Just print html_page from the database and exit.

Cheers,
Justin
__________________
Author of <a href=http://www.hotscripts.com/Detailed/18290.html target=_blank>ezSQL</a> (makes life soooo easy when working with databases)

Last edited by jv2222; 06-24-03 at 05:59 PM.
Reply With Quote
  #7 (permalink)  
Old 06-27-03, 11:25 AM
extremesportsonly extremesportsonly is offline
The Extreme Programmer
 
Join Date: Jun 2003
Location: Atascocita, Texas
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Got the number down to 103 Thanks for replies. Hopefully I can cut another half off that, but I don't see how The home page just has lots of stuff ....only 75 - 80 on the other sport pages ...will need to think about the caching idea sooner than I thought, but know absolutely nothing about caching Lots of user interaction, so caching may not be very efficient either.

thanks again!
__________________
<b>Mark Givens</b>
<a href="http://www.extremesportsonly.com/">ExtremeSportsOnly.com</a><p>"Never rest, never follow, never settle. Always imagine, always strive, always desire more."
Reply With Quote
  #8 (permalink)  
Old 06-27-03, 11:54 AM
jv2222 jv2222 is offline
The Freshmaker
 
Join Date: Jun 2003
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
If you can't cache whole pages then consider the possibility of caching query results of multiple queries by using..

serialize and unserialize.

Cheers,
Justin.
__________________
Author of <a href=http://www.hotscripts.com/Detailed/18290.html target=_blank>ezSQL</a> (makes life soooo easy when working with databases)
Reply With Quote
  #9 (permalink)  
Old 06-27-03, 12:35 PM
Tweakin Tweakin is offline
New Member
 
Join Date: Jun 2003
Location: Minneapolis, MN
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Just keep optimizing slowly to get the number down, as it is quite high (for what I see on the page). Perhaps you could walk us through a few of the queries (maybe a section or so) to provide an idea of how you are approaching things, I am sure you would then get some great input on optimization.

You must be hosted pretty well by the way, those 161 queires took far less then a second to load for me... unless you are miscounting them thats something to keep your head up about :-)

Nice site.
__________________
-Mr Tweakin-

Nutty for all things WebDev.
Reply With Quote
  #10 (permalink)  
Old 06-27-03, 06:32 PM
extremesportsonly extremesportsonly is offline
The Extreme Programmer
 
Join Date: Jun 2003
Location: Atascocita, Texas
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Speaking of counting the queries, this is how I have it being done.

Top of each page is:

PHP Code:

$querynum 0
And then each query has the querynum++ after it.

PHP Code:

$result mysql_query"SELECT ........." ) or errormysql_error() ); $querynum++; 

This should be effective to count the queries, correct?
__________________
<b>Mark Givens</b>
<a href="http://www.extremesportsonly.com/">ExtremeSportsOnly.com</a><p>"Never rest, never follow, never settle. Always imagine, always strive, always desire more."
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
great product for dumping/recovering MySQL databases Dave Brown General Advertisements 1 10-03-03 07:40 AM
mysql to access aspuser25 Database 2 09-16-03 11:01 AM
MySQL with PHP question. HELP for a newbie kenfused PHP 3 08-02-03 12:53 AM
select & delete mysql queries superman PHP 2 07-20-03 03:08 AM


All times are GMT -5. The time now is 08:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.