开发者

Multiple MYSQL queries vs. Multiple php foreach loops

开发者 https://www.devze.com 2022-12-16 07:34 出处:网络
Database structure: idgalleryIdtypefile_namedescription 1`artists_2010-01-15_7c1ec``image``band602.jpg``Red Umbrella Promo`

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 Pa开发者_开发技巧rty`
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.


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.


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.


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.


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.


It really depends,

IN operator

ini_set('memory_limit', '-1');
$startMemory = memory_get_usage();
$conn = mysqli_connect("localhost", "", "", "");
$ar = array();
$sql = "SELECT * FROM table WHERE e IN (.....)";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $ar[$row['c']] = $row;
}

echo (memory_get_usage() - $startMemory) / 1024 / 1024, ' MB'; //1409.7124481201
$end_time = microtime(true);
echo ($end_time - $start_time) . ' Seconds'; //5.2406549453735 Seconds

Foreach

ini_set('memory_limit', '-1');
$startMemory = memory_get_usage();
$conn = mysqli_connect("localhost", "", "", "");
$ar = array();
$array_loop = array(....)
foreach($array_loop as $key => $value){
   $sql = "SELECT * FROM table WHERE e = '$value'";
   $result = mysqli_query($conn, $sql);
   while ($row = mysqli_fetch_assoc($result)) {
      $ar[$row['c']] = $row;
   }
}

echo (memory_get_usage() - $startMemory) / 1024 / 1024, ' MB'; //42.773330688477 MB 
$end_time = microtime(true);
echo ($end_time - $start_time) . ' Seconds'; //12.469061136246 Seconds

I noticed that foreach consumes time but not memory and IN operator consumes memory but not time. All the test done based on test data generated by sql procudre about 1 Million

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号