· 8 years ago · Mar 19, 2018, 11:30 AM
1SELECT DISTINCT i.`id`, i.`name`
2FROM `p_items` as i, `p_cats_sync` as c
3WHERE c.`id` = i.`id` AND c.`cat_id` IN (109,152,153,156,157,158,161,163,166,140,266) AND c.`status` = 1 AND c.`p_status` = 1
4ORDER BY i.`status` DESC, i.`views` DESC
5LIMIT 40, 10;
6
7id select_type table type possible_keys key key_len ref rows Extra
81 SIMPLE c range PRIMARY,id PRIMARY 4 NULL 412 Using where; Using index; Using temporary; Using f...
91 SIMPLE i ref PRIMARY PRIMARY 3 c.id 1
10
11CREATE TABLE IF NOT EXISTS `p_items` (
12 `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
13 `name` varchar(200) NOT NULL,
14 `status` tinyint(1) unsigned NOT NULL,
15 `views` smallint(5) unsigned NOT NULL,
16 PRIMARY KEY (`id`,`status`,`views`),
17 UNIQUE KEY `status` (`status`,`views`,`id`),
18 KEY `views` (`views`)
19) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2594 ;
20
21CREATE TABLE IF NOT EXISTS `p_cats_sync` (
22 `id` mediumint(8) unsigned NOT NULL COMMENT '',
23 `cat_id` smallint(5) unsigned NOT NULL COMMENT '',
24 `status` tinyint(1) unsigned NOT NULL COMMENT '',
25 `p_status` tinyint(1) NOT NULL COMMENT '',
26 PRIMARY KEY (`cat_id`,`status`,`p_status`,`id`),
27 KEY `id` (`id`)
28) ENGINE=MyISAM DEFAULT CHARSET=utf8;
29
30EXPLAIN SELECT DISTINCT c.`id`
31FROM `p_cats_sync` AS c
32WHERE c.`cat_id`
33IN ( 109, 152, 153, 156, 157, 158, 161, 163, 166, 140, 266 )
34AND c.`status` =1
35AND c.`p_status` =1
36
371 SIMPLE c range PRIMARY PRIMARY 4 NULL 412 Using where; Using index; Using temporary