Mysql – Execution plan not understanding correctly

execution-planMySQLmysql-workbenchperformancequery-performance

I am running a query:

SELECT * FROM message where fr_user = 1 ORDER BY created DESC LIMIT 10;

Without the LIMIT there would be 170 rows returned where fr_user = 1;

I am now trying to understand how efficient this query (not trying to improve the query, just to see how efficient it CURRENTLY is) is.

I have ran the query and looked at the execution plan:

  • In the execution plan there is a query cost of 43.00… what does this mean/how is it calculated?

  • What do the four fields (Read, Eval, Prefix, DataRead) mean?

  • Why does the ratio of rows examined to the ratio of rows produced = 100% when there are 170 rows examined and 10 rows produced? I have an index (fr_user, created) so why are all the rows (170) being examined? If I have to examine all the rows it would seem pointless to have an index then?

enter image description here

Best Answer

(To answer @Michael, then segue into the OP's question.) ORDER BY fr_user DESC, created DESC can use an index backwards if all are DESC. Or, as in this orginal case, it can do WHERE fr_user = constant ORDER BY created DESC when you have INDEX(fr_user, created). It simply goes to the end of the fr_user=1 entries in the index and walks backward.

But your question is "why are all 170 rows examined". Well, they aren't. The "170" is bogus because EXPLAIN almost always ignores LIMIT. This is a known failing of EXPLAIN. The Optimizer, in some situations, does take LIMIT into consideration.