


Why is the filtered column value in Mysql explain extended always 100%_PHP tutorial
Why the filtered column value in Mysql explain extended is always 100%
1. Problem
The output of executing Mysql explain extended will have one more filtered column than the simple explain( By default, MySQL 5.7 will output filtered), which refers to the percentage of rows that return results to the rows that need to be read (the value of the rows column). It is said that filtered is a very useful value, because for join operations, the result set size of the previous table directly affects the number of loops. But the result of the test in my environment is that the value of filtered is always 100%, which means it has lost its meaning.Refer to the MySQL 5.6 code below. The filtered value is only valid for index and all scans (this is understandable. In other situations, the rows value is usually equal to the estimated result set size.).
sql/opt_explain.cc
- bool Explain_join::explain_rows_and_filtered()
- {
- if (table->pos_in_table_list- >schema_table)
- return false;
- double examined_rows;
- if (select && select ->quick)
- examined_rows= rows2double(select->quick->records);
- else if (tab->type == JT_INDEX_SCAN || tab-> type == JT_ALL)
- {
- if (tab->limit)
- examined_rows= rows2double(tab->limit);
- else
- {
- table->pos_in_table_list->fetch_number_of_rows();
- examined_rows= rows2double(table->file->stats.records);
- }
- }
- else
- examined_rows= tab->position->records_read;
- fmt->entry()->col_rows.set(static_cast
(examined_rows)) ; - /* Add "filtered" field */
- if (describe(DESCRIBE_EXTENDED))
- {
- float f= 0.0;
- if (examined_rows)
- f= 100.0 * tab->position-> records_read / examined_rows;
- fmt->entry()->col_filtered.set(f);
- }
- return false;
- }
However, after I constructed a full table scan, the filtered result was wrong, it was still 100%, and what I expected was 0.1 %.
- mysql> desc tb2;
------- -------------- ------ ----- - -------- -------
| Field | Type | Null | Key | Default | Extra |
------- --------- ----- ------ ----- --------- -------
| id | int(11) | NO | PRI | 0 | |
| c1 | int(11) | YES | | NULL | |
| c2 | varchar(100) | YES | | NULL | |
------- ------ -------- ------ ----- --------- -------
3 rows in set (0.00 sec)
mysql> explain extended select * from tb2 where c1 ---- ------------- ------- ------ -- ------------- ------ --------- ------ -------- -------- ---------------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- -- ----------- ------- ------ --------------- ------ ----- ---- ------ -------- ---------- -------------
| 1 | SIMPLE | tb2 | ALL | NULL | NULL | NULL | NULL | 996355 | 100.00 | Using where |
---- ------------- ------- ----- - --------------- ------ --------- ------ -------- ----- ----- -------------
1 row in set, 1 warning (10 min 29.96 sec)
mysql> select count(*) from tb2 where c1 ----------
| count(*) |
----------
| 1001 |
-- --------
1 row in set (1.99 sec)
Through gdb tracking, I found that the branch taken by the code is correct. But there is something wrong with the value below.
- (gdb) p table->file->stats.records
- $18 = 996355
- (gdb) p tab-> position->records_read
- $19 = 996355
2. Reasons
Why does the above situation occur? Later, I checked the statistical information collected by MySQL and understood.MySQL, like other mainstream databases, automatically needs to collect statistical information in order to generate better execution plans. You can also use analyze table to collect it manually. The collected statistical information is stored in mysql.innodb_table_stats and mysql.innodb_index_stats.
Reference: http://dev.mysql.com/doc/refman/5.6/en/innodb-persistent-stats.html#innodb-persistent-stats-tables
But that’s not the point, the point Yes, looking at these two tables you will find that MySQL collects very little statistics.
- mysql> select * from mysql.innodb_table_stats where table_name='tb2';
--------------- -------- ---- --------------------- -------- ----------------- -------------------------------
| database_name | table_name | last_update | n_rows | clustered_index_size | sum_of_other_index_sizes |
--------------- ------------ --------------------- -------- ---------------------- --------------------------
| test | tb2 | 2015-12-02 06:26:54 | 996355 | 3877 | 0 |
--------------- ------------ --------------------- -------- ---------------------- --------------------------
1 row in set (0.00 sec)
mysql> select * from mysql.innodb_index_stats where table_name='tb2';
--------------- ------------ ------------ --------------------- -------------- ------------ ------------- -----------------------------------
| database_name | table_name | index_name | last_update | stat_name | stat_value | sample_size | stat_description |
--------------- ------------ ------------ --------------------- -------------- ------------ ------------- -----------------------------------
| test | tb2 | PRIMARY | 2015-12-02 06:26:54 | n_diff_pfx01 | 996355 | 20 | id |
| test | tb2 | PRIMARY | 2015-12-02 06:26:54 | n_leaf_pages | 3841 | NULL | Number of leaf pages in the index |
| test | tb2 | PRIMARY | 2015-12-02 06:26:54 | size | 3877 | NULL | Number of pages in the index |
--------------- ------------ ------------ --------------------- -------------- ------------ ------------- -----------------------------------
3 rows in set (0.00 sec)
3. 引申
后面我联系到MySQL匮乏的统计信息会带来什么后果?不难想象,如果缺少索引,MySQL很可能会生成性能糟糕的执行计划,比如搞错大表和小表的join顺序,就像下面这样。
- mysql> explain extended select count(*) from tb1,tb2 where tb1.c1=tb2.c1 and tb2.c2='xx';
---- ------------- ------- ------ --------------- ------ --------- ------ -------- ---------- ----------------------------------------------------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- ------------- ------- ------ --------------- ------ --------- ------ -------- ---------- ----------------------------------------------------
| 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | NULL |
| 1 | SIMPLE | tb2 | ALL | NULL | NULL | NULL | NULL | 996355 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
---- ------------- ------- ------ --------------- ------ --------- ------ -------- ---------- ----------------------------------------------------
2 rows in set, 1 warning (0.00 sec)
相同的查询,PostgreSQL给出的执行计划是更好的,先扫描t2表再循环扫描t1表。
- postgres=# explain select count(*) from tb1,tb2 where tb1.c1=tb2.c1 and tb2.c2='xx';
- QUERY PLAN
- -------------------------------------------------------------------
- Aggregate (cost=20865.50..20865.51 rows=1 width=0)
- -> Nested Loop (cost=0.00..20865.50 rows=1 width=0)
- Join Filter: (tb1.c1 = tb2.c1)
- -> Seq Scan on tb2 (cost=0.00..20834.00 rows=1 width=4)
- Filter: ((c2)::text = 'xx'::text)
- -> Seq Scan on tb1 (cost=0.00..19.00 rows=1000 width=4)
- (6 rows)
MySQL花了0.34s
- mysql> select count(*) from tb1,tb2 where tb1.c1=tb2.c1 and tb2.c2='xx';
- ----------
- | count(*) |
- ----------
- | 0 |
- ----------
- 1 row in set (0.34 sec)
PostgreSQL花了0.139s
- postgres=# select count(*) from tb1,tb2 where tb1.c1=tb2.c1 and tb2.c2='xx';
- count
- -------
- 0
- (1 row)
- Time: 139.600 ms
The performance difference in the above example is actually not very big. If the condition of tb2.c2='xx' is removed, the difference will be very big.
Mysql took 1 minute and 8 seconds
- mysql> explain select count(*) from tb1,tb2 where tb1.c1=tb2.c1;
---- --- ---------- ------- ------ --------------- ------ ------ --- ------ -------- ---------------------------------- ------------------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---- ------------- ------- ------ --------------- ------ --- ------ ------ -------- ------------------------------- -----------------------
| 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 1000 | NULL |
| 1 | SIMPLE | tb2 | ALL | NULL | NULL | NULL | NULL | 996355 | Using where; Using join buffer (Block Nested Loop) |
---- ------------- ------- ------ --------------- ------ --------- ------ - ------------------------------------------------------- ---------
2 rows in set (0.00 sec)
mysql> select count(*) from tb1,tb2 where tb1.c1=tb2.c1;
- ---------
| count(*) |
----------
| 9949 |
----------
1 row in set (1 min 8.26 sec)
PostgreSQL only took 0.163 seconds
- postgres=# explain select count( *) from tb1,tb2 where tb1.c1=tb2.c1;
- QUERY PLAN
- ---------------- -------------------------------------------------- --------
- Aggregate (cost=23502.34..23502.35 rows=1 width=0)
- -> Hash Join (cost=31.50. .23474.97 rows=10947 width=0)
- Hash Cond: (tb2.c1 = tb1.c1)
- -> Seq Scan on tb2 (cost=0.00. .18334.00 rows=1000000 width=4)
- -> Hash (cost=19.00..19.00 rows=1000 width=4)
- -> Seq Scan on tb1 (cost=0.00..19.00 rows=1000 width=4)
- (6 rows)
- Time: 0.690 ms
- postgres=# select count(*) from tb1,tb2 where tb1.c1=tb2.c1;
- count
- -- -----
- 10068
- (1 row)
- Time: 163.868 ms
However, this performance difference has nothing to do with statistical information. The reason is that PG supports Nest Loop Join, Merge Join and Hash Join, while MySQL only supports Nest Loop Join. Without the index, Nest Loop Join will As slow as a turtle.
4. Summary
1. MySQL has very little statistical information, only the number of table rows and the number of unique values of index columns, which makes the MySQL optimizer often unable to have a correct estimate of the data size. understanding and give an execution plan with poor performance.2. The efficiency of MySQL's join operation is very dependent on the index (the two previous times I helped people tune MySQL's SQL statements were all indexes). It's not that PG's join does not require indexes, but it is not as big a reaction as MySQL's lack of indexes. In the above example, MySQL took more than 1 minute to execute. After adding the index, the execution time of both MySQL and PG immediately dropped to less than 10 milliseconds. Therefore, developers should evaluate the possible query methods when designing tables and build all the indexes that should be built (not less or more).
3. In contrast, PG not only counts the value distribution of all columns, but also includes histograms, frequent values and other information in addition to unique values, which supports the PG optimizer to make correct decisions. It is speculated that for this reason, the PG community believes that PG's optimizer is smart enough and there is no need to add hint functions similar to Oracle to the PG kernel (because hints may be abused by people, making the system difficult to maintain; however, in fact, If you want to use it, you can install the pg_hint_plan plug-in yourself).

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
