mysql查看某库表大小

本文提供SQL语句,用于查询数据库及单个表所占用的空间大小,包括数据文件和索引文件的大小,并给出具体示例。

查询所有数据库占用磁盘空间大小的SQL语句:

语句如下

1
2
3
4
5
select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;

  

执行结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;
+--------------------+------------+------------+
| TABLE_SCHEMA       | data_size  | index_size |
+--------------------+------------+------------+
| shdx               | 1042.38 MB | 258.97MB   |
| mysql              | 0.52 MB    | 0.08MB     |
| information_schema | 0.00 MB    | 0.00MB     |
+--------------------+------------+------------+
3 rows in set
 
mysql>

  

 

查询单个库中所有表磁盘占用大小的SQL语句:

执行语句如下

1
2
3
4
5
select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB'as data_size,
concat(truncate(index_length/1024/1024,2),' MB'as index_size
from information_schema.tables where TABLE_SCHEMA = 'TestDB'
group by TABLE_NAME
order by data_length desc;

以上语句测试有效,注意替换以上的TestDB为数据库名 

 

执行结果如下,截取部分

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
mysql> select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB'as data_size,
concat(truncate(index_length/1024/1024,2),' MB'as index_size
from information_schema.tables where TABLE_SCHEMA = 'shdx'
group by TABLE_NAME
order by data_length desc;
+--------------------------------+-----------+------------+
| TABLE_NAME                     | data_size | index_size |
+--------------------------------+-----------+------------+
| tab_automatch_log              | 499.08 MB | 95.03 MB   |
| tab_fundlog201505              | 101.96 MB | 19.13 MB   |
| tab_online                     | 63.49 MB  | 14.32 MB   |
| tab_game_record201505          | 56.97 MB  | 30.93 MB   |
| tab_user                       | 52.17 MB  | 12.31 MB   |
| dc_user_action_active          | 39.94 MB  | 0.00 MB    |
| tab_gametime_day               | 37.22 MB  | 21.50 MB   |
| tab_fundlog                    | 35.12 MB  | 6.45 MB    |
| tab_game_record                | 19.85 MB  | 10.56 MB   |
| tab_feeaccount                 | 19.17 MB  | 10.75 MB   |
| tab_userinfo                   | 18.78 MB  | 6.33 MB    |
| tab_userfund                   | 17.91 MB  | 9.51 MB    |
| tab_click_count                | 14.63 MB  | 1.75 MB    |
| tab_achievement_log            | 10.51 MB  | 1.36 MB    |
| tab_gameloginlog201505         | 5.78 MB   | 0.75 MB    |
| tab_gametime                   | 4.29 MB   | 3.16 MB    |
| tab_gameloginlog               | 3.83 MB   | 0.50 MB    |
| tab_login_log201505            | 2.79 MB   | 0.67 MB    |
| tab_system_operatelog          | 2.74 MB   | 0.19 MB    |
| tab_singleplay_score           | 2.10 MB   | 0.28 MB    |
| tab_game_record201301          | 1.93 MB   | 1.08 MB    |
| tab_game_score_shddz           | 1.61 MB   | 0.80 MB    |
| tab_game_score_ddz             | 1.39 MB   | 0.66 MB    |
| tab_game_score_shmj            | 1.34 MB   | 0.67 MB    |
| tab_game_xyzdx_getscore_detail | 1.28 MB   | 0.21 MB    |

  

参考链接

http://www.jb51.net/article/40789.htm

转载于:https://www.cnblogs.com/wangjizhen/p/9447208.html

<think> 我们参考了用户提供的多个引用,其中引用[1]和引用[2]直接涉及到查看字段属性值的方法。 引用[1]提到:查询数据库中某个所有字段的属性【table_schema= '数据库名'】;查询数据库中指定指定所有字段的属性【table_schema= '数据库名' and table_name= '名'】。 引用[2]给出了更具体的查询语句:查询特定列(如字段名、字段类型,长度大小、字段注释等)可以使用: select table_schema, table_name, column_name, column_type, column_comment from information_schema.columns where ... 用户的需求是查看MySQL中某字段的属性。因此,我们可以通过查询information_schema数据库中的columns来获取字段属性。 具体步骤: 1. 确定要查询的数据库名(schema名)和名,以及字段名(如果知道的话)。 2. 编写SQL查询语句,从information_schema.columns中选择需要的列。 注意:如果不指定名,则返回整个数据库的所有字段属性;如果指定名,则返回该的所有字段属性;如果还想限定字段名,可以在where条件中加入column_name。 示例: 1. 查看某个数据库(例如'db1')中所有的所有字段属性: SELECT table_name, column_name, column_type, column_comment FROM information_schema.columns WHERE table_schema = 'db1'; 2. 查看某个数据库('db1')中特定(例如'table1')的所有字段属性: SELECT column_name, column_type, column_comment, is_nullable, column_default, extra FROM information_schema.columns WHERE table_schema = 'db1' AND table_name = 'table1'; 3. 查看某个数据库('db1')中特定('table1')的特定字段(例如'id')的属性: SELECT column_name, column_type, column_comment, is_nullable, column_default, extra FROM information_schema.columns WHERE table_schema = 'db1' AND table_name = 'table1' AND column_name = 'id'; 在information_schema.columns中,常用的列有: - column_name: 字段名 - column_type: 字段类型(包括长度) - column_comment: 字段注释 - is_nullable: 是否允许为NULL('YES'/'NO') - column_default: 默认值 - extra: 额外信息(如自增等) 另外,引用[3]提到了自增字段的属性,在extra列中可以看到是否有自增(auto_increment)。 引用[4]提到了JSON字段的查询,但用户当前需求是查看字段属性,因此不直接相关。 引用[5]是关于修改字段属性的,与当前需求无关。 因此,回答重点应放在使用information_schema.columns查询字段属性。 根据以上分析,我们给出详细解答。 </think> ### MySQL查看字段属性的方法 在MySQL查看字段属性,可通过查询`information_schema`系统数据库实现。以下是具体方法和语法: #### 1. 查看指定的所有字段属性 ```sql SELECT column_name AS '字段名', column_type AS '数据类型', is_nullable AS '允许空值', column_default AS '默认值', column_comment AS '字段注释', extra AS '额外属性' FROM information_schema.columns WHERE table_schema = '数据库名' AND table_name = '名'; ``` **示例**:查看`test_db`数据库中`users`的所有字段属性 ```sql SELECT * FROM information_schema.columns WHERE table_schema = 'test_db' AND table_name = 'users'; ``` #### 2. 查看特定字段的属性 ```sql SELECT column_name, column_type, column_key, extra FROM information_schema.columns WHERE table_schema = '数据库名' AND table_name = '名' AND column_name = '字段名'; ``` **示例**:查看`users`中`email`字段的属性 ```sql SELECT column_type, is_nullable, column_default FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'email'; ``` #### 3. 关键字段说明 | 列名 | 描述 | |------|------| | `column_name` | 字段名称 | | `column_type` | 数据类型(如`int(11)`, `varchar(255)`) | | `is_nullable` | 是否允许NULL值(`YES`/`NO`) | | `column_default` | 默认值 | | `column_comment` | 字段注释说明 | | `extra` | 特殊属性(如`auto_increment`自增) | #### 4. 查看JSON字段属性 ```sql SELECT column_name, data_type, column_type FROM information_schema.columns WHERE data_type = 'json' AND table_name = '名'; ``` 对于JSON字段内的具体属性值,需使用`->>`操作符查询实际数据[^4]。 > **原理说明**:MySQL通过`information_schema.columns`系统视图存储所有的元数据,包含字段定义、类型约束等关键信息[^1][^2]。自增属性可通过`extra`列的`auto_increment`标识识别[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值