租号玩代理申请
A- A+

WordPress 文章归档页 分类 标签 留言评论数量 文章数量 最后更新时间等查询语句说明记录

2026年07月06日 建站相关 暂无评论 阅读 54 次 打印

最近,优化了一下我的归档页面,提升一下性能,在此记录一下优化后的代码和优化手段,这些查询大多走索引,有一些不走索引,我们需要手动给数据库MYSQL添加索引。

一、全套索引 + 对应代码归属(区分【模板页面】/【归档函数】+ 对应 SQL + MySQL5.7/8.0 创建语句)
索引 1:idx_archive_year_post(仅归档函数 archives_list_cx 使用,wp_posts)

MySQL5.7 创建

ALTER TABLE wp_posts ADD INDEX idx_archive_year_post (post_date,post_type,post_status,post_password,ID);

MySQL8.0 创建

ALTER TABLE wp_posts ADD INDEX idx_archive_year_post (post_date,post_type,post_status,post_password,ID) COMMENT '归档函数archives_list_cx专用覆盖索引,加速年份分组、年份区间文章查询';

归属:仅 inc/functions/archives.php 归档函数

匹配两条 SQL
查询全部年份(归档函数)

SELECT DISTINCT YEAR(post_date) AS y, COUNT(ID) AS cnt FROM wp_posts WHERE post_date < %s AND post_type='post' AND post_status='publish' AND post_password='' GROUP BY y ORDER BY y DESC

按年份区间读取文章(归档函数)

SELECT ID, post_date, post_title, comment_count FROM wp_posts WHERE post_date >= %s AND post_date < %s AND post_type='post' AND post_status='publish' ORDER BY post_date DESC

-------以上是inc/functions/archives.php 归档函数对应的,下面没有了-------

索引 2:idx_tax_count(仅模板页面统计模块使用,wp_term_taxonomy)

MySQL5.7

ALTER TABLE wp_term_taxonomy ADD INDEX idx_tax_count (taxonomy,count,term_id);

MySQL8.0

ALTER TABLE wp_term_taxonomy ADD INDEX idx_tax_count (taxonomy,count,term_id) COMMENT '归档模板顶部统计:筛选有内容分类、标签计数';

归属:template-archives.php 模板头部统计代码

匹配 SQL

SELECT COUNT(DISTINCT tt.term_id) FROM wp_term_taxonomy tt WHERE tt.taxonomy = 'category' AND tt.count > 0

索引 3:idx_post_stat_total(仅模板页面统计模块使用,wp_posts)
MySQL5.7

ALTER TABLE wp_posts ADD INDEX idx_post_stat_total (post_type,post_status,post_password);

MySQL8.0

ALTER TABLE wp_posts ADD INDEX idx_post_stat_total (post_type,post_status,post_password) COMMENT '归档模板统计:快速统计已发布公开文章总数';

归属:template-archives.php 模板头部统计
作用:优化 wp_count_posts() 底层 COUNT 统计

索引 4:idx_post_modified_sort(仅模板页面统计模块使用,wp_posts)
MySQL5.7

ALTER TABLE wp_posts ADD INDEX idx_post_modified_sort (post_type,post_status,post_modified);

MySQL8.0

ALTER TABLE wp_posts ADD INDEX idx_post_modified_sort (post_type,post_status,post_modified DESC) COMMENT '归档模板统计:查询最新修改文章,展示站点最后更新时间';

归属:template-archives.php 模板头部更新时间统计
匹配 SQL

SELECT post_modified FROM wp_posts WHERE post_type IN ('post','page') AND post_status = 'publish' ORDER BY post_modified DESC LIMIT 1

索引 5:idx_comment_approved(仅模板页面统计模块使用,wp_comments)
MySQL5.7

ALTER TABLE wp_comments ADD INDEX idx_comment_approved (comment_approved);

MySQL8.0

ALTER TABLE wp_comments ADD INDEX idx_comment_approved (comment_approved) COMMENT '归档模板统计:统计前台已审核有效留言总数';

归属:template-archives.php 模板评论数量统计
匹配 SQL

SELECT COUNT(*) FROM wp_comments WHERE comment_approved = '1'

二、区分归属总览
归档函数(inc/functions/archives.php)独占索引:idx_archive_year_post
归档模板(template-archives.php)独占索引:idx_tax_count、idx_post_stat_total、idx_post_modified_sort、idx_comment_approved
所有索引互不冲突,一次创建,模板 + 归档全部加速。

三、查看所有自定义索引(通用,不分版本)

-- 文章表索引
SHOW INDEX FROM wp_posts WHERE Key_name IN ('idx_archive_year_post','idx_post_stat_total','idx_post_modified_sort');
-- 分类标签表索引
SHOW INDEX FROM wp_term_taxonomy WHERE Key_name = 'idx_tax_count';
-- 评论表索引
SHOW INDEX FROM wp_comments WHERE Key_name = 'idx_comment_approved';

四、删除索引语句
MySQL5.7(无 IF EXISTS,索引不存在会报错)

ALTER TABLE wp_posts DROP INDEX idx_archive_year_post;
ALTER TABLE wp_posts DROP INDEX idx_post_stat_total;
ALTER TABLE wp_posts DROP INDEX idx_post_modified_sort;
ALTER TABLE wp_term_taxonomy DROP INDEX idx_tax_count;
ALTER TABLE wp_comments DROP INDEX idx_comment_approved;

MySQL8.0(安全删除,索引不存在无报错)

ALTER TABLE wp_posts DROP INDEX IF EXISTS idx_archive_year_post;
ALTER TABLE wp_posts DROP INDEX IF EXISTS idx_post_stat_total;
ALTER TABLE wp_posts DROP INDEX IF EXISTS idx_post_modified_sort;
ALTER TABLE wp_term_taxonomy DROP INDEX IF EXISTS idx_tax_count;
ALTER TABLE wp_comments DROP INDEX IF EXISTS idx_comment_approved;

五、补充缓存归属区分
1、模板页面缓存(template-archives.php)有效期 86400
archive_cat_num、archive_tag_num、archive_post_num、archive_comment_count、archive_last_update
2、归档函数缓存(archives.php)有效期 86400
archive_year_list_global、archive_year_single_年份数字、archive_build_lock(锁 10 秒)

最后,批量创建我统计一下:
MySQL 5.7 批量创建(无索引注释)

-- 归档函数专用索引
ALTER TABLE wp_posts ADD INDEX idx_archive_year_post (post_date,post_type,post_status,post_password,ID);
-- 统计:文章总数索引
ALTER TABLE wp_posts ADD INDEX idx_post_stat_total (post_type,post_status,post_password);
-- 统计:站点更新时间索引
ALTER TABLE wp_posts ADD INDEX idx_post_modified_sort (post_type,post_status,post_modified);
-- 统计:分类标签计数索引
ALTER TABLE wp_term_taxonomy ADD INDEX idx_tax_count (taxonomy,count,term_id);
-- 统计:评论计数索引
ALTER TABLE wp_comments ADD INDEX idx_comment_approved (comment_approved);

MySQL 8.0 批量创建(带注释方便识别用途)

-- 归档函数archives_list_cx专用覆盖索引,加速年份分组、年份区间文章查询
ALTER TABLE wp_posts ADD INDEX idx_archive_year_post (post_date,post_type,post_status,post_password,ID) COMMENT '归档函数专用索引';
-- 统计模块:快速统计已发布公开文章总数
ALTER TABLE wp_posts ADD INDEX idx_post_stat_total (post_type,post_status,post_password) COMMENT '统计-文章总数';
-- 统计模块:按修改时间倒序取最新文章,展示站点更新日期
ALTER TABLE wp_posts ADD INDEX idx_post_modified_sort (post_type,post_status,post_modified DESC) COMMENT '统计-站点更新时间';
-- 统计模块:筛选有内容的分类、标签计数
ALTER TABLE wp_term_taxonomy ADD INDEX idx_tax_count (taxonomy,count,term_id) COMMENT '统计-分类标签数量';
-- 统计模块:统计前台已审核有效留言总数
ALTER TABLE wp_comments ADD INDEX idx_comment_approved (comment_approved) COMMENT '统计-评论总数';

给我留言

5 + 3 =
评论规范提醒