How many queries are you running?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I just read that some content management systems are running more than 30 queries to generate a regular page. Some said they were making several hundred database calls. I just could not believe that. How many queries are generating your pages? And why so many?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

My blog homepage (running WordPress) run 18 queries, and an article page runs 28. I'm guessing that it runs so many because of the plugins...they are on their own to get the data they need, so I'm sure there are redundant calls. Plus, simple template tags that gets a setting like the blog path is a query every time it's ran - even if in the template several times. I haven't had the time to tweak it, and I'll worry about it more when my blog pulls enough traffic to be a problem.

My own sites that I do from scratch doesn't do nearly that many. I would say maybe 5 tops.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I just read that some content management systems are running more than 30 queries

Drupal is a well-known sql query hog - I would guess in the range of 50+ queries for a page...

I'm guessing that it runs so many because of the plugins...

Yup, those nifty modules add their own set of queries...

Drupal also has methods to reduce the page load, including caching some javascript and css, and "throttle" which will reduce or turn off selected modules during heavy traffic.

Some developers will cache all static content, using memcache and/or distributed server architecture.

I met with developers at Northpoint Solutions who are using combinations of these techniques to create Drupal sites capable of handling 1,000,000+ page views/minute!

They have: 5 posts

Joined: Jan 2009

Most of the time I had about 60 queries.. But sometimes (very often) it goes upto 100 even..
The site becomes slow and little unresponsive at that time..
Does anyone else also experience the same issue?

Cheap cPanel Web Hosting with 99.9% uptime - Web Hosting India

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Funny this topic came back up. Last night I added in some code on a site, and it did an average of 60 calls per page. One page that loads in all product info to do comparisons, topped out at 1300 calls for the one page (PHP was actually executing for just over 4 seconds). Needless to say, this site will get looked over to better optimize it's code...

Also, something to consider on a high traffic production site, not only the number of queries, but number of actual database connections and makings sure queries are freed up when done using them.

Also optimizing the queries (not actual code, generalized for easier reading)

SELECT UserID FROM UsersOnline WHERE LastAccess {within 5 minutes}
{Loop thorugh all results}
    SELECT * FROM Users WHERE UserID={rs.UserID}
    SELECT Thumbnail FROM Files WHERE Type='Atavar' AND ParentID={rs.UserID}

    {Display user pic/info}

Now if 20 people are online, that bit makes 41 queries, where it could be done in one query (ok, i'm a bit tired, you may have to look up the exact, but here is the gist)

SELECT u.*, f.thumb FROM Users u
LEFT JOIN Files f on f.ParentID=u.UserID
WHERE u.UserID IN (SELECT UserID) FROM UsersOnline WHERE LastAccess {within 5 minutes})

Oh the resources saved! When you have a query that is based upon data being looped upon from an earlier query, it is usually a good spot for a JOIN! Always have the DB server do as much as it can to limit and format your results!

-Greg

They have: 5 posts

Joined: Feb 2009

I try not to be clog with querries.

They have: 13 posts

Joined: Feb 2009

My page have 30+ quires, sometimes i do more on other sites.

They have: 1 posts

Joined: May 2009

At some stage in the code you will fill the report eg. with

JasperFillManager.fillReport(jr, reportParameters,
dataSource);

All you have to make sure is that the reportParameters contains all the
values
from your queries in a Map.

Eg. with totalCount 100

and that your jrxml references the parameter with $P{totalCount}. Use a text

field in iReport but change from $F to $P ...

Test it out with a hardcoded Map first and then fill the map from your data.

[url=http://www.scooterandwheelchairstore.com]wheelchair lifts[/url]
[url=http://www.bogar.nl/makeup.html]permanente make up[/url]

Megatron's picture

They have: 2 posts

Joined: May 2009

The number of queries doesn't really matter... of course it should be as low as possible, but if they are optimized 1000 queries will be faster then 1 bad query.

I've worked with systems that did 6000 queries for a youtube like home page because of bad "concept" in the background code. Totally WTF when I first realized the number, but the queries were optimized and the site worked normaly even during high loads.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Hmm, even the most complex sites with lots of information requirements I have built have not had more than 3 queries on a page.

Initially designing the database with your known site output is key. Then you don't need many queries to access the data.

They have: 4 posts

Joined: May 2009

My site has approx 40 queries.

He has: 53 posts

Joined: Jun 2010

Hi, According to my exp. it, depend on the page content , means Actually what you want to do and it's vary from page to page.

They have: 1 posts

Joined: Oct 2010

I had about 60 queries.. But sometimes (very often) it goes up to 100 even..]

{links removed}

stokes1900's picture

They have: 55 posts

Joined: Oct 2010

as for the best practice number of query should be reduced. if number of query is large it will slow down your application.. we should keep in mind these things while developing application.

They have: 9 posts

Joined: Dec 2010

Hey guys,,,,, sneha here,,,,,I think good information,,,,,,,," According to my exp. it, depend on the page content , means Actually what you want to do and it's vary from page to page"............Thanks lot nice article .

[url=http://awebsitedevelopment.com/]Seo Services in Delhi[/url] | [url=http://www.panditgopalsharma.com/]fengShui Consultant[/url]| [url=http://www.bestastrologysolutions.com/astrology.html]Astrology Consultant [/url] | [url=http://www.vaastunaresh.com/]Vastu[/url]|

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.