案例:創(chuàng)建數(shù)據(jù)庫(kù)index_test,按照下表的結(jié)構(gòu)在index_test數(shù)據(jù)庫(kù)中創(chuàng)建兩個(gè)數(shù)據(jù)表test_table1和test_table2,并按照操作過(guò)程完成對(duì)數(shù)據(jù)表的基本操作。
(1)登錄mysql數(shù)據(jù)庫(kù)
(2)創(chuàng)建數(shù)據(jù)庫(kù)index_test
(3)創(chuàng)建表test_table1
(4)創(chuàng)建表test_table2,存儲(chǔ)引擎為MyISAM
(5)使用alter table 語(yǔ)句在表test_table2的birth字段上建立名稱(chēng)為ComDateIdx的普通索引
(6)使用alter table語(yǔ)句在表test_table2的id字段上添加名稱(chēng)為UniqIdx2的唯一索引,并以降序排列
(7)使用create index 在firstname、middlename和lastname三個(gè)字段上建立名稱(chēng)為MultiColidx2的組合索引
(8)使用create index在title字段上建立名稱(chēng)為FTidx的全文索引
(9)使用alter table語(yǔ)句刪除表test_table1中名稱(chēng)為Uniqidx的唯一索引
(10)使用drop index語(yǔ)句刪除表test_table2中名稱(chēng)為MultiColidx2的組合索引
幾個(gè)注意點(diǎn)
(免費(fèi)學(xué)習(xí)推薦:mysql視頻教程)
(1)登錄MySQL數(shù)據(jù)庫(kù)
C:UsersHudie>mysql -h localhost -u root -p Enter password: *******
(2)創(chuàng)建數(shù)據(jù)庫(kù)index_test
mysql> create database index_test;Query OK, 1 row affected (0.06 sec)mysql> use index_test;Database changed
(3)創(chuàng)建表test_table1
mysql> create table test_table1 -> ( -> id int not null primary key auto_increment, -> name char(100) not null, -> address char(100) not null, -> description char(100) not null, -> unique index uniqidx(id), -> index MultiColidx(name(20),address(30) ), -> index Comidx(description(30)) -> );Query OK, 0 rows affected (0.11 sec)mysql> show create table test_table1 G*************************** 1. row *************************** Table: test_table1Create Table: CREATE TABLE `test_table1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(100) NOT NULL, `address` char(100) NOT NULL, `description` char(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniqidx` (`id`), KEY `MultiColidx` (`name`(20),`address`(30)), KEY `Comidx` (`description`(30))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)
可以看到在test_table表中成功創(chuàng)建了3個(gè)索引,分別是在id字段上名稱(chēng)為uniqidx的唯一索引;在name和address字段上的組合索引;在description字段上長(zhǎng)度為30的普通索引。
(4)創(chuàng)建表test_table2,存儲(chǔ)引擎為MyISAM
mysql> create table test_table2 -> ( -> id int not null primary key auto_increment, -> firstname char(100) not null, -> middlename char(100) not null, -> lastname char(100) not null, -> birth date not null, -> title char(100) null -> )ENGINE=MyISAM;Query OK, 0 rows affected (0.07 sec)
(5)使用alter table 語(yǔ)句在表test_table2的birth字段上建立名稱(chēng)為ComDateIdx的普通索引
mysql> alter table test_table2 add index ComDateidx(birth);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0
(6)使用alter table語(yǔ)句在表test_table2的id字段上添加名稱(chēng)為Uniqidx2的唯一索引
mysql> alter table test_table2 add unique index Uniqidx(id);Query OK, 0 rows affected (0.11 sec)Records: 0 Duplicates: 0 Warnings: 0
(7)使用create index 在firstname和middlename兩個(gè)字段上建立名稱(chēng)為 MultiColidx2的組合索引
mysql> create index MultiColidx2 on test_table2(firstname,middlename);Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0
(8)使用create index在title字段上建立名稱(chēng)為FTidx的全文索引
mysql> create fulltext index ftidx on test_table2(title);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0
(9)使用alter table語(yǔ)句刪除表test_table1中名稱(chēng)為Uniqidx的唯一索引
mysql> alter table test_table1 drop index uniqidx;Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0
(10)使用drop index語(yǔ)句刪除表test_table2中名稱(chēng)為MultiColidx2的組合索引
mysql> drop index MultiColidx2 on test_table2;Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0
幾個(gè)注意點(diǎn):
1.索引對(duì)數(shù)據(jù)庫(kù)的性能如此重要,如何使用它?
- 如果索引列較少,則需要的磁盤(pán)空間和維護(hù)開(kāi)銷(xiāo)都較少。
- 如果在一個(gè)大表上創(chuàng)建了多種組合索引,索引文件也會(huì)膨脹很快。另外索引較多,可覆蓋更多的查詢。
- 嘗試添加、刪除、修改索引,不影響數(shù)據(jù)庫(kù)架構(gòu)或應(yīng)用程序設(shè)計(jì)。
2.盡量使用短索引
- 對(duì)字符串類(lèi)型的字段進(jìn)行索引,如果可能應(yīng)該指定一個(gè)前綴長(zhǎng)度。例如,有一個(gè)char(255)的列,如果在前 10或30個(gè)字符內(nèi)多數(shù)值是唯一的,就不需要對(duì)整個(gè)列進(jìn)行索引。
- 短索引不僅可以提高查詢速度,也能節(jié)省磁盤(pán)空間、減少I(mǎi)/O操作。
相關(guān)免費(fèi)學(xué)習(xí)推薦:mysql數(shù)據(jù)庫(kù)(視頻)