Tối Ưu Database WordPress 2026: Hướng Dẫn Dọn Dẹp A-Z Giảm 40% Dung Lượng

Câu trả lời nhanh
Database WordPress phinh len vi post revisions, spam comments, expired transients va orphaned metadata tich tu theo thoi gian. Don dep bang WP-CLI va SQL queries giam 30-50% dung luong database, giam TTFB 150-400ms va cai thien Core Web Vitals. Buoc ngo critical cho site WordPress chay cham.

Database WordPress là nơi lưu moi thu: bai viet, binh luan, cai dat plugin, transient cache, metadata. Sau 1-2 nam hoat dong, database cua ban co the phinh len den hang tram MB rac thuc. Minh da xem xet hon 50 site WordPress trong nam 2026 va phat hien ra rang 90% site chay cham hon muc tieu chi vi database chua don dep bao gio.

Trong huong dan nay, minh se di tung buoc tu sao luu, don dep post revisions, xoa spam comments, clear transients, don orphaned metadata, den optimize table va set up tu dong. Tat ca deu dung WP-CLI va SQL queries cu the, ban co the copy-pape lai chay duoc ngay.

Database WordPress Phinh Len Vi Cau Gi?

Tối ưu hóa database WordPress
Tối ưu hóa database WordPress

Mo lan ban nhan “Save Draft” hay “Update”, WordPress luu mot ban revision moi. Tren blog 500 bai viet, moi bai chinh sua 10 lan, ban co 5.000 ban revision du thua trong wp_posts. Them vao do, wp_options tro thanh “bo xuong” cai dat plugin da xoa, transient het han khong tu xoa, va orphaned metadata tich tung ngay.

Ket qua? Database 300+ MB trong khi du lieu thuc te chi can 100 MB. Moi query SQL phai scan qua nhieu hon can thiet, TTFB tang 150-400ms, Core Web Vitals bi anh huong truc tiep.

Sao Luu Database Truooc Khi Don Dep Co Can Thiet Khong?

Bat buoc. Don dep database la thao tac destructive – du lieu xoa roi khong lay lai duoc. Ban can tao full backup truoc khi chay bat ky lenh nao. Dung UpdraftPlus, Duplicator, hoac WP-CLI:

wp db export backup-$(date +%Y%m%d).sql

Luu file backup o ngoai server – Amazon S3, Google Drive, hoac o cung cu the. Dung bao tuyet doi vao snapshot hang ngay cua hosting nhu buoc bao ve duy nhat khi dang thay doi database.

Cach Xoa Post Revisions Gan Nhieu Nhat Trong WordPress?

Post revisions la nguyen nhan so 1 gay phinh database. WordPress mac dinh luu vo han revision. Ban nen gioi han xuong 3 ban moi them vao wp-config.php:

define( 'WP_POST_REVISIONS', 3 );

Xoa tat ca revision cu bang WP-CLI:

wp post delete $(wp post list --post_type='revision' --format=ids) --force

Tren site 800 bai viet chay 3 nam, buoc nay thuong giai phong duoc 40-80 MB va giam 80-150ms admin page load.

Cach Xoa Spam Comments Va Comment Trash Hang Loat?

Bang wp_comments va wp_commentmeta phinh nhanh tren site co nhieu traffic. Cho du co Akismet, spam van tich tu. Xoa bang WP-CLI:

wp comment delete $(wp comment list --status=spam --format=ids) --force
wp comment delete $(wp comment list --status=trash --format=ids) --force

Tren site traffic cao tich tu spam nhieu nam, buoc nay co the xoa hang chuc nghin rows va giai phong 10-50 MB.

Expired Transients La Gi Va Xoa The Nao?

Transient la key-value tam thoi ma plugin va theme luu trong wp_options nhu lightweight cache. Khi transient het han, WordPress khong tu dong xoa – row cu ngoi do voi expiration timestamp qua khu. Tren site ban, hang tram stale transient tich tu trong nhieu thang.

Xoa expired transients bang WP-CLI:

wp transient delete --expired

Xoa tat ca transients (can than, se tu tao lai):

wp transient delete --all

Luu y: Neu ban chay Redis hoac Memcached object cache, transient duoc luu o RAM chu khong trong database. Buoc nay tro nen khong can thiet – day cung la ly do lon nen dung object cache.

Cach Don Orphaned Metadata Trong WordPress?

Khi xoa bai viet hoac user, cac row lien quan trong wp_postmeta va wp_usermeta khong phai luc nao cung duoc don sach. Chung tro thanh “orphaned rows” – khong con lien ket den bat ky post hay user nao nhung van ton tai trong database.

Xoa orphaned postmeta bang SQL query (chay trong phpMyAdmin hoac qua WP-CLI):

DELETE pm FROM wp_postmeta pm
LEFT JOIN wp_posts p ON p.ID = pm.post_id
WHERE p.ID IS NULL;

Xoa orphaned usermeta:

DELETE um FROM wp_usermeta um
LEFT JOIN wp_users u ON u.ID = um.user_id
WHERE u.ID IS NULL;

Luon kiem tra so row truoc va sau khi xoa:

SELECT COUNT(*) FROM wp_postmeta;

WP_Options Autoload Bloat Anh Huong The Nao Den Toc Do?

wp_options la table dac biet vi cac row co autoload=yes se duoc load vao bo nho tren moi page load. Neu plugin lung tung set autoload=yes cho du lieu lon, TTFB tang dang ke. Minh thuong thay site co 3-5MB autoload data trong khi chi can 500KB.

Kiem tra tong kich thuoc autoload data:

SELECT SUM(LENGTH(option_value)) AS total_size
FROM wp_options WHERE autoload = 'yes';

Tim top 10 lon nhat:

SELECT option_name, LENGTH(option_value) AS size
FROM wp_options WHERE autoload = 'yes'
ORDER BY size DESC LIMIT 10;

Nhung option lon hon 1MB nen duoc set autoload=no neu plugin ho tro. Mot so plugin cache nhu WP Rocket hay LiteSpeed Cache thuong gay autoload bloat vi luu CSS/JS inline vao options.

Cach Chay OPTIMIZE TABLE De Defragment Database?

Sau khi xoa rows, MySQL tables co “khoang trong” – goi la table fragmentation. Lenh OPTIMIZE TABLE giai phong khong gian do va build lai index. Tren InnoDB (default MySQL 8.0), lenh nay tuong duong ALTER TABLE ... ENGINE=InnoDB va co the lock table mot lat, nen chay luc it traffic.

Optimize tat ca WordPress tables cung luc:

wp db optimize

Hoac target cu the trong phpMyAdmin – chon all tables, chon “Optimize table” tu dropdown. Hoac chay SQL truc tiep:

OPTIMIZE TABLE wp_posts, wp_postmeta, wp_options, wp_comments, wp_commentmeta;

Tren site co fragmentation lon, OPTIMIZE TABLE giam table size 20-40% va cai thien query time dang ke.

Dung Plugin Nao De Don Dep Database Tu Dong?

Neu WP-CLI va SQL kho hoc, hai plugin sau lam moi thu bang click:

WP-Optimize (Free / Premium $49/nam): Xoa revisions, spam, transients, optimize table, va len lich tu dong. Phu hop voi da so site WordPress. Tab Database liet ke tung danh muc don dep kem so row tuong ung.

Advanced Database Cleaner (Free / Pro $39/nam): Manh ve tim tables bi bo lai boi plugin da go cai dat. Phan loai table thanh “WordPress core”, “known plugins”, hoac “unknown” – de danh biet va xoa data thua cua plugin da go nhieu thang truoc.

Ca hai deu chay duoc OPTIMIZE TABLE va ho tro len lich cleanup tu dong hang tuan hoac hang thang.

Object Cache Redis Co Giup Database Nhe Hon Khong?

Co, va rat nhieu. Redis object cache luu query results, transients, va frequently-accessed options trong RAM. Response time cho cached object giam xuong duoi 1ms thay vi 5-50ms cho database reads. Khi Redis hoat dong, transient khong con luu trong wp_options nua – no luu o Redis.

Mình da viet huong dan chi tiet ve cau hinh Redis Object Cache cho WordPress neu ban mun trien khai. Ket hop database cleanup + Redis la combo manh nhat de giam TTFB.

Ket Qua Thuc Te Sau Khi Don Dep Database?

Day la ket qua thuc te tren site WooCommerce 4 nam, hosting shared (PHP 8.4, MySQL 8.0, 2GB RAM) ma minh da thu:

  • Database size: 312 MB xuong 187 MB (-40%)
  • wp_posts rows: 14.800 xuong 3.200 (-78%)
  • wp_options rows: 3.940 xuong 1.820 (-54%)
  • TTFB homepage: 780ms xuong 410ms (-47%)
  • Admin “Posts” load: 3.4s xuong 1.1s (-68%)

TTFB giam tu 780ms xuong 410ms du du de chuyen Core Web Vitals tu “Needs Improvement” len “Good”. Ket hop voi page caching va Nginx FastCGI Cache, day la thao tac leverage cao nhat ban nen lam tren site WordPress lai.

Cach Set Up Don Dep Database Tu Dong Hang Tuan?

One-time cleanup chi giai quyet tam thoi. Ma khong co maintenance, rac se tich tu lai trong vai thang. Cach tot nhat la tu dong hoa:

Cach 1: Cron job tren server – Tao script bash chay WP-CLI:

#!/bin/bash
# db-cleanup.sh - Run weekly via crontab
cd /var/www/yoursite.com/public_html

wp post delete $(wp post list --post_type='revision' --format=ids) --force --allow-root
wp comment delete $(wp comment list --status=spam --format=ids) --force --allow-root
wp transient delete --expired --allow-root
wp db optimize --allow-root

Them vao crontab chay moi Sunday 3h sang:

0 3 * * 0 /path/to/db-cleanup.sh >> /var/log/wp-db-cleanup.log 2>&1

Cach 2: WP-Optimize scheduler – Vao WP-Optimize > Settings, bat “Scheduled cleanup”, chon weekly. Don gian hon nhung it linh hoat hon cron job.

Minh thich dung server cron thay vi WP-Cron de chay tac vu nay – on dinh hon va khong bi anh huong boi traffic.

Gioi Han Post Revisions De Ngan Phinh Database

Sau khi don dep xong, ban can ngan rac tich tu lai. Them vao wp-config.php:

// Gioi han 3 revision moi bai viet
define( 'WP_POST_REVISIONS', 3 );

// Xoa trash sau 7 ngay
define( 'EMPTY_TRASH_DAYS', 7 );

// Tat autosave interval (mac dinh 60s)
define( 'AUTOSAVE_INTERVAL', 300 );

Buoc don nhat nhung hieu qua nhat. Site cua ban se khong bao gio phinh len nhu truoc nua.

Kiem Tra Database Sau Khi Don Dep Bang Cong Cu Nao?

Sau khi optimize, ban can do lai de xem ket qua. Dung cac tool sau:

Query Monitor (plugin free): Hien thi query time, memory usage, va hook breakdown tren moi page. Cai xong, xem tab “Queries” o admin bar.

PageSpeed Insights: Do TTFB va Core Web Vitals truoc/sau khi don dep de so sanh.

New Relic / Blackfire: Cho site traffic cao, dung APM tool de profile database queries cu the dau cham.

WP-CLI: Kiem tra kich thuoc database:

wp db size
wp db size --tables

Lenh nay hien thi kich thuoc moi table rieng le, giup ban xac dinh table nao van con lon sau khi don dep.

Tong Ket: Database Optimization La Buoc Khong Nguoc Lai

Database optimization la mot trong nhieu thao tac co leverage cao nhat tren site WordPress. Khong can plugin phuc tap, khong can thay hosting, chi can don dep data thua va gioi han revision. Site cua ban se cham hon tu ngay dau tien.

Thu tu uu tien minh de xuat: (1) Backup, (2) Xoa revisions, (3) Xoa spam comments, (4) Clear transients, (5) Don orphaned metadata, (6) Optimize table, (7) Set up tu dong. Lam theo dung thu tu nay, ban se giam 30-50% database size trong 30 phut.

Neu ban moi bat dau voi WordPress, minh co huong dan tong quan toc do WordPress day du tu server den Core Web Vitals. Database optimization chi la 1 trong 10 buoc – doc het de co gang full optimization.

Thanh Tùng

Mình là Thanh Tùng. Bạn bè gọi mình là "bác sĩ máy tính" vì hễ máy nào có vấn đề là mình muốn mò vào xem sao. Mình viết hướng dẫn theo cách mà mình mong người khác đã viết cho mình ngày xưa — từng bước rõ ràng, không bỏ sót, và nói luôn cái gì hay bị lỗi. Ngoài giờ làm mình chơi guitar, nuôi mèo, và có một con VPS riêng dành riêng cho việc cài thử đủ thứ linh tinh.

Xem tất cả bài viết →

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *