如果我截断一张表,还应该添加索引吗?


如果你截断了一张表,则不需要添加索引,因为表在被截断后会重新创建,索引也会自动添加。

我们首先创建一个表:-

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> FirstName varchar(20),
   -> LastName varchar(20)
   -> );
Query OK, 0 rows affected (0.65 sec)

以下查询用来创建索引:-

mysql> create index Index_firstName_LastName on DemoTable(FirstName,LastName);
Query OK, 0 rows affected (1.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

使用插入命令在表中插入一些记录:-

mysql> insert into DemoTable(FirstName,LastName) values('John','Smith');
Query OK, 1 row affected (0.47 sec

使用 select 语句显示表中的所有记录:-

mysql> select *from DemoTable;

输出

这将产生以下输出:-

+----+-----------+----------+
| Id | FirstName | LastName |
+----+-----------+----------+
|  1 | John      | Smith    |
+----+-----------+----------+
1 row in set (0.00 sec)

在截断表之前,你需要检查表的 status:-

mysql> show create table DemoTable;

输出

这将产生以下输出:-

+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create
Table|
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable    | CREATE TABLE `DemoTable` (`Id` int(11) NOT NULL AUTO_INCREMENT,`FirstName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,`LastName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,PRIMARY KEY (`Id`),KEY `Index_firstName_LastName` (`FirstName`,`LastName`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

现在截断表:-

mysql> truncate table DemoTable;
Query OK, 0 rows affected (2.01 sec)

现在再次检查表的状态。你不需要添加索引:-

mysql> show create table DemoTable;

输出

这将产生以下输出:-

+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create
Table
|
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable | CREATE TABLE `DemoTable` (`Id` int(11) NOT NULL AUTO_INCREMENT,`FirstName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,`LastName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`Id`),KEY `Index_firstName_LastName` (`FirstName`,`LastName`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

更新于: 2020 年 6 月 30 日

709 人次浏览

开启您职业生涯

完成课程后获得认证

开始
广告