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 ?
All you can expect from the query optimizer its a best effort approach, so it wont necessarilly find the best solution, rather the best solution it can find in a certain period of time. If you want to imrpove this, you need to create an index that improve your query or change the design of your database, the query optimizer will see that and it will choose it as a better query plan.