用于查询的文件排序,我不明白为什么(mysql 5)

时间:2015-04-04 14:05:01

标签: mysql

mysql> EXPLAIN SELECT *  FROM 
(`phppos_modules`)  
JOIN `phppos_permissions` ON `phppos_permissions`.`module_id`=`phppos_modules`.`module_id`  
WHERE `phppos_permissions`.`person_id` =  '1'  
ORDER BY `sort` asc;
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
| id | select_type | table              | type   | possible_keys     | key     | key_len | ref                                | rows | Extra          |
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
|  1 | SIMPLE      | phppos_modules     | ALL    | PRIMARY           | NULL    | NULL    | NULL                               |   11 | Using filesort |
|  1 | SIMPLE      | phppos_permissions | eq_ref | PRIMARY,person_id | PRIMARY | 306     | pos.phppos_modules.module_id,const |    1 | Using index    |
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
2 rows in set (0.00 sec)


mysql> show create table phppos_modules;
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table          | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_modules | CREATE TABLE `phppos_modules` (
  `name_lang_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `desc_lang_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `sort` int(10) NOT NULL,
  `icon` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `module_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`module_id`),
  UNIQUE KEY `desc_lang_key` (`desc_lang_key`),
  UNIQUE KEY `name_lang_key` (`name_lang_key`),
  KEY `sort` (`sort`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>     
    mysql> show create table phppos_permissions;
    +--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table              | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_permissions | CREATE TABLE `phppos_permissions` (
  `module_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `person_id` int(10) NOT NULL,
  PRIMARY KEY (`module_id`,`person_id`),
  KEY `person_id` (`person_id`),
  CONSTRAINT `phppos_permissions_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `phppos_employees` (`person_id`),
  CONSTRAINT `phppos_permissions_ibfk_2` FOREIGN KEY (`module_id`) REFERENCES `phppos_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

1 个答案:

答案 0 :(得分:1)

您可能会受到documentation

的影响
  

用于获取行的密钥与使用的密钥不同   订购:

SELECT * FROM t1 WHERE key2=constant ORDER BY key1;

尝试以下方法:

ALTER TABLE `phppos_modules`
ADD INDEX `all_w_sort_first` (`sort`,`module_id`,`name_lang_key`,`desc_lang_key`,`icon`);