WordPress 是一個緩慢的 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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。
