什么是php脚本中最优的MYSQL查询数? [英] What is the optimal MYSQL query number in php script?

查看:203
本文介绍了什么是php脚本中最优的MYSQL查询数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专业程序员,所以我不能确定这个。有多少mysql查询你的脚本发送在一个页面,什么是你最佳的查询数。例如在stackoverflow的主页列出问题显示这些问题的作者。是stackoverflow发送mysql查询foreach问题来获取作者的信息。或者它发送1个查询并获取所有用户数据并与问题匹配?

I am not professional programmer so i can not be sure about this.How many mysql queries your scripts send at one page and what is your optimal query number .For example at stackoverflow's homepage it lists questions shows authors of these questions . is stackoverflow sends mysql query foreach question to get information of author. or it sends 1 query and gets all user data and match it with questions ?

推荐答案

不要关注查询数。这不是一个有用的指标。您需要查看其他几项内容:

Don't focus on the number of queries. This is not a useful metric. Instead, you need to look at a few other things:


  • 有多少查询重复?

  • 有多少查询有交叉数据集?或者是另一个的子集?

  • 他们需要多长时间运行?
  • how many queries are duplicated?
  • how many queries have intersecting datasets? or are a subset of another?
  • how long do they take to run? have you profiled the common ones to check indices?
  • how many are unnecessarily complex?

很多次,我已经有了很多次了。看到三个更简单的查询一起在返回相同信息的一个复杂的十分之一的时间中执行。同样的道理,SQL是强大的,但不要疯狂地尝试在SQL中做一些事情,这在PHP的一个循环中会更容易和更简单。

Numerous times I've seen three simpler queries together execute in a tenth of the time of one complex one that returned the same information. By the same token, SQL is powerful, but don't go mad trying to do something in SQL that would be easier and simpler in a loop in PHP.

你在做多少渐进处理?

how much progressive processing are you doing?

如果无法避免使用大型数据集进行长时间查询,请尝试重新排列算法,以便在数据库来自数据库时处理数据集。这允许您在MySQL中使用未缓冲的查询,并且可以提高内存使用率。如果您可以在这样做时提供输出,则可以尽快提供第一个输出,从而提高网页的感知速度。

If you can't avoid longer queries with large datasets, try to re-arrange the algorithm so that you can process the dataset as it comes from the database. This lets you use an unbuffered query in MySQL and that improves your memory usage. And if you can provide output whilst you're doing this, you can improve your page's perceived speed by provinding first output sooner.

的这个数据?甚至缓存它几秒钟可以帮助非常大。

how much can you cache some of this data? Even caching it for a few seconds can help immensely.

这篇关于什么是php脚本中最优的MYSQL查询数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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