PHP阵VS MYSQL查询速度优化 [英] PHP Array vs MYSQL Queries speed optimization

查看:136
本文介绍了PHP阵VS MYSQL查询速度优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的PHP的MySQL驱动页面尽可能快。
我有一个相当标准的网站与文章和食谱(从DB加载)列出了左边列菜单,然后在主要内容有一些用剪切previews还随机食谱随机变化的文章。

I'm trying to make my php mysql driven pages as fast as possible. I've got a fairly standard website with a left-column menu with lists of articles and recipes (loaded from the db) and then on the main content there are some randomly changing articles with clipped previews and also random recipes.

我已经在此刻得到的是它加载的所有文章和食谱到一个PHP数组,看起来像这样的SQL查询:

What I've got at the moment is one sql query which loads all the articles and recipes into a php array that looks like this:

$s = s("SELECT * FROM `pages` WHERE `visible`='1' ORDER BY `group` ASC, `date` DESC");
   while($r=mysql_fetch_assoc($s)) $hPage[]=$r;
   $hPage_count = count($hPage);
   $hPage_keys = array_keys($hPage);
   $hPage_size = sizeOf($hPage_keys);

然后,我只是重提和搜索这个数组,我需要为我的网页的不同部分的所有数据。

Then I simply refer back to and search this array for all the data I need for different parts of my page.

我的一个循环看起来像

for ($i=0; $i<$hPage_size; $i++){
$c = $hPage[$hPage_keys[$i]];
if($c['group']==1){
$type[$c['type']][count($type[$c['type']])] = array('title'=>$c['title'],'url'=>$c['url']);
}
}

我需要找出的是,这是做的最快方法?只有一个SQL查询,加载一切都变成一个PHP数组,然后与工作,或者这将是更好地使多个SQL查询?

What I need to find out is, is this the fastest way to do it? With only one SQL query, loading everything into a php array and then working with that, Or would it be better to make multiple SQL queries?

感谢您的帮助。

推荐答案

作为一个经验法则,拉着你在尽可能少的数据库调用尽可能需要的所有信息对于应用程序的性能是一件好事。假设你是不是拉下了大量的信息,你实际上并不需要因为我觉得你可能已经在正确的轨道上的页面。

As a rule of thumb, pulling all the information you need in as few database calls as possible is a good thing for application performance. Assuming you aren't pulling down a ton of information you don't actually need for the page I think you're probably already on the right track.

既然你实际使用的数据库来查询,你要确保查询被很好的优化,以及。

Since you're actually using the database to query, you'll want to make sure the query is well optimized as well.

正如所有的东西性能相关的只有真正了解是一个测量和分析器,不过,只有当存在真的需要提高性能。方法

As with all things performance related only way to really know is with a measurements and a profiler, though, and that only if there's really a need to increase the performance.

这篇关于PHP阵VS MYSQL查询速度优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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