Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL EXPLAIN EXTENDED filtered column (obviously it's not a percentage)

Tags:

mysql

explain

I've been searching for this and they all state some sort of percentage, explain this:

EXPLAIN EXTENDED SELECT * FROM PageAccess ORDER BY AccessId DESC LIMIT 20;
SELECT COUNT(*) FROM PageAccess;

Giving:

id, select_type, table, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, 'SIMPLE', 'PageAccess', 'index', '', 'PRIMARY', '4', '', 20, 9295.00, ''

(Yes, filtered = 9295.00)

and:

1830

For count(*)

Yes I want the last 20 rows, AccessId is the auto-incremented primary key.

What does 9295 mean!?

like image 864
Alec Teal Avatar asked Apr 09 '14 17:04

Alec Teal


People also ask

What does the filtered column mean in MySQL?

The MySQL 5.7 documentation states: The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables.

What is the filtered column when using the extended keyword?

MySQL 5.6 Reference Manual / ... / When EXPLAIN is used with the EXTENDED keyword, the output includes a filtered column not otherwise displayed. This column indicates the estimated percentage of table rows that are filtered by the table condition.

What is the filtered column in the explain output?

/ When EXPLAIN is used with the EXTENDED keyword, the output includes a filtered column not otherwise displayed. This column indicates the estimated percentage of table rows that are filtered by the table condition.

What is the difference between rows and filtered columns?

The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables.


1 Answers

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

The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables. This column is displayed if you use EXPLAIN EXTENDED.

In the filtered 100% means that all rows from this table are filtered. So getting higher value is not worrisome since its a good sign, means it does not have to read as much data from the table.

This is how its calculated. Say I have a table called users Lets run some analysis on it.

mysql> select count(*) from users ;
+----------+
| count(*) |
+----------+
|    79309 |
+----------+

You can see there are 79309 rows in the table. Now lets run explain

mysql> explain extended select * from users order by idusers desc limit 20 ;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | filtered  | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+
|  1 | SIMPLE      | users | index | NULL          | PRIMARY | 4       | NULL |   20 | 396545.00 |       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+

Now wondering why filtered = 396545.00

Well the calculation is simple.

The table has total rows = 79309 say its tx.

The explain shows rows = 20 say its tx1.

The filtered is calculated as

(tx / tx1)*100 = 396545.00

So if this value is high means the query is good and not reading everything from the table.

So not to confuse this is not the number of rows that the query will look its a relative calculation of % over number of rows available to the number of rows fetched.

If it becomes 100 meaning the query is looking all the available rows in the table.

like image 190
Abhik Chakraborty Avatar answered Oct 11 '22 05:10

Abhik Chakraborty