How many mySQL queries is too much?

06-23-03, 12:40 PM
|
|
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."
|

06-23-03, 03:27 PM
|
|
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
|

06-24-03, 01:53 AM
|
|
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
|

06-24-03, 05:00 AM
|
|
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.
|

06-24-03, 07:42 AM
|
|
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
Instead of:
(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
|

06-24-03, 05:56 PM
|
|
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.
|

06-27-03, 11:25 AM
|
|
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."
|

06-27-03, 11:54 AM
|
|
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)
|

06-27-03, 12:35 PM
|
|
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.
|

06-27-03, 06:32 PM
|
|
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:
And then each query has the querynum++ after it.
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."
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|