在你回答这个问题之前,我从来没有开发过任何流行到足以达到高服务器负载的东西。请把我当作(唉)一个刚刚登陆地球的外星人,尽管我知道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!优先考虑——但有什么经验是我应该注意的吗?


当前回答

@Gary

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

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


@Ryan, Eric, tj9991

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

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

其他回答

一般

在开始看到真实世界的负载之前,不要尝试优化。你可能猜对了,但如果你猜错了,那你就是在浪费时间。 使用jmeter、xdebug或其他工具对站点进行基准测试。 如果加载开始成为一个问题,对象或数据缓存都可能涉及到,所以通常阅读缓存选项(memcached, MySQL缓存选项)

Code

对代码进行分析,以便了解瓶颈在哪里,以及它是在代码中还是在数据库中

数据库

Use MYSQLi if portability to other databases is not vital, PDO otherwise If benchmarks reveal the database is the issue, check the queries before you start caching. Use EXPLAIN to see where your queries are slowing down. After the queries are optimized and the database is cached in some way, you may want to use multiple databases. Either replicating to multiple servers or sharding (splitting the data over multiple databases/servers) may be appropriate, depending on the data, the queries, and the kind of read/write behavior.

缓存

Plenty of writing has been done on caching code, objects, and data. Look up articles on APC, Zend Optimizer, memcached, QuickCache, JPCache. Do some of this before you really need to, and you'll be less concerned about starting off unoptimized. APC and Zend Optimizer are opcode caches, they speed up PHP code by avoiding reparsing and recompilation of code. Generally simple to install, worth doing early. Memcached is a generic cache, that you can use to cache queries, PHP functions or objects, or entire pages. Code must be specifically written to use it, which can be an involved process if there are no central points to handle creation, update and deletion of cached objects. QuickCache and JPCache are file caches, otherwise similar to Memcached. The basic concept is simple, but also requires code and is easier with central points of creation, update and deletion.

杂项

考虑高负载的替代web服务器。像lighthttp和nginx这样的服务器可以用比Apache少得多的内存处理大量流量,如果你可以牺牲Apache的强大功能和灵活性(或者如果你不需要这些东西,通常情况下,你不需要)。 请记住,现在的硬件非常便宜,所以一定要花费精力来优化一大块代码,而不是“让我们购买一个巨型服务器”。 考虑将“MySQL”和“scaling”标签添加到这个问题中

APC是绝对必须的。它不仅是一个伟大的缓存系统,而且从自动缓存的PHP文件中获得的好处是天赐良机。至于多数据库的想法,我认为在同一台服务器上使用不同的数据库不会有什么好处。它可能会在查询时提高一些速度,但我怀疑为确保三者同步而部署和维护代码所付出的努力是否值得。

我还强烈建议运行Xdebug来查找程序中的瓶颈。它使优化对我来说轻而易举。

我的第一个建议是考虑这个问题,并在设计网站时牢记它,但不要走极端。通常很难预测一个新网站的成功,我认为你的时间最好花在早点起床,然后再优化它。

一般来说,Simple是快速的。 模板会降低您的速度。数据库会降低您的速度。复杂的库会降低您的速度。从数据库中检索模板并在一个复杂的库中解析它们——>时间延迟相互相乘。

一旦你有了基本的站点并开始运行,就可以做一些测试,告诉你应该把精力花在哪里。很难看出目标在哪里。通常,为了加快速度,你必须分解代码的复杂性,这会使代码变得更大,更难维护,所以你只在必要的时候才这么做。

根据我的经验,建立数据库连接是相对昂贵的。如果可以的话,不要在访问量最大的页面(如网站首页)上为普通访问者连接数据库。创建多个数据库连接非常疯狂,而且收效甚微。

我不认为自己会很快从MySQL转换过来——所以我想我不需要PDO的抽象功能。DavidM,谢谢你的文章,它们帮了我很多。

第一个问题是,你真正期望它有多大?你们计划在基础设施上投资多少?既然你觉得有必要在这里问这个问题,我猜你希望从有限的预算开始。

Performance is irrelevant if the site is not available. And for availability you need horizontal scaling. The minimum you can sensibly get away with is 2 servers, both running apache, php and mysql. Set up one DBMS as a slave to the other. Do all the writes on the master, and all the reads on the local database (whatever that is) - unless for some reason you need to read back the data you've just read (use master). Make sure you've got the machinery in place to automatically promote the slave and fence the master. Use round-robin DNS for the webserver addresses to give more affinity for the slave node.

在这个阶段,在不同的数据库节点上划分你的数据是一个非常糟糕的主意——然而,你可能会考虑在同一台服务器上的不同数据库上划分数据(当你超越facebook时,这将有助于跨节点划分)。

一定要确保你有适当的监控和数据分析工具来衡量你的网站性能和识别瓶颈。大多数性能问题都可以通过编写更好的SQL /修复数据库模式来解决。

将模板缓存保存在数据库上是一个愚蠢的想法——数据库应该是结构化数据的中央公共存储库。将你的模板缓存保存在你的web服务器的本地文件系统中——这样会更快,也不会降低你对数据库的访问速度。

使用操作码缓存。

花大量的时间研究你的网站和它的日志,了解为什么它运行得这么慢。

将尽可能多的缓存推到客户端。

使用mod_gzip可以压缩所有内容。

C.