Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92644753
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: Mysql/postgreSQL

2008-04-19 23:21:44

 

  因为对元数据(元数据就是那些关于如何数据库的那些数据)的使用已经变得愈来愈普遍了,MySQL5 在产品中也相应地设计了一个专门的数据库,这个数据库的固定名称就是 information_schema,该数据库作为一个中心数据字典的角色出现,其中包含了数据库的所有对象信息和其他与数据库相关的项目(比如安全),这些对象等信息都在 MySQL 服务器产品中做了定义。

  数据库管理人员和相关开发人员可以使用 information_schema 数据字典数据库来获得 MySQL 服务器上一个或多个数据库相关的多方面的元数据。

  举例说明,如果数据库管理人员想要知道某个 MySQL 数据库实例中存储空间的概况,那么可以通过执行如下的对数据字典的查询来实现:

  mysql> SELECT a.schema_name db_name,
  -> IFNULL(ROUND((SUM(b.data_length)+SUM(b.index_length))/1024/1024,2),0.00)
  -> total_size_mb,
  -> IFNULL(ROUND(((SUM(b.data_length)+SUM(b.index_length))-
  -> SUM(b.data_free))/1024/1024,2),0.00) data_used_mb,
  -> IFNULL(ROUND(SUM(data_free)/1024/1024,2),0.00) data_free_mb,
  -> IFNULL(ROUND((((SUM(b.data_length)+SUM(b.index_length))-SUM(b.data_free))/
  -> ((SUM(b.data_length)+SUM(b.index_length)))*100),2),0) pct_used,
  -> COUNT(table_name) tables
  -> FROM information_schema.schemata a
  -> LEFT JOIN information_schema.tables b ON a.schema_name = b.table_schema
  -> WHERE a.schema_name != 'information_schema'
  -> GROUP BY a.schema_name
  -> ORDER BY 1;

  +---------+---------------+--------------+--------------+----------+--------+
  | db_name | total_size_mb | data_used_mb | data_free_mb | pct_used | tables |
  +---------+---------------+--------------+--------------+----------+--------+
  | gim | 432.67 | 432.67 | 0.00 | 100.00 | 16 |
  | gim2 | 8.64 | 8.64 | 0.00 | 100.00 | 6 |
  | mysql | 0.33 | 0.33 | 0.00 | 99.69 | 18 |
  | test | 0.00 | 0.00 | 0.00 | 0.00 | 0 |
  | tpcc | 126.09 | 126.09 | 0.00 | 100.00 | 9 |
  +---------+---------------+--------------+--------------+----------+--------+

  注意上面的示例中,应该设为不查询字典数据库自身,通过 where 条件语句来实现。
阅读(606) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~