mysql视图效率测试

mysql中的视图不太智能,稍微复杂些的视图,不能展开,下面是测试:

CREATE ALGORITHM=UNDEFINED DEFINER=`myadmin`@`` SQL SECURITY DEFINER VIEW `v_big_table` AS select `big_table`.`TABLE_CATALOG` AS `TABLE_CATALOG`,`big_table`.`TABLE_SCHEMA` AS `TABLE_SCHEMA`,`big_table`.`TABLE_NAME` AS `TABLE_NAME`,`big_table`.`COLUMN_NAME` AS `COLUMN_NAME`,`big_table`.`ORDINAL_POSITION` AS `ORDINAL_POSITION`,`big_table`.`COLUMN_DEFAULT` AS `COLUMN_DEFAULT`,`big_table`.`IS_NULLABLE` AS `IS_NULLABLE`,`big_table`.`DATA_TYPE` AS `DATA_TYPE`,`big_table`.`CHARACTER_MAXIMUM_LENGTH` AS `CHARACTER_MAXIMUM_LENGTH`,`big_table`.`CHARACTER_OCTET_LENGTH` AS `CHARACTER_OCTET_LENGTH`,`big_table`.`NUMERIC_PRECISION` AS `NUMERIC_PRECISION`,`big_table`.`NUMERIC_SCALE` AS `NUMERIC_SCALE`,`big_table`.`DATETIME_PRECISION` AS `DATETIME_PRECISION`,`big_table`.`CHARACTER_SET_NAME` AS `CHARACTER_SET_NAME`,`big_table`.`COLLATION_NAME` AS `COLLATION_NAME`,`big_table`.`COLUMN_TYPE` AS `COLUMN_TYPE`,`big_table`.`COLUMN_KEY` AS `COLUMN_KEY`,`big_table`.`EXTRA` AS `EXTRA`,`big_table`.`PRIVILEGES` AS `PRIVILEGES`,`big_table`.`COLUMN_COMMENT` AS `COLUMN_COMMENT`,`big_table`.`id` AS `id` from `big_table` union select `small_table`.`TABLE_CATALOG` AS `TABLE_CATALOG`,`small_table`.`TABLE_SCHEMA` AS `TABLE_SCHEMA`,`small_table`.`TABLE_NAME` AS `TABLE_NAME`,`small_table`.`COLUMN_NAME` AS `COLUMN_NAME`,`small_table`.`ORDINAL_POSITION` AS `ORDINAL_POSITION`,`small_table`.`COLUMN_DEFAULT` AS `COLUMN_DEFAULT`,`small_table`.`IS_NULLABLE` AS `IS_NULLABLE`,`small_table`.`DATA_TYPE` AS `DATA_TYPE`,`small_table`.`CHARACTER_MAXIMUM_LENGTH` AS `CHARACTER_MAXIMUM_LENGTH`,`small_table`.`CHARACTER_OCTET_LENGTH` AS `CHARACTER_OCTET_LENGTH`,`small_table`.`NUMERIC_PRECISION` AS `NUMERIC_PRECISION`,`small_table`.`NUMERIC_SCALE` AS `NUMERIC_SCALE`,`small_table`.`DATETIME_PRECISION` AS `DATETIME_PRECISION`,`small_table`.`CHARACTER_SET_NAME` AS `CHARACTER_SET_NAME`,`small_table`.`COLLATION_NAME` AS `COLLATION_NAME`,`small_table`.`COLUMN_TYPE` AS `COLUMN_TYPE`,`small_table`.`COLUMN_KEY` AS `COLUMN_KEY`,`small_table`.`EXTRA` AS `EXTRA`,`small_table`.`PRIVILEGES` AS `PRIVILEGES`,`small_table`.`COLUMN_COMMENT` AS `COLUMN_COMMENT`,`small_table`.`id` AS `id` from `small_table`

这个视图定义了2个表union

[email protected]>explain select * from big_table where table_name in(‘xx’,’aa’);
+—-+————-+———–+——-+—————-+—————-+———+——+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———–+——-+—————-+—————-+———+——+——+———————–+
| 1 | SIMPLE | big_table | range | idx_TABLE_NAME | idx_TABLE_NAME | 194 | NULL | 1022 | Using index condition |
+—-+————-+———–+——-+—————-+—————-+———+——+——+———————–+

在源表上的一个in子查询,是可以用到索引的,但是下面的视图中查询,是无法使用到索引的

mysql@test>explain select * from v_big_table where table_name in(‘xx’,’aa’);
+—-+————–+————-+——+—————+——+———+——+——–+—————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————–+————-+——+—————+——+———+——+——–+—————–+
| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 904149 | Using where |
| 2 | DERIVED | big_table | ALL | NULL | NULL | NULL | NULL | 902397 | NULL |
| 3 | UNION | small_table | ALL | NULL | NULL | NULL | NULL | 1752 | NULL |
| NULL | UNION RESULT |

你可能感兴趣的:(MYSQL)