多个 MYSQL 查询与多个 php foreach 循环

发布于 2024-08-18 00:23:37 字数 591 浏览 3 评论 0原文

数据库结构:

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`

我要为应用程序的一个部分提取图像,在另一个部分提取视频,等等。为每个部分进行多个 mysql 查询是否更好,如下所示:

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

...或者我应该构建一个关联数组并每当我需要使用结果集时,只是一遍又一遍地循环数组?

谢谢你的想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冬天旳寂寞 2024-08-25 00:23:37

这取决于哪个更重要:可读性或性能。我希望单个查询和预填充 PHP 数组的执行速度会更快,因为数据库连接的成本很高,但是每个部分的简单查询都更具可读性。

除非您知道(而不仅仅是希望)您将获得大量流量,否则我会进行单独的查询,然后担心优化是否会成为问题。那时,您无论如何都会想做其他事情,例如构建数据访问层并添加一些缓存。

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.

丘比特射中我 2024-08-25 00:23:37

如果“部分”指的是用户可以查看的单独的单个页面(单独的 HTTP 请求),我建议根据需要按类型进行查询。例如,如果在只有图像数据集的页面上,您实际上不需要获取视频数据集。您不会真正节省太多时间来获取所有内容,因为无论如何您都会为每个页面点击连接到数据库(我假设)。

如果“部分”指的是一页的不同部分,那么立即获取所有内容。这将节省您的查询时间(仅一次查询)。

但是,根据数据集的大小,您可能会遇到 PHP 的内存限制查询所有内容的问题。然后,您可以尝试提高内存限制,但如果失败,您可能不得不退回到按类型查询。

使用按类型查询方法可将部分计算负载转移到数据库服务器,因为您将仅请求和获取真正需要的内容。而且您不必编写代码来过滤和排序结果。数据库通常比 PHP 代码更擅长过滤和排序。如果可能的话,启用 MySQL 的查询缓存,这将比用 PHP 编写的任何查询都快得多。

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.

逆光下的微笑 2024-08-25 00:23:37

如果你的数据全部来自一张表,我只会做一个查询。

我假设您正在构建一个页面,其中包含图片部分、视频部分、音乐部分等。编写按媒体类型排序的查询返回结果 - 迭代所有图片,然后是所有视频,然后是所有音乐。

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.

一个人的旅程 2024-08-25 00:23:37

最好有多个查询。每次运行查询时,所有数据都会被拉出并加载到内存中。如果您有 5 种不同的类型,则意味着该类型的每个页面加载的数据量是其所需数据量的 5 倍。

即使一次只有一个,如果您有超过 100 个或每次可以合理地在一页上显示的数量,您可能会希望相当快地开始使用 LIMIT/OFFSET 查询进行分页。

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.

债姬 2024-08-25 00:23:37

这确实取决于,

IN 运算符

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

我注意到 foreach 消耗时间但不消耗内存,而 IN 运算符消耗内存但不消耗时间。所有测试均基于sql procudre生成的测试数据约100万条

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文