MySQL數據庫的維護手冊

 

 

查看MySQL 庫中全部表的大小和記錄數mysql

SELECT TABLE_NAME,DATA_LENGTH,INDEX_LENGTH,(DATA_LENGTH+INDEX_LENGTH) as length, \
TABLE_ROWS,concat(round((DATA_LENGTH+INDEX_LENGTH)/1024/1024,3), 'MB') as total_size \
FROM information_schema.TABLES WHERE TABLE_SCHEMA='database_name' order by length desc;

說明:sql

    TABLE_NAME :表名字;
    DATA_LENGTH : 數據大小;
    INDEX_LENGTH :索引大小;
    TABLE_ROWS : 記錄數量;
    TABLE_SCHEMA : 數據庫名字;
    ENGINE:所使用的存儲引擎;
    information_schema :是mysql自帶的,它提供了訪問數據庫元數據的方式,元數據是關於數據的數據,
           如數據庫名或表名,列的數據類型,或訪問權限等。有些時候用於表述該信息的其餘術語包括「數據詞典」和「系統目錄」。數據庫


一個表佔用空間的大小,至關因而 數據大小 + 索引大小;spa

 

  

 實例:code

mysql> SELECT TABLE_NAME,DATA_LENGTH,INDEX_LENGTH,(DATA_LENGTH+INDEX_LENGTH) as length,\
    -> TABLE_ROWS,concat(round((DATA_LENGTH+INDEX_LENGTH)/1024/1024,3), 'MB') as total_size \
    -> FROM information_schema.TABLES WHERE TABLE_SCHEMA='zhcwsystem' order by length desc;
+---------------------------------+-------------+--------------+------------+------------+------------+
| TABLE_NAME                      | DATA_LENGTH | INDEX_LENGTH | length     | TABLE_ROWS | total_size |
+---------------------------------+-------------+--------------+------------+------------+------------+
| tab_lottery_spinfo_bd           |  7332730928 |    489879552 | 7822610480 |   31176968 | 7460.223MB |
| tab_kaijiang_info               |   130711552 |     16269312 |  146980864 |     853084 | 140.172MB  |
| tab_lottery_match_bd            |    55002516 |      3718144 |   58720660 |     271763 | 56.000MB   |
| tab_lottery_result_bd           |    16977364 |     10750976 |   27728340 |     276547 | 26.444MB   |
| tab_cz_zj_info                  |    18198416 |       607232 |   18805648 |      26841 | 17.934MB   |
| tab_lottery_result              |     6447924 |      3440640 |    9888564 |     120396 | 9.430MB    |
| tab_sport_lottery_info          |     5374524 |      1154048 |    6528572 |      39045 | 6.226MB    |
| tab_cz_dq_info                  |     5115396 |       424960 |    5540356 |       8684 | 5.284MB    |
| tab_nb_detail                   |          56 |         2048 |       2104 |          1 | 0.002MB    |
| tab_cz_type                     |          40 |         2048 |       2088 |          2 | 0.002MB    |
| tab_lottery_result_lancai_copy1 |           0 |         2048 |       2048 |          0 | 0.002MB    |
| tab_lottery_result_lancai_copy  |           0 |         2048 |       2048 |          0 | 0.002MB    |
+---------------------------------+-------------+--------------+------------+------------+------------+
12 rows in set (0.19 sec)

  

 

MySQL 複製表結構orm

MySQL複製表結構主要有如下幾種方法。blog

方法1:   create table new_tab like ori_tab索引

mysql> desc tab_lottery_spinfo_bd;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| lotteryid    | varchar(32) | NO   | MUL | NULL    |                |
| lotteryissue | varchar(12) | NO   |     | NULL    |                |
| ballid       | varchar(8)  | NO   |     | NULL    |                |
| spinfo       | text        | YES  |     | NULL    |                |
| single       | int(1)      | YES  |     | 0       |                |
| p_status     | varchar(30) | YES  |     | NULL    |                |
| lasttime     | datetime    | YES  |     | NULL    |                |
| addtime      | datetime    | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)


mysql> create table tab_lottery_spinfo_bd_new like tab_lottery_spinfo_bd;
Query OK, 0 rows affected (0.08 sec)


mysql> desc tab_lottery_spinfo_bd_new;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| lotteryid    | varchar(32) | NO   | MUL | NULL    |                |
| lotteryissue | varchar(12) | NO   |     | NULL    |                |
| ballid       | varchar(8)  | NO   |     | NULL    |                |
| spinfo       | text        | YES  |     | NULL    |                |
| single       | int(1)      | YES  |     | 0       |                |
| p_status     | varchar(30) | YES  |     | NULL    |                |
| lasttime     | datetime    | YES  |     | NULL    |                |
| addtime      | datetime    | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)


mysql> select count(*) from tab_lottery_spinfo_bd_new;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

  

方法2:   create table new_tab1 select * from ori_tab limit 0;ci

mysql> create table tab_lottery_spinfo_bd_new1 select * from tab_lottery_spinfo_bd limit 0;
Query OK, 0 rows affected (0.08 sec)

  

方法3:執行SQL語句來建立新表的表結構rem

# 顯示需建立表的SQL語句:
mysql> show create table tab_lottery_spinfo_bd\G;
*************************** 1. row ***************************
       Table: tab_lottery_spinfo_bd
Create Table: CREATE TABLE `tab_lottery_spinfo_bd` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lotteryid` varchar(32) NOT NULL,
  `lotteryissue` varchar(12) NOT NULL,
  `ballid` varchar(8) NOT NULL,
  `spinfo` text,
  `single` int(1) DEFAULT '0',
  `p_status` varchar(30) DEFAULT NULL,
  `lasttime` datetime DEFAULT NULL,
  `addtime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `lotteryissue` (`lotteryid`,`lotteryissue`,`ballid`)
) ENGINE=MyISAM AUTO_INCREMENT=312 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


ERROR:
No query specified

# Copy上面的SQL語句,修改 表名 和 AUTO_INCREMENT 的值,而後執行一下。
CREATE TABLE `tab_lottery_spinfo_bd_new2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lotteryid` varchar(32) NOT NULL,
  `lotteryissue` varchar(12) NOT NULL,
  `ballid` varchar(8) NOT NULL,
  `spinfo` text,
  `single` int(1) DEFAULT '0',
  `p_status` varchar(30) DEFAULT NULL,
  `lasttime` datetime DEFAULT NULL,
  `addtime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `lotteryissue` (`lotteryid`,`lotteryissue`,`ballid`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

 

 

 

MySQL複製表結構及其表數據

 

方法1: create table new_tab select * from ori_tab;

mysql> create table tab_lottery_spinfo_bd_new1 select * from tab_lottery_spinfo_bd;
Query OK, 1004000 rows affected (0.58 sec)
Records: 1004000 Duplicates: 0 Warning: 0

  

方法2: create table new_tab2 select filed1,filed2 from ori_tab [ where id=1];

mysql> create table tab_lottery_spinfo_bd_new2 select id,lotteryid,lotteryissue,ballid,spinfo from tab_lottery_spinfo_bd where lotteryissue=90602;
Query OK, 311 rows affected (0.28 sec)
Records: 311 Duplicates: 0 Warning: 0

  

以上兩種方法,方便快捷、靈活性強。

 

方法3: 先建立一個空表,而後在進行 INSERT INTO new_tab3
 
先建立一個新表,而後執行以下兩個語句:
     INSERT INTO new_tab3 SELECT * FROM ori_tab;
或者:
     INSERT INTO  new_tab3(field1, field2, field3) SELECT fied1, field2, field3 FROM ori_tab;
 
    這種方法不是很方便,可是也有不少人常常用。
相關文章
相關標籤/搜索