1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| DESC information_schema.TABLES TABLE_SCHEMA TABLE_NAME ENGINE TABLE_ROWS AVG_ROW_LENGTH INDEX_LENGTH
查询整个数据库中所有库和所对应的表信息 SELECT table_schema,GROUP_CONCAT(table_name) FROM information_schema.tables GROUP BY table_schema;
统计所有库下的表个数 SELECT table_schema,COUNT(table_name) FROM information_schema.TABLES GROUP BY table_schema
查询所有innodb引擎的表及所在的库 SELECT table_schema,table_name,ENGINE FROM information_schema.`TABLES` WHERE ENGINE='innodb'; 统计world数据库下每张表的磁盘空间占用 SELECT table_name,CONCAT((TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") AS size_KB FROM information_schema.tables WHERE TABLE_SCHEMA='world';
统计所有数据库的总的磁盘空间占用 SELECT TABLE_SCHEMA, CONCAT(SUM(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") AS Total_KB FROM information_schema.tables GROUP BY table_schema; mysql -uroot -p123 -e "SELECT TABLE_SCHEMA,CONCAT(SUM(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024,' KB') AS Total_KB FROM information_schema.tables GROUP BY table_schema;"
生成整个数据库下的所有表的单独备份语句 模板语句: mysqldump -uroot -p123 world city >/tmp/world_city.sql SELECT CONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql" ) FROM information_schema.tables WHERE table_schema NOT IN('information_schema','performance_schema','sys') INTO OUTFILE '/tmp/bak.sh' ;
CONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql" )
107张表,都需要执行以下2条语句 ALTER TABLE world.city DISCARD TABLESPACE; ALTER TABLE world.city IMPORT TABLESPACE; SELECT CONCAT("alter table ",table_schema,".",table_name," discard tablespace") FROM information_schema.tables WHERE table_schema='world' INTO OUTFILE '/tmp/dis.sql';
|