473,406 Members | 2,954 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,406 software developers and data experts.

Lots of small queries vs one big query

Hi,

Pretty new to PHP and MySQL.

I have a page on my site that displays a lot of information from
various tables. Currently I have lots of small PHP wrapper functions
around SQL queries to get each bit of information. This results in
maybe 10 queries to display one page, but they are all very small and
simple, and it keeps the PHP looking nice too as they are simple
function calls to get each piece of info, which can be called from any
page in my site.

However, after reading up on SQL a bit, I am wondering whether I should
be looking at just generating one large query at the start, and using
joins etc to lump all the relevant info together. This complicates the
PHP as this means a custom query for each page of my site depending on
what needs to be displayed, but I am wondering if it is worth it if I
get a fair performance gain.

Can any gurus point me in the right direction? Is there a "proper" way
to do this sort of thing?

Many thanks.

Nov 21 '06 #1
5 5526
li************@hotmail.com wrote:
Hi,

Pretty new to PHP and MySQL.

I have a page on my site that displays a lot of information from
various tables. Currently I have lots of small PHP wrapper functions
around SQL queries to get each bit of information. This results in
maybe 10 queries to display one page, but they are all very small and
simple, and it keeps the PHP looking nice too as they are simple
function calls to get each piece of info, which can be called from any
page in my site.

However, after reading up on SQL a bit, I am wondering whether I should
be looking at just generating one large query at the start, and using
joins etc to lump all the relevant info together. This complicates the
PHP as this means a custom query for each page of my site depending on
what needs to be displayed, but I am wondering if it is worth it if I
get a fair performance gain.

Can any gurus point me in the right direction? Is there a "proper" way
to do this sort of thing?

Many thanks.
That's what relational databases are all about - having related data in
different tables and joining them together.

Sure you'll have different queries on different pages. That's because
you need different data on different pages, don't you? The rest of your
code isn't the same - why should the queries be?

Let the database do what it's best at!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 22 '06 #2

li************@hotmail.com schreef:
Hi,

Pretty new to PHP and MySQL.

I have a page on my site that displays a lot of information from
various tables. Currently I have lots of small PHP wrapper functions
around SQL queries to get each bit of information. This results in
maybe 10 queries to display one page, but they are all very small and
simple, and it keeps the PHP looking nice too as they are simple
function calls to get each piece of info, which can be called from any
page in my site.

However, after reading up on SQL a bit, I am wondering whether I should
be looking at just generating one large query at the start, and using
joins etc to lump all the relevant info together. This complicates the
PHP as this means a custom query for each page of my site depending on
what needs to be displayed, but I am wondering if it is worth it if I
get a fair performance gain.

Can any gurus point me in the right direction? Is there a "proper" way
to do this sort of thing?

Many thanks.
Normally if you join tables, it's because they need to be joined. E.g.
if you want to display the private details of different persons from
PRIVATE_DETAILS_TABLE and all the messages that these persons have sent
from MESSAGES_TABLE, these to tables have to be joined to know what
messages belong to what person. That's logical.
Of course u can first query the PRIVATE_DETAILS_TABLE and based on that
data query the MESSAGES_TABLE with a loop, but that's not so clean and
I guess much slower since you have to query the DB again and again,
while u only need to query it once.

Greets,

Bart

Nov 22 '06 #3
You are generally going to get a performance gain from grouping smaller
queries into one query if possible. Be careful though, because this is
not always the case. There are situations where the cost of the joins
and the possible locking of the tables may decrease performance. These
cases are rare though.

Now, just because you get better performance does not mean that you
should go out and change your code immediately. You mentioned that
your code is currently easy to maintain. This carries a lot of value.
There are certain situations where speed is critical and must be
achieved at any cost, even if it increases the difficulty of the code
maintenance. There's always that balance.

Of course, do not think that the best performance is only achieved
through the most cryptic code. There might be a way to get the best of
both worlds. Is there a way that you can use the functions you have
right now to, rather than perform queries, aggregate onto a query. I
have done this many times where a function call does not generate a
query of its own but simply adds to a query that I am generating. Once
I have called all my necessary methods, I then run the query and
extract the data.

Hopefully this has been somewhat helpful.

Nov 22 '06 #4
There is no "proper" way for all systems. You have to strike a balance
that works for your system. The query cache stores the results of
queries and returns the stored result rather than performing the query
again, provided the tables underneath have not changed.

So if all of your tables update infrequently then you would be able to
get some performance gains out of combining all your queries into one.
However, if one of those tables updates frequently that same combined
query could potentially decrease performance depending on the
complexity of the joins. If some update frequently and some don't then
you could get some gain combining the queries that update infrequently,
and leaving the others alone.

See where I'm going? It's database specific. You need to analyze how
often things update to see whether or not you might gain something by
combining queries. Like others have said, you also have to keep in mind
maintenance.

Nov 22 '06 #5
Just wanted to say thanks for everyone who replied. Much appreciated
and have taken on board many of your comments.

In the end I have grouped some similar queries together rather than the
whole lot, and cached the results in a php class. I then access the
values through that class which just does the one fetch initially, then
feeds me similar values should I request them without doing a lookup.

Thanks again.

Nov 22 '06 #6

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

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
3
by: Versteijn | last post by:
Hello all, I have a database in SQL Server that should save data from a CRM-like application. The database consists of tables like products, services, customers, partners etc. Problem is that...
16
by: mamo74 | last post by:
Hello. I am administering a SQL Server (Enterprise Edition on Windows 2003) from some month and can't understand what is going on in the latest week (when the db grow a lot). The DB is around...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
by: NeilAnderson | last post by:
I'm a fairly new user of access & I've never had any training, so I'm wondering if I'm doing the right thing here, or if it matter at all. I'm building a database for room booking purposes and I'm...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
24
by: Salad | last post by:
Every now and then I see ads that state something like "Experience with Large Databases ...multi-gig...blah-de-blah" And I have to laugh. What's the difference between a large or small database? ...
1
by: JosAH | last post by:
Greetings, Introduction This week we start building Query objects. A query can retrieve portions of text from a Library. I don't want users to build queries by themselves, because users make...
1
by: Constantine AI | last post by:
Hi I am just wanting to know what is the best procedure for the following issues; If i open a form that can contain rows of data but doesn't always. What is the best way to display a message...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.