Mat*_*ick 9 mysql performance execution-plan view
我有一个包含多个视图的模式。我需要检查执行计划以确保适当的索引到位并被使用。
我该怎么做呢?
我宁愿不必将输出复制并粘贴show create view <viewname>
到 中explain
,尤其是当某些视图构建在其他视图之上时,这将非常痛苦。
这将为所有视图生成 EXPLAIN
mysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
这将为 mydb 数据库中的所有视图生成 EXPLAIN
mysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views where table_schema = 'mydb'" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
试一试 !!!
@MattFenwick,你的答案比我的简单得多。这是我在运行 MySQL 5.5.12 的 PC 上尝试的示例。我在您的答案中的 SELECT 版本和从我的答案中生成的 EXPLAIN 上都运行了 EXPLAIN:
mysql> explain select * from bigjoin;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
| 1 | SIMPLE | k | index | NULL | PRIMARY | 4 | NULL | 14 | Using index |
| 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | test.k.id_key | 1 | Using index |
| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 4 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
3 rows in set (0.00 sec)
mysql> explain select `a`.`id_key` AS `id_key1`,`b`.`id_key` AS `id_key2` from ((`test`.`idlist` `k` left join `test`.`id_key_table` `a` on((`k`.`id_key` = `a`.`id_key`))) left join `test`.`new_keys_to_load` `b` on((`k`.`id_key` = `b`.`id_key`)));
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
| 1 | SIMPLE | k | index | NULL | PRIMARY | 4 | NULL | 14 | Using index |
| 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | test.k.id_key | 1 | Using index |
| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 4 | |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
3 rows in set (0.00 sec)
mysql>
Run Code Online (Sandbox Code Playgroud)
他们都制定了相同的 EXPLAIN 计划。我会改变我的答案来实现你的方式。尽管为简单起见是+2,但您从我这里得到了+1。你应该继续接受你自己的答案。
这是一个关于 MySQL 中的 VIEW 的有趣事实:一个视图在 information_schema 数据库中的两个位置表示
这将为所有视图生成 EXPLAIN
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.tables WHERE engine IS NULL" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
或者
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.views" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
这将为 mydb 数据库中的所有视图生成 EXPLAIN
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.tables WHERE table_schema='mydb' AND engine IS NULL;" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
或者
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.views WHERE table_schema='mydb';" > /root/ExplainViews.sql
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试的:
mysql> explain view_name;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| field1 | varchar(3) | YES | | NULL | |
| field2 | varchar(3) | YES | | NULL | |
| field3 | bigint(21) | NO | | 0 | |
| field4 | bigint(21) | NO | | 0 | |
+---------+------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)
显然这行不通——这和做describe view_name
.
但是,select * from view_name
似乎有效:
mysql> explain select * from view_name;
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | PRIMARY | <derived5> | ALL | NULL | NULL | NULL | NULL | 18 | |
| 1 | PRIMARY | <derived3> | ALL | NULL | NULL | NULL | NULL | 105 | Using where; Using join buffer |
| 5 | DERIVED | <derived6> | ALL | NULL | NULL | NULL | NULL | 68 | Using temporary; Using filesort |
| 6 | DERIVED | basetable | ALL | NULL | NULL | NULL | NULL | 928 | Using temporary; Using filesort |
| 3 | DERIVED | <derived4> | ALL | NULL | NULL | NULL | NULL | 386 | Using temporary; Using filesort |
| 4 | DERIVED | basetable | ALL | NULL | NULL | NULL | NULL | 928 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10316 次 |
最近记录: |