首页 后端开发 php教程 WordPress 是一个缓慢的 CMS

WordPress 是一个缓慢的 CMS

Sep 05, 2024 pm 04:31 PM

WordPress is a Slow CMS

我的旧帖子 WordPress es un CMS lento 的英文版 - 2014

我不止一次地陷入争论:WordPress 慢吗?好吧,当附加到 WordPress 的人的唯一答案是有很多访问量的网站使用它并且它们的性能是最佳的时,这并没有太大的争议。这些人似乎忘记了,如果在功能强大的机器上“运行”,即使冒泡排序算法对于过大的样本也能表现良好。然而,如果我们考虑其计算复杂性,这并不意味着它一定是一种有效的算法(事实上并非如此)。 WordPress 也会发生同样的情况。考虑到相同数量的信息,它将需要比其他 CMS 更强大的托管。不仅如此,正如我们将看到的,无论是否有大量信息,WordPress 本质上都很慢。

这并不意味着 WordPress 不好。事实并非如此。就像汽车一样,速度并不是一切。 CMS 领域也是如此。事实上,我的许多网络项目都是用它构建的。然而,每个项目都是不同的,因此,您需要明智地选择最好的工具,而不是出于执着。

由于我是一名技术人员,所以我的论点将基于技术方面。特别是当我了解到 WordPress 由于其设计而缓慢时。我邀请任何不同意的人发表评论并说明他们的理由。

一切尽在一张桌子上

在为 Web 项目设计数据库模式时,会出现一个问题:是追求实用性还是效率。就 WordPress 而言,他们选择了实用性,并将帖子、自定义帖子、资源和版本全部分组在一个表中:wp_posts。此操作的优点是简化代码和搜索(尽管这是 WordPress 难以解决的另一个问题,正如我们将在另一篇文章中看到的),但缺点是,它大大降低了 WordPress 的效率。一些例子可以让这一点更清楚:

  • 如果我们有 500 个帖子,每个帖子都有四个不同的修订版(当前的一个和另外三个),就好像我们正在处理 2,000 个帖子。

  • 如果我们在 WooCommerce 中有 500 种产品,并且每种产品都有一张特色图片,并且产品库中有四张,那么就好像我们的 CMS 必须处理 3,000 种产品。

  • 如果我们有一个包含 35 个页面和 35 个菜单项的小型网站,无论是外部链接还是内部链接,我们的内容管理器都会像有 70 个页面一样工作,因为每个菜单项都算作我们 CMS 中的一个条目或页面。在此示例中,这可能看起来不多,但它显示了影响性能的另一个因素。

  • 如果您有 500 种四种语言的产品,您的 WordPress 将像处理 2,000 种产品一样运行。

  • 现在,让我们总结一下现实世界的示例:如果您有一个包含 500 个产品的网站,每个产品都有一个特色图像、四个产品图库图像和一个包含技术信息的 PDF,并且该网站还有一个包含 200 个条目的博客,每个条目都有各自的特色图片。如果您的网站还支持三种语言,并且设置为每个帖子仅允许两次修订,则 WordPress 每次查询数据库时都必须搜索超过 5,500 个项目。我忽略了其他因素,例如菜单项、页面和自定义帖子。建议:

  • 将修订数量限制为两次或完全禁用它们:

// Limit revisions to two:
define('WP_POST_REVISIONS', 2);
// Completely disable revisions:
// define('WP_POST_REVISIONS', false);
登录后复制
  • 不时删除所有修订。您可以通过运行以下 SQL 查询来完成此操作:
DELETE a, b, c FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';
登录后复制
  • 节约网站上的图片。请勿将不会使用的图像添加到您的 CMS。

  • 除非必要,否则避免使用多个菜单。删除您不打算使用的菜单项。

  • 如果您没有其他选择,因为您的客户坚持使用 WordPress 进行中型或大型项目,请尝试创建辅助表以尽可能减轻 wp_posts 的负载。

你的 WordPress 患有老年痴呆症

WordPress 不惜一切代价寻求灵活性,甚至以牺牲速度为代价。也许是因为它一开始只是一个博客系统,在这种情况下,如此大的灵活性不会造成太大的损害。然而,当我们开始使用它作为 CMS 时,这种灵活性导致的性能问题开始出现。

Let me give you some bad news: your content manager has Alzheimer’s. It forgets everything from one request to another. You will have to repeat each time which custom posts, sidebars, or menus you are going to use. There is no other choice because it forgets. That's why, for example, if you want to add an entry to the admin menu, you will have to tell it every time it is to be displayed. Yes, it offers enormous flexibility, but it forces PHP and the CMS to process the same thing repeatedly, resulting in a loss of efficiency. The same thing happens with plugins, which is why many plugins can significantly slow down your website. It’s not because of the plugin system itself (which is magnificently designed and programmed) but because plugins have to declare the same information repeatedly, forcing WordPress to go through them entirely with every request.

A performance-focused CMS would have done it differently. For example, by having the theme declare during activation what sidebars, custom posts, or other elements it needs. WordPress would register this information and adjust internally. The same could be applied to plugins. But, as mentioned earlier, such an approach would significantly reduce the CMS's flexibility, which is not desirable.

Tips:

  • Limit the number of plugins.

  • Choose minimalist themes that only have what you need.

  • You might be advised to use a cache plugin; I don't. Only use one if your website is extremely slow and do so with caution. I will discuss this in another post (edit: now available: Don’t Use Cache Plugins with WordPress, but basically, it’s because you will disable all of WordPress’s internal workings based on hooks. That is, you will force WordPress to work in a way that is not intended.

Everything Always Available

As almost everyone knows, WordPress started as a blogging system based on a previous system. It wasn't designed for large projects, which is why its design leaned toward simplicity. No classes, just functions. As with any design aspect, this doesn’t have to be a bad thing (just ask those using desktops built with GTK) unless you are looking for flexibility. Then, the headaches begin.

If you come from the PHP world, you might be surprised that with WordPress, you don’t have to use "require," "include," or "namespace." This is easy to understand: WordPress always loads its entire arsenal of libraries. Yes, always, whether you use them or not. When you combine this with its memory issues, well... that's a lot of code to read with every request. But, of course, this is all for flexibility. You can use a core function without having to include a file that might change names or paths tomorrow.

Since PHP 5.6, there is full support for function namespaces. Maybe this is a solution for WordPress. But in that case, they will have to make the difficult decision of breaking backward compatibility. I don't know what they will do.

There’s nothing you can do to improve this, as it’s part of WordPress’s design. All you can do is your part: make sure your code doesn't follow this path. If you decide to do so, here are my tips:

  • Create anonymous functions for "actions" that are nothing more than includes to external files where you keep your code. This way, if the action never triggers, PHP won’t have to parse all the code. Example:
add_action('admin_init', function() {
    include(__DIR__ . "/zones/panel/init.php");
});

add_action('admin_menu', function() {
    include(__DIR__ . "/zones/panel/menu.php");
});
登录后复制
  • For widgets, shortcodes, and filters, use classes with namespaces. Also, make sure these classes are instantiated using autoloading.
// It's better to use: spl_autoload_register()

function __autoload($classname) {
    $file = str_replace('\\', DIRECTORY_SEPARATOR, $classname);

    include_once(BASE_PATH . $file . '.php');
}

add_shortcode('block', array('misshortcodesBlock', 'load'));
//... my other shortcodes, filters, and widgets...
登录后复制

In summary, we have seen that WordPress's design principles are simplicity and flexibility, but in a way that sacrifices efficiency. It is essential to understand that no development tool is good for everything. If someone presents it that way, they are either misleading you or selling you a Swiss army knife that is good for nothing.

WordPress struggles with speed, but for showcase websites, this is not something to worry about. However, for websites where the business relies on the web, or for sites with a lot of traffic, alternative options should be considered. Still, if we choose WordPress for its ease of use and flexibility, we must compensate by investing in good hosting, being very careful with the selection of plugins, and using a high-quality theme tailored to our needs.

以上是WordPress 是一个缓慢的 CMS的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1676
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型? Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

您如何防止PHP中的SQL注入? (准备的陈述,PDO) 您如何防止PHP中的SQL注入? (准备的陈述,PDO) Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

PHP和Python:代码示例和比较 PHP和Python:代码示例和比较 Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP:处理数据库和服务器端逻辑 PHP:处理数据库和服务器端逻辑 Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

PHP的目的:构建动态网站 PHP的目的:构建动态网站 Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

在PHP和Python之间进行选择:指南 在PHP和Python之间进行选择:指南 Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

See all articles