Jump to content

[SOLVED] Is it better to have 1 big query or 3-4 smaller ones?


Recommended Posts

OK, this is what I'm doing. I'm working on a website and on the main page I need to pull data from my database. What I need to pull is the main content form one table, then I need to get the 8 most recent posts from my forums, and the top 8 most downloaded files as well as may be pulling user information.


Now this site is a little different from others in that it's user friendly err that users can actually modify the site to their taste.


I'm starting the php coding now and I'm wondering if it's a better idea to query all at once (which I'm not sure how to do exactly) however I do know how to query every thing individually.


So I don't really know. I've read articles and read that sometimes it may be better to do a 2 or 3 queries to reduce overhead, which makes since if you have a few visitors, but how would it play out on heavy traffic?

Link to comment
Share on other sites

Usually one large, well designed, query is better than four smaller queries.... but I do recall an instance where splitting a small portion of a large query resulted in a decreased page load time - this depends on how connected that query table is to the original source and what information is available to query against.

Link to comment
Share on other sites

It sounds like the information that you're querying is very different -- main content vs. eight posts from another table, etc.  You'll necessarily be writing seperate queries for these.


If I'm misunderstanding and a single query does make sense, and can be written in four queries, then you should benchmark to be sure which is faster.


If you do encounter heavy traffic, another option may be to keep some things stored on disk, such as your main site content, for instance.  Keeping those in files on disk could avoid database overhead.

Link to comment
Share on other sites

Well this site is like nothing I've worked on before. Right now what I'm trying to do is create a custom forum. I've decided to get rid of the download section and go with a "Most Viewed" this will work as the same idea but reduce the amount of rows in my database.


When you go to my site, the main index page will do a few things.

1. Read the 8-10 latest topics in my forums (which I do need help coding the forums if any one is will to donate the time)

2. Then it reads the the 8-10 most viewed files

3. If you're logged in check to see what template you have set as default and apply that.


I don't have much of any thing up right now, but my site http://vx-fx.com

Link to comment
Share on other sites

Actually now that I think about it, If I only need to pull no more than 24 rows from the database, the perhaps joining the queries would work, but there is a problem that lies in that. I've read articles and tutorials on JOIN but I don't really get it.............

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.