最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文帶你理解MySql中explain結果filtered

 更新時間:2022年09月09日 14:55:00   作者:xszhaobo  
使用EXPLAIN關鍵字可以模擬優(yōu)化器執(zhí)行SQL查詢語句,從而知道MySQL是如何處理你的SQL語句的,下面這篇文章主要給大家介紹了關于MySql中explain結果filtered的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

MySql explain語句的返回結果中,filtered字段要怎么理解?

MySql5.7官方文檔中描述如下:

The filtered column indicates an estimated percentage of table rows filtered by the table condition. The maximum value is 100, which means no filtering of rows occurred. Values decreasing from 100 indicate increasing amounts of filtering. rows shows the estimated number of rows examined and rows × filtered shows the number of rows joined with the following table. For example, if rows is 1000 and filtered is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500.

這段文字不是很好理解,舉例來說,有如下三個查詢語句的explain結果,針對b和c表的顯示filtered是100,而針對a表的顯示是18。

+-------------+-------+--------+---------+---------+------+----------+
| select_type | table | type   | key     | key_len | rows | filtered |
+-------------+-------+--------+---------+---------+------+----------+
| PRIMARY     | a     | range  | search  | 4       |  174 |   18.00  |
| PRIMARY     | b     | eq_ref | PRIMARY | 4       |    1 |   100.00 |
| PRIMARY     | c     | ALL    | PRIMARY | 4       |    1 |   100.00 |

我們可以怎么理解filtered的值呢?從filtered的值中得出什么結論呢?到底是100更好還是18更好?

首先,這里的filtered表示通過查詢條件獲取的最終記錄行數(shù)占通過type字段指明的搜索方式搜索出來的記錄行數(shù)的百分比。

以上圖的第一條語句為例,MySQL首先使用索引(這里的type是range)掃描表a,預計會得到174條記錄,也就是rows列展示的記錄數(shù)。接下來MySql會使用額外的查詢條件對這174行記錄做二次過濾,最終得到符合查詢語句的32條記錄,也就是174條記錄的18%。而18%就是filtered的值。

更完美的情況下,應該是使用某個索引,直接搜索出32條記錄并且過濾掉另外82%的記錄。

因此一個比較低filtered值表示需要有一個更好的索引,假如type=all,表示以全表掃描的方式得到1000條記錄,且filtered=0.1%,表示只有1條記錄是符合搜索條件的。此時如果加一個索引可以直接搜出來1條數(shù)據(jù),那么filtered就可以提升到100%。

由此可見,filtered=100%確實是要比18%要好。

當然,filtered不是萬能的,關注執(zhí)行計劃結果中其他列的值并優(yōu)化查詢更重要。比如為了避免出現(xiàn)filesort(使用可以滿足order by的索引),即使filtered的值比較低也沒問題。再比如上面filtered=0.1%的場景,我們更應該關注的是添加一個索引提高查詢性能,而不是看filtered的值。

參考內容:

MySQL :: MySQL 5.7 Reference Manual :: 8.8.2 EXPLAIN Output Format

innodb - What is the meaning of filtered in MySQL explain? - Database Administrators Stack Exchange

總結 

到此這篇關于MySql中explain結果filtered的文章就介紹到這了,更多相關MySql中explain結果filtered內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

谷城县| 中方县| 乳源| 洞口县| 永嘉县| 安阳县| 财经| 淮阳县| 香港 | 泗水县| 乌拉特后旗| 五指山市| 香港| 磐石市| 三门峡市| 伊川县| 德昌县| 买车| 乌鲁木齐市| 安福县| 唐山市| 克东县| 金昌市| 保康县| 灌南县| 交口县| 泸定县| 闵行区| 庄浪县| 芦山县| 嘉荫县| 广汉市| 徐州市| 遵义县| 如东县| 嘉峪关市| 青阳县| 石嘴山市| 嵊州市| 行唐县| 枣阳市|