在你回答这个问题之前,我从来没有开发过任何流行到足以达到高服务器负载的东西。请把我当作(唉)一个刚刚登陆地球的外星人,尽管我知道PHP和一些优化技术。


我正在开发一个PHP工具,可以获得相当多的用户,如果它是正确的。然而,虽然我完全有能力开发程序,但当涉及到制作可以处理巨大流量的东西时,我几乎一无所知。所以这里有一些关于它的问题(也可以把这个问题变成一个资源线程)。

数据库

At the moment I plan to use the MySQLi features in PHP5. However how should I setup the databases in relation to users and content? Do I actually need multiple databases? At the moment everything's jumbled into one database - although I've been considering spreading user data to one, actual content to another and finally core site content (template masters etc.) to another. My reasoning behind this is that sending queries to different databases will ease up the load on them as one database = 3 load sources. Also would this still be effective if they were all on the same server?

缓存

我有一个用于构建页面和交换变量的模板系统。主模板存储在数据库中,每当一个模板被调用时,它的缓存副本(html文档)就会被调用。目前,我在这些模板中有两种类型的变量-静态变量和动态变量。静态变量通常是像页面名称,网站的名称-不经常改变的东西;动态变量是在每次页面加载时改变的东西。

我的问题是:

比如说我对不同的文章有评论。这是一个更好的解决方案:存储简单的注释模板,并在每次页面加载时呈现注释(来自DB调用),或者将注释页面的缓存副本存储为html页面——每次添加/编辑/删除注释时,页面都会被重新检索。

最后

有人有任何提示/指针运行一个高负载的PHP网站。我很确定这是一种可行的语言——Facebook和Yahoo!优先考虑——但有什么经验是我应该注意的吗?


当前回答

我不敢相信居然没有人提到这个:模块化和抽象。如果您认为您的站点将不得不扩展到许多机器,那么您必须这样设计它!这意味着一些愚蠢的事情,比如不要假设数据库在本地主机上。它还意味着一些一开始会很麻烦的事情,比如编写数据库抽象层(像PDO,但要轻得多,因为它只做您需要它做的事情)。

这意味着在一个框架下工作。您将需要对代码进行分层,以便稍后通过重构数据抽象层(例如,通过告诉它某些对象位于不同的数据库中)来获得性能,并且代码不必知道或关心。

最后,要注意内存密集型操作,例如不必要的字符串复制。如果你能保持PHP的内存使用较低,那么你的web服务器就会得到更好的性能,当你采用负载平衡的解决方案时,这是可以扩展的。

其他回答

当然pdo很好,但是它与mysql和mysqli相比的性能一直存在一些争议,尽管现在看起来已经固定了。

如果您考虑可移植性,您应该使用pdo,如果不考虑可移植性,则应该使用mysqli。它有一个面向对象接口、准备好的语句和pdo提供的大部分功能(除了可移植性)。

另外,如果真的需要性能,可以准备PHP 5.3中的MysqLnd驱动程序,它将与PHP更加紧密地集成,具有更好的性能和改进的内存使用(以及用于性能调优的统计数据)。

如果你有集群服务器(和youtube一样的负载),Memcache是很好的,但我也会先尝试APC。

我在一些网站上工作过,这些网站都是由PHP和MySQL支持的,每个月都有数百万的点击率。以下是一些基本知识:

Cache, cache, cache. Caching is one of the simplest and most effective ways to reduce load on your webserver and database. Cache page content, queries, expensive computation, anything that is I/O bound. Memcache is dead simple and effective. Use multiple servers once you are maxed out. You can have multiple web servers and multiple database servers (with replication). Reduce overall # of request to your webservers. This entails caching JS, CSS and images using expires headers. You can also move your static content to a CDN, which will speed up your user's experience. Measure & benchmark. Run Nagios on your production machines and load test on your dev/qa server. You need to know when your server will catch on fire so you can prevent it.

我推荐阅读《构建可扩展的网站》,它是由Flickr的一位工程师写的,是一个很好的参考。

看看我关于可伸缩性的博客文章,它有很多关于多种语言和平台可伸缩性的演示文稿的链接: http://www.ryandoherty.net/2008/07/13/unicorns-and-scalability/

看来我错了。MySQLi仍在开发中。但是根据这篇文章,PDO_MySQL现在由MySQL团队贡献。摘自文章:

The MySQL Improved Extension - mysqli - is the flagship. It supports all features of the MySQL Server including Charsets, Prepared Statements and Stored Procedures. The driver offers a hybrid API: you can use a procedural or object-oriented programming style based on your preference. mysqli comes with PHP 5 and up. Note that the End of life for PHP 4 is 2008-08-08. The PHP Data Objects (PDO) are a database access abstraction layer. PDO allows you to use the same API calls for various databases. PDO does not offer any degree of SQL abstraction. PDO_MYSQL is a MySQL driver for PDO. PDO_MYSQL comes with PHP 5. As of PHP 5.3 MySQL developers actively contribute to it. The PDO benefit of a unified API comes at the price that MySQL specific features, for example multiple statements, are not fully supported through the unified API. Please stop using the first MySQL driver for PHP ever published: ext/mysql. Since the introduction of the MySQL Improved Extension - mysqli - in 2004 with PHP 5 there is no reason to still use the oldest driver around. ext/mysql does not support Charsets, Prepared Statements and Stored Procedures. It is limited to the feature set of MySQL 4.0. Note that the Extended Support for MySQL 4.0 ends at 2008-12-31. Don't limit yourself to the feature set of such old software! Upgrade to mysqli, see also Converting_to_MySQLi. mysql is in maintenance only mode from our point of view.

对我来说,这篇文章似乎偏向MySQLi。我想我偏向于PDO。 我真的很喜欢PDO胜过MySQLi。这对我来说很简单。这个API更接近于我编写的其他语言。OO数据库接口似乎工作得更好。

我还没有遇到过任何PDO无法提供的MySQL特性。如果有的话,我才会惊讶呢。

@Gary

不要使用MySQLi——PDO是“现代的”OO数据库访问层。最重要的功能是在查询中使用占位符。使用服务器端准备和其他优化也足够聪明。

我现在正在看PDO,看起来你是对的-但是我知道MySQL正在为PHP开发MySQLd扩展-我认为是为了成功MySQL或MySQLi -你怎么看?


@Ryan, Eric, tj9991

谢谢你关于PHP缓存扩展的建议——你能解释一下为什么要使用一个而不是另一个吗?我听说过通过IRC的memcached很棒,但从来没有听说过APC -你对它们有什么看法?我认为使用多个缓存系统会适得其反。

我肯定会挑选一些测试人员,非常感谢你的建议。

首先,正如Knuth所说,“过早的优化是万恶之源”。如果你现在不需要处理这些问题,那就不要去做,先专注于交付一些正确工作的东西。也就是说,如果优化不能等待。

试着分析你的数据库查询,找出什么是慢的,什么是经常发生的,并从中提出一个优化策略。

我会研究Memcached,因为很多高负载站点都使用它来有效地缓存所有类型的内容,而且它的PHP对象接口非常好。

在服务器之间分割数据库并使用某种负载平衡技术(例如,在具有必要数据的冗余数据库中生成1到#之间的随机数—并使用该数字确定要连接到哪个数据库服务器)也是提高效率的一种极好的方法。

在过去,对于一些相当高的负载站点,这些方法都非常有效。希望这能帮助你开始:-)