MySQL:使用两个键,一个用于连接,一个用于排序?
我在一个键控字段上有一个简单的where子句,在不同字段上的两个表之间有一个连接,然后在第二个表的另一个字段上有一个排序。我所有的东西都有密钥,但似乎描述给了我一个文件排序和30秒的查询执行:MySQL:使用两个键,一个用于连接,一个用于排序?,sql,mysql,join,foreign-keys,Sql,Mysql,Join,Foreign Keys,我在一个键控字段上有一个简单的where子句,在不同字段上的两个表之间有一个连接,然后在第二个表的另一个字段上有一个排序。我所有的东西都有密钥,但似乎描述给了我一个文件排序和30秒的查询执行: mysql> describe SELECT * FROM `alias` LEFT OUTER JOIN `aliaspoint` ON (`alias`.`id` = `aliaspoint`.`alias_id`) WHERE `alias`.`type_id` = 9 ORDER BY
mysql> describe SELECT * FROM `alias` LEFT OUTER JOIN `aliaspoint` ON (`alias`.`id` = `aliaspoint`.`alias_id`) WHERE `alias`.`type_id` = 9 ORDER BY `aliaspoint`.`points` DESC LIMIT 100 OFFSET 200;
+----+-------------+---------------------+--------+------------------------+------------------------+---------+------------------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------------+--------+------------------------+------------------------+---------+------------------------+--------+---------------------------------+
| 1 | SIMPLE | metaward_alias | ref | metaward_alias_type_id | metaward_alias_type_id | 4 | const | 356710 | Using temporary; Using filesort |
| 1 | SIMPLE | metaward_aliaspoint | eq_ref | alias_id | alias_id | 4 | paul.metaward_alias.id | 1 | |
+----+-------------+---------------------+--------+------------------------+------------------------+---------+------------------------+--------+---------------------------------+
2 rows in set (7.52 sec)
为什么不使用orderby上的点
上的别名点
键
表:
mysql> show create table aliaspoint;
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| aliaspoint | CREATE TABLE `aliaspoint` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`alias_id` int(11) NOT NULL,
`points` double DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `alias_id` (`alias_id`),
KEY `aliaspoint_points` (`points`)
) ENGINE=MyISAM AUTO_INCREMENT=1014683 DEFAULT CHARSET=latin1 |
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
我想你说的“键”是指索引列
一般来说,RDBMS将使用一个索引和一个索引来访问一个表
乐观主义者显然决定使用一个索引来过滤掉“where=”
然后对剩余输出进行排序。它可以使用order by column上的索引来获得正确序列中的所有行,然后过滤掉其他行,但这会破坏连接
如果你仔细想想,你就会发现做其他事情是困难的。一旦您在结果集中选择了一行,索引就毫无用处了,因为它指向磁盘上的一行
您可以尝试将所有三列放在一个索引中,这可能会启用更多的技巧
为什么不在orderby的点上使用aliaspoint键
因为索引只用于数据检索。需要排序的是返回的行