千萬行的表怎麼加索引

執行過程以下(前提是中止寫入防止數據異常)mysql

一、建立一個表結構和對象同樣的表sql

二、給新表加索引bash

三、給新表插入老表數據對象

四、把2個表重命名。blog

五、刪除重命名後老表(可作可不作)索引

create table tmp like paper_author;
ALTER TABLE tmp ADD INDEX ( `PaperID` )rem

insert into tmp(ooo,...)  select  ooo,... from paper_author
Query OK, 35510600 rows affected (9 min 24.99 sec)
Records: 35510600  Duplicates: 0  Warnings: 0table

RENAME TABLE paper_author TO tmp2, tmp to paper_author;class

drop table tmp2;select

 

執行過程以下

mysql> use seewoserzs;
Database changed
mysql> desc t_gameinfo;
+----------+---------------------+------+-----+-------------------+----------------+
| Field    | Type                | Null | Key | Default           | Extra          |
+----------+---------------------+------+-----+-------------------+----------------+
| gameid   | varchar(32)         | YES  | UNI | 10000             |                |
| gamename | varchar(32)         | YES  |     | NULL              |                |
| cpid     | bigint(20)          | YES  |     | NULL              |                |
| gametype | varchar(20)         | YES  |     | NULL              |                |
| addtime  | timestamp           | YES  |     | CURRENT_TIMESTAMP |                |
| id       | bigint(20) unsigned | NO   | PRI | NULL              | auto_increment |
+----------+---------------------+------+-----+-------------------+----------------+
6 rows in set

mysql> create table t1 like t_gameinfo;
Query OK, 0 rows affected

mysql> desc t1;
+----------+---------------------+------+-----+-------------------+----------------+
| Field    | Type                | Null | Key | Default           | Extra          |
+----------+---------------------+------+-----+-------------------+----------------+
| gameid   | varchar(32)         | YES  | UNI | 10000             |                |
| gamename | varchar(32)         | YES  |     | NULL              |                |
| cpid     | bigint(20)          | YES  |     | NULL              |                |
| gametype | varchar(20)         | YES  |     | NULL              |                |
| addtime  | timestamp           | YES  |     | CURRENT_TIMESTAMP |                |
| id       | bigint(20) unsigned | NO   | PRI | NULL              | auto_increment |
+----------+---------------------+------+-----+-------------------+----------------+
6 rows in set

mysql> insert into t1 select * from t_gameinfo;
Query OK, 34 rows affected
Records: 34  Duplicates: 0  Warnings: 0

mysql> rename table t_gameinfo to t2 ,t1 to t_gameinfo;
Query OK, 0 rows affected

mysql> 
相關文章
相關標籤/搜索