"使用sort_union"是什么意思?

ajr*_*eal 3 mysql sql-execution-plan

(不确定这是否已经讨论过......)

在为类别构建SQL时

示例类别数据:

ID | NAME
--------------------------
1  | hardware
2  | hardware : notebooks
3  | hardware : phones
4  | hardware : desktops
5  | review
6  | review : photo gallery
Run Code Online (Sandbox Code Playgroud)

hardware 将是的父类别 hardware : *

当我考虑构建api来查询数据库时

desc select * from category 
where id=1 or 
name like concat((select name from category where id in(1)), '_%');
Run Code Online (Sandbox Code Playgroud)

上述退货(执行计划中)

Using sort_union(PRIMARY,some_index); Using where
Run Code Online (Sandbox Code Playgroud)

有什么sort_union办法?
好吗?坏?要避免?


(理解我可以将整个查询重写为更优化的方式,
但是我有一些限制来重新构建整个api)


mysql版本5.1

小智 5

如果你有一个where子句,你在其中使用两个或更多个OR,则使用sort_union,MySQL认为它比使用index_merge更好.

有关更多信息,请参阅MySQL文档

简而言之,它并不坏(imho); 因为它会在排序联合它们之前首先从两个索引中获取行.