Question

I'm trying to make sense of the EXPLAIN result for the query below. I get rows=7 (The total size of the table) but the actual query only returns 1 row. Does rows=7 on the explain result imply MySQL checked every row still to get this result?

 EXPLAIN SELECT conversation_id FROM messages 
    WHERE to_id = '355' AND timestamp < '1376856934' LIMIT 1;

Only 4 rows have to_id='355'and the columns to_id and timestamp are both indexed together so I can't see why it would have to scan the entire table just to get 1 row.

Était-ce utile?

La solution

The "rows" column in the EXPLAIN output is an estimate of the number of rows that need to be examined.

Whether or not the query will actually need to examine every row in the table really depends on the access plan (shown in other columns in the EXPLAIN output. For example, an index range scan operation doesn't have to look at EVERY row, only the rows in particular index range. But a full scan operation will look at EVERY row in the table.

Your question cannot be answered based on the value returned in the "rows" column; we'd really need to see the output from EXPLAIN to determine whether MySQL is using an index range scan or not.

http://dev.mysql.com/doc/refman/5.5/en/explain-output.html

Autres conseils

From MySQL documentation:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.

EXPLAIN describes how result is collected, LIMIT is only restricts which rows should be returned from the previously collected result.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top