如何优化下面的mysql查询

如何优化下面的mysql查询,mysql,sqlperformance,Mysql,Sqlperformance,有人可以调整此查询以获得更好的性能吗 SELECT `Vocabulary`.`id`, `Vocabulary`.`title`, `Vocabulary`.`alias`, `Vocabulary`.`description`, `Vocabulary`.`required`, `Vocabulary`.`multiple`, `Vocabulary`.`tags`, `Vocabulary`.`plugin`, `Vocabulary`.`weight`, `Vocabulary`.`up

有人可以调整此查询以获得更好的性能吗

SELECT `Vocabulary`.`id`, `Vocabulary`.`title`, `Vocabulary`.`alias`, `Vocabulary`.`description`, `Vocabulary`.`required`, `Vocabulary`.`multiple`, `Vocabulary`.`tags`, `Vocabulary`.`plugin`, `Vocabulary`.`weight`, `Vocabulary`.`updated`, `Vocabulary`.`created`, `TypesVocabulary`.`id`, `TypesVocabulary`.`type_id`, `TypesVocabulary`.`vocabulary_id`, `TypesVocabulary`.`weight` FROM `epowerg`.`vocabularies` AS `Vocabulary` JOIN `epowerg`.`types_vocabularies` AS `TypesVocabulary` ON (`TypesVocabulary`.`type_id` IN (1, 2, 4) AND `TypesVocabulary`.`vocabulary_id` = `Vocabulary`.`id`) ORDER BY `Vocabulary`.`weight` ASC; 选择“词汇表”。“id”, `词汇“title”, `词汇“别名”, `词汇“描述”, `词汇“必选”, `词汇“多重”, `词汇`.`tags`, `词汇`.`plugin`, `词汇“重量”, `词汇“更新”, `词汇“created”, `typesvobableary`.`id`, `类型可撤销性`.`type\u id`, `类型可撤销性`.`词汇表'u id`, `类型可撤销的`.`weight` 从'epowerg'。'vocabularies'作为'vocabularies' 将'epowerg`.'types\u词汇表'加入为'types撤销性' ON(`typesvobaculary`.`type_id`IN(1,2,4) 和'typesvobaculary'。'vocability_id`='vocability`.'id`) 按“词汇表”排序。'weight'ASC; 查询生成的结果:

+----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ | 1 | SIMPLE | Vocabulary | ALL | PRIMARY | NULL | NULL | NULL | 2 | Using temporary; Using filesort | | 1 | SIMPLE | TypesVocabulary | ALL | NULL | NULL | NULL | NULL | 4 | Using where; Using join buffer | +----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ +----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ |id |选择|类型|类型|可能的|键|键|列|参考|行|额外| +----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ |1 |简单|词汇|所有|主要|空|空|空| 2 |使用临时词汇;使用文件排序| |1 |简单|类型可撤销| ALL | NULL | NULL | NULL | NULL | 4 |使用where;使用连接缓冲区| +----+-------------+-----------------+------+---------------+------+---------+------+------+---------------------------------+ 提前谢谢
普拉巴卡兰。R

以下是您的查询结构:

select v.*, tv.*
from `epowerg`.`vocabularies` v JOIN
     `epowerg`.`types_vocabularies` tv
     ON tv.type_id IN (1, 2, 4) AND      
        tv.vocabulary_id = v.id    
ORDER BY v.weight ASC; 
此查询基本上有两种可能的执行计划:首先读取
v
,然后在
tv
中查找值。或者读取
tv
并在
v
中查找值

首先,您需要
词汇表(权重,id)
类型词汇表(词汇表,类型,id)
上的索引。如果这起作用,那么它将消除排序

第二种尝试方法是首先对类型进行过滤。这方面的索引是:
types\u词汇表(type\u id,词汇表id)
vocabularies(id)


哪种方法更好取决于数据的性质。但是首先尝试第一种方法,因为消除排序通常是一个好主意。

以什么方式优化?您是否有性能方面的原因?向
type\u id添加索引
为什么您认为它需要调整?看起来不错。如果你想要一个好的回复,请发布这两个表的预期表大小