mysql常用快速查询修改操作

时间:2022-05-12 06:05:28

mysql常用快速查询修改操作

一、查找并修改非innodb引擎为innodb引擎

# 通用操作
mysql> select concat('alter table ',table_schema,'.',table_name,' engine=innodb;') from information_schema.tables where table_schema not in ('information_schema','mysql','performance_schema') and engine='myisam';
# 示例
select concat('alter table test.',table_name,' engine=innodb;') from information_schema.tables where table_schema not in ('information_schema','mysql','performance_schema') and engine='myisam';
mysql> select concat('alter table test.',table_name,' engine=innodb;') from information_schema.tables where table_schema not in ('information_schema','mysql','performance_schema') and engine='myisam';
+----------------------------------------------------------+
| concat('alter table test.',table_name,' engine=innodb;') |
+----------------------------------------------------------+
| alter table test.tempusermap engine=innodb;              |
| alter table test.users_relation_list engine=innodb;      |
+----------------------------------------------------------+
2 rows in set (0.58 sec) mysql>

二、查询数据大小

在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量。

1、要查询表所占的容量,就是把表的数据和索引加起来就可以了

mysql>  select TABLE_SCHEMA,(sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024/1024/1024 AS 'size_g' from information_schema.tables where TABLE_SCHEMA NOT IN ('information_schema','mysql','performance_schema')  group by TABLE_SCHEMA;
+--------------+-----------------+
| TABLE_SCHEMA | size_g |
+--------------+-----------------+
| db01 | 13.810716629028 |
| db02 | 0.279693603516 |
| test | 0.143768310547 |
+--------------+-----------------+
3 rows in set (9.06 sec)

2、查询所有的数据大小

select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from information_schema.tables; -- 查询所有的数据大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from information_schema.tables; -- 查询所有的数据大小
+-------------------------------------------------+
| concat(round(sum(DATA_LENGTH/1024/1024),2),'M') |
+-------------------------------------------------+
| 5324.54M |
+-------------------------------------------------+
1 row in set (8.60 sec)

3、查询某个表的数据

select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from tables where table_schema='test' AND table_name='USER';

mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from information_schema.tables where table_schema='test' AND table_name='USER';
+-------------------------------------------------+
| concat(round(sum(DATA_LENGTH/1024/1024),2),'M') |
+-------------------------------------------------+
| 73.61M |
+-------------------------------------------------+
1 row in set (0.03 sec) mysql>select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),'M') AS 'SIZE_MB' from information_schema.tables where table_schema='db222' group by table_name order by SIZE_MB asc,table_name asc;
+----------------+---------+
| table_name     | SIZE_MB |
+----------------+---------+
| t1             | 0.02M   |
| t_user_partion | 487.33M |
| t_user_id3     | 71.61M  |
| t_user_id0     | 74.61M  |
| t_user_id1     | 74.61M  |
| t_user_id2     | 74.61M  |
| t_user_id4     | 74.61M  |
| t_user_id5     | 74.61M  |
| t_user_id6     | 74.61M  |
| t_user_id7     | 74.61M  |
| t_user_id8     | 74.61M  |
| t_user_id9     | 74.61M  |
+----------------+---------+
12 rows in set (0.00 sec) mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') AS table_size,table_name from information_schema.tables where table_schema='fudao_account' AND table_name IN ('db3','db2','db3','db1') group by table_name;
+------------+------------------------+
| table_size | table_name             |
+------------+------------------------+
| 2.52M      | db1                      |
| 0.23M      | db2                    |
| 0.30M      | db3                    |
+------------+------------------------+
3 rows in set (0.00 sec) mysql>