为什么我的查询在运行时速度很快。
select count(*) as aggregate from `news` where `news`.`deleted_at` is null and `status` = '1'
但是,我跑的时候很慢。
select count(*) as aggregate from `news` where `news`.`deleted_at` is null and `status` = '1' and `newscategory_id` = '17'
是我的表news
结构图,看看here .
对不起,因为我的声望小于8,所以我不能附上图片。
最佳答案
尝试在您用于选择的三列上添加复合索引:
ALTER TABLE news ADD INDEX comp_index (deleted_at, status, newscategory_id);
再检查一遍。 可能使用 EXPLAIN 来查看是否使用了任何索引。
关于php - 按类别过滤时Mysql计数慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28446552/