0

Recently when I come across some slow queries, I have seen this "Using sort_union(sourceClass,destinationClass)" phrase in the EXPLAIN plan.

When I run the EXPLAIN with the same query with FORCE INDEX of each index separately, I saw it is doing almost the full table scan due to data distribution.

I understood that this is to improve the performance and this decision is taken by the query optimizer only.

In my query the keys and query are like this

sourceClass:      KEY (`sourceClass`,`sourceId`,`kind`)
destinationClass: KEY (`destinationClass`,`destinationId`,`kind`),

query: 
WHERE (sourceClass='channel' && sourceId=1016) ||  (destinationClass='channel' 
&& destinationId=1016) 

I would like to know at what when the optimizer comes to choose this and are there any disadvantages due to this kind of finding results ?

4

1 回答 1

1

您可以从查询优化器中得到所有期望,它是一种尽力而为的方法,因此它不一定会找到最佳解决方案,而是它可以在特定时间段内找到的最佳解决方案。如果你想改进这一点,你需要创建一个索引来改进你的查询或改变你的数据库的设计,查询优化器会看到它,它会选择它作为一个更好的查询计划。

于 2012-05-29T14:16:10.660 回答