在你回答这个问题之前,我从来没有开发过任何流行到足以达到高服务器负载的东西。请把我当作(唉)一个刚刚登陆地球的外星人,尽管我知道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!优先考虑——但有什么经验是我应该注意的吗?
我在一些网站上工作过,这些网站都是由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特性。如果有的话,我才会惊讶呢。
回复: PDO / MySQLi / MySQLND
@gary
你不能说“不要使用MySQLi”,因为他们有不同的目标。PDO几乎就像一个抽象层(尽管实际上不是),它的设计目的是为了方便使用多个数据库产品,而MySQLi则专门针对MySQL连接。在将PDO与MySQLi进行比较的情况下,说PDO是现代访问层是错误的,因为你的声明暗示了进程已经是mysql -> MySQLi -> PDO,而事实并非如此。
MySQLi和PDO之间的选择很简单——如果你需要支持多个数据库产品,那么就用PDO。如果你只使用MySQL,那么你可以在PDO和MySQLi之间选择。
那么你为什么选择MySQLi而不是PDO呢?见下文……
@ross
You are correct about MySQLnd which is the newest MySQL core language level library, however it is not a replacement for MySQLi. MySQLi (as with PDO) remains the way you would interact with MySQL through your PHP code. Both of these use libmysql as the C client behind the PHP code. The problem is that libmysql is outside of the core PHP engine and that is where mysqlnd comes in i.e. it is a Native Driver which makes use of the core PHP internals to maximise efficiency, specifically where memory usage is concerned.
MySQLnd是由MySQL自己开发的,最近已经登陆到PHP 5.3分支,该分支正在RC测试中,准备在今年晚些时候发布。然后你将能够使用mysqnd与MySQLi…但PDO不行。这将使MySQLi在许多方面(不是所有方面)都有性能提升,如果你不需要像PDO那样的抽象功能,它将成为MySQL交互的最佳选择。
也就是说,mysqnd现在可以在PHP 5.3中用于PDO,因此您可以从ND到PDO的性能增强中获得优势,然而,PDO仍然是一个通用的数据库层,因此不太可能像MySQLi那样从ND的增强中获益。
这里可以找到一些有用的基准,尽管它们是2006年的。你还需要注意类似这个选项的事情。
在MySQLi和PDO之间做出选择时,有很多需要考虑的因素。事实上,除非你的请求数量非常高,否则这并不重要,在这种情况下,使用一个专门为MySQL设计的扩展比使用一个抽象的东西并恰好提供MySQL驱动程序更有意义。
这不是哪一个最好的简单问题,因为每一个都有优点和缺点。您需要阅读我提供的链接,然后做出自己的决定,然后测试并找出答案。我在过去的项目中使用过PDO,它是一个很好的扩展,但我对纯性能的选择是MySQLi与新的MySQLND选项编译(当PHP 5.3发布时)。
无论如何,在PHP中缓存是非常简单的,即使没有像memcached这样的扩展/帮助包。
你所需要做的就是使用ob_start()创建一个输出缓冲区。
创建全局缓存函数。调用ob_start,将函数作为回调函数传递。在函数中,查找页面的缓存版本。如果存在,就把它送上,然后结束。
如果不存在,脚本将继续处理。当它到达匹配的ob_end()时,它将调用您指定的函数。这时,您只需要获取输出缓冲区的内容,将它们放到一个文件中,保存文件,然后结束。
添加一些过期/垃圾收集。
许多人没有意识到可以嵌套ob_start()/ob_end()调用。如果你已经在使用输出缓冲区来解析广告或者做语法高亮等等,你可以嵌套另一个ob_start/ob_end调用。