473,698 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5550
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*******@attgl obal.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
6771
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 the SQL Profile to watch the T-SQL-Command which Access ( who creates the commands?) creates and noticed:
3
1729
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 the users should be able to find these items on different properties and with or without substring finding (SQL: LIKE). Example: I want the users to be able to find a customer, providing a customerID, but also providing a customername, zipcode or
16
21314
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 250G, and has one table with 1 billion rows. It is performing in a decent way, but can't understand why a particolar table has strong performance problem.
9
4351
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 predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
3
2327
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 trying to do all record changing using queries. From what I can glean from this (excellent!) group this is the right way to go about it. At present what I'm doing is building & trouble-shooting the queries with query-builder then using code to...
7
21628
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 server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
24
4034
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? A table is a table, a record is a record, a field is a field. All you are doing is manipulating data in tables. I wouldn't think it'd make much difference in working with a table with 10 records or a billion records...they're nothing more than...
1
4432
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 mistakes. Instead, the Library hands out queries to the user given a simple query String. This is how the library does it:
1
1233
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 stating the form you tried opening contains no information. I know it would be an if statement as it is not always the case. Dont really know how to detect whether a form is carrying data or not. Not done this before so any advice would be greatly...
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.