就像这样的
表结构如下:
CREATE TABLE `lmx_app_category` ( `id` int(11) NOT NULL DEFAULT '0' COMMENT '分类编号', `pid` tinyint(4) NOT NULL DEFAULT '1' COMMENT '分类的类型(目前两个 1:应用,2:游戏)', `name` varchar(50) NOT NULL COMMENT '分类名称', PRIMARY KEY (`cat_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='应用分类表'; CREATE TABLE `lmx_apps` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'app编号', `cat_id` int(11) NOT NULL DEFAULT '0' COMMENT '分类编号', `name` varchar(100) NOT NULL COMMENT 'app名称', `year` char(5) NOT NULL DEFAULT '0' COMMENT '年份', `down_count` bigint(20) NOT NULL DEFAULT '0' COMMENT '下载量', `hit_count` bigint(20) DEFAULT '0' COMMENT '搜索量', PRIMARY KEY (`id`), ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
我现在建立了两个联合索引:
但是这个两个索引存在几个问题,如果我点击搜索全部 那么 排序就不会走索引,怎么解决这个搜索分类时选择 全部 不走索引的问题、
走索引的 sql 如下:
-- EXPLAIN SELECT a.id,a.cat_id,a.`name`,a.down_count,b.`name` FROM `lmx_apps` a INNER JOIN `lmx_app_category` `b` ON `a`.`cat_id`=`b`.`id` WHERE a.cat_id = 103 ORDER BY a.down_count DESC LIMIT 10,20
不走索引的 sql 如下
SELECT a.id,a.cat_id,a.`name`,a.down_count,b.`name` FROM `lmx_apps` a INNER JOIN `lmx_app_category` `b` ON `a`.`cat_id`=`b`.`id` -- WHERE -- a.cat_id IN (SELECT cat_id FROM lmx_app_category WHERE orgame = 1) -- 当没有 cat_id 这个条件 或者这个条件为 in 时 ORDER BY a.down_count DESC LIMIT 10,20
Now I think of another plan, which is
Delete the cat_id field of the application table and the index created by cat_id
Create a relationship table between the classification table (lmx_app_category) and the application table (lmx_apps),
The fields are id, app_id, cat_id
Create an index on this related table. I don’t know what this solution looks like