Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple MYSQL queries vs. Multiple php foreach loops

Database structure:

id  galleryId                 type     file_name       description
1   `artists_2010-01-15_7c1ec`  `image`  `band602.jpg`   `Red Umbrella Promo`
2   `artists_2010-01-15_7c1ec`  `image`  `nov7.jpg`      `CD Release Party`
3   `artists_2010-01-15_7c1ec`  `video`  `band.flv`      `Presskit`

I'm going to pull images out for one section of an application, videos on another, etc. Is it better to make multiple mysql queries for each section like so:

$query = mysql_query("SELECT * FROM galleries WHERE galleryId='$galleryId' && type='image');

...Or should I be building an associative array and just looping through the array over and over whenever I need to use the result set?

Thanks for the thoughts.

like image 487
jay Avatar asked Jan 15 '10 17:01

jay


4 Answers

It depends what's more important: readability or performance. I'd expect a single query and prefilling PHP arrays would be faster to execute, since database connections are expensive, but then a simple query for each section is much more readable.

Unless you know (and not just hope) you're going to get a huge amount of traffic I'd go for separate queries and then worry about optimising if it looks like it'll be a problem. At that point there'll be other things you'll want to do anyway, such as building a data access layer and adding some caching.

like image 60
Karl B Avatar answered Oct 17 '22 13:10

Karl B


If by "sections" you mean separate single pages (separate HTTP requests) that users can view, I would suggest query-per-type as needed. If on a page where there are only image data sets, you really don't need to fetch the video data set for example. You won't be really saving much time fetching everything, since you will be connecting to the database for every page hit anyway (I assume.)

If by "sections" you mean different parts of one page, then fetch everything at once. This will save you time on querying (only one query.)

But depending on the size of your data set, you could run into trouble with PHP's memory limit querying for everything, though. You could then try raising the memory limit, but if that fails you'll probably have to fall back to query-per-type.

Using the query-per-type approach moves some of the computing load to the database server, as you will only be requesting and fetching what you really need. And you don't have to write code to filter and sort your results. Filtering and sorting is something the database is generally better at than PHP code. If at all possible, enable MySQL's query cache, that will speed up these queries much more than anything you could write in PHP.

like image 41
fresch Avatar answered Oct 17 '22 15:10

fresch


If your data is all coming from one table, I would only do one query.

I presume you are building a single page with a section for pictures, a section for video, a section for music, etc. Write your query return results sorted by media type - iterate through all the pictures, then all the video, then all the music.

like image 2
Hugh Bothwell Avatar answered Oct 17 '22 15:10

Hugh Bothwell


Better to have multiple queries. Every time you run a query all the data is getting pulled out and loaded into memory. If you have 5 different types, it means each page of that type is loading 5 times as much data as it needs to do.

Even with just one at a time, you are probably going to want to start paginating with LIMIT/OFFSET queries fairly quickly if you have more than 100 or however many you can reasonably display on one page at a time.

like image 1
gtd Avatar answered Oct 17 '22 15:10

gtd