snippetsqlMinor
How do I get the execution plan for a view?
Viewed 0 times
theviewplangetforhowexecution
Problem
I have a schema with a number of views. I need to check the execution plans to make sure the appropriate indexes are in place and being used.
How do I do this?
I'd rather not have to copy and paste the output from
How do I do this?
I'd rather not have to copy and paste the output from
show create view into explain, especially as some of the views are built on top of other views and this would be quite a pain.Solution
Use the information_schema.views table
This will generate the EXPLAIN for all views
This will generate the EXPLAIN for all views in the mydb database
Give it a Try !!!
UPDATE 2012-03-22 11:30 EDT
@MattFenwick, your answer is a whole lot simpler than mine. Here is an example I tried out on my PC running MySQL 5.5.12. I ran EXPLAIN on both the SELECT version from your answer and the EXPLAIN generated from my answer:
They both produced the same EXPLAIN plan. I will change my answer to implement your way. You get a +1 from me although it's +2 for simplicity. You should go ahead and accept your own answer on this one.
Here is an interesting factoid about VIEWs in MySQL : A view is represented in two places in the information_schema database
This will generate the EXPLAIN for all views
or
This will generate the EXPLAIN for all views in the mydb database
or
This will generate the EXPLAIN for all views
mysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views" > /root/ExplainViews.sqlThis will generate the EXPLAIN for all views in the mydb database
mysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views where table_schema = 'mydb'" > /root/ExplainViews.sqlGive it a Try !!!
UPDATE 2012-03-22 11:30 EDT
@MattFenwick, your answer is a whole lot simpler than mine. Here is an example I tried out on my PC running MySQL 5.5.12. I ran EXPLAIN on both the SELECT version from your answer and the EXPLAIN generated from my answer:
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>They both produced the same EXPLAIN plan. I will change my answer to implement your way. You get a +1 from me although it's +2 for simplicity. You should go ahead and accept your own answer on this one.
Here is an interesting factoid about VIEWs in MySQL : A view is represented in two places in the information_schema database
- information_schema.views : table_schema,table_name identify, and view_definition
- information_schema.tables : table_schema,table_name where ENGINE is NULL
This will generate the EXPLAIN for all views
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.tables WHERE engine IS NULL" > /root/ExplainViews.sqlor
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.views" > /root/ExplainViews.sqlThis will generate the EXPLAIN for all views in the mydb database
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.sqlor
mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.views WHERE table_schema='mydb';" > /root/ExplainViews.sqlCode Snippets
mysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views" > /root/ExplainViews.sqlmysql -uroot -p -AN -e"select concat('explain ',view_definition) from information_schema.views where table_schema = 'mydb'" > /root/ExplainViews.sqlmysql> 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>mysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.tables WHERE engine IS NULL" > /root/ExplainViews.sqlmysql -uroot -p -AN -e"select concat('explain select * from ',table_schema,'.',table_name,';') from information_schema.views" > /root/ExplainViews.sqlContext
StackExchange Database Administrators Q#15371, answer score: 8
Revisions (0)
No revisions yet.