多个MYSQL查询与多个PHP foreach循环 [英] Multiple MYSQL queries vs. Multiple php foreach loops

查看:161
本文介绍了多个MYSQL查询与多个PHP foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库结构:

  id galleryId类型文件名描述
1`artists_2010-01-15_7c1ec``图片` `band602.jpg``红伞促销'
2`artists_2010-01-15_7c1ec``图片``nov7.jpg``CD发行方`
3`artists_2010-01-15_7c1ec`` video` `band.flv``Presskit`

我要将图片拉出应用程序,在另一个视频等,是更好的做每个部分多个MySQL查询如此:

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





感谢您的帮助,感谢您的支持。想法。

解决方案

这取决于更重要的是:可读性还是性能,我期望一个查询和预填充PHP数组将会更快执行,因为数据库连接是昂贵的,但是每个部分的一个简单的查询更具可读性。



除非你知道(而不只是希望),否则你会得到大量的流量,我会去单独的查询,然后担心优化,如果它看起来像一个问题。到那时,还会有其他的事情要做,比如构建数据访问层并添加一些缓存。


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.

解决方案

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.

这篇关于多个MYSQL查询与多个PHP foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆