首頁  >  文章  >  資料庫  >  詳解MySQL如何建立索引(案例)

詳解MySQL如何建立索引(案例)

coldplay.xixi
coldplay.xixi轉載
2021-03-24 09:59:575375瀏覽

詳解MySQL如何建立索引(案例)

案例:建立資料庫index_test,依照下表的結構在index_test資料庫中建立兩個資料表test_table1和test_table2,並且依照操作程序完成對資料表的基本操作。

(1)登入MySQL資料庫
(2)建立資料庫index_test
(3)建立表格test_table1
(4)建立表格test_table2,儲存引擎為MyISAM
(5)使用alter table 語句在表格test_table2的birth欄位上建立名稱為ComDateIdx的普通索引
(6)使用alter table語句在表格test_table2的id欄位上新增名稱為UniqIdx2的唯一索引,並以降序排列
(7)使用create index 在firstname、middlename和lastname三個欄位上建立名稱為MultiColidx2的組合索引
(8)使用create index在title欄位上建立名稱為FTidx的全文索引
(9)使用alter table語句刪除表test_table1中名稱為Uniqidx的唯一索引
(10)使用drop index語句刪除表test_table2中名稱為MultiColidx2的組合索引
幾個注意點

#(免費學習推薦:mysql影片教學)


#(1)登入MySQL資料庫
C:\Users\Hudie>mysql -h localhost -u root -p
Enter password: *******
(2)建立資料庫index_test
mysql> create database index_test;Query OK, 1 row affected (0.06 sec)mysql> use index_test;Database changed
(3)建立表格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表中成功建立了3個索引,分別是在id欄位上名稱為uniqidx的唯一索引;在name和address欄位上的組合索引;在description欄位上長度為30的普通索引。

(4)建立表格test_table2,儲存引擎為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 語句在表格test_table2的birth欄位上建立名稱為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語句在表格test_table2的id欄位上新增名稱為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兩個欄位上建立名稱為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欄位上建立名稱為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語句刪除資料表test_table1中名稱為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語句刪除表test_table2中名稱為MultiColidx2的組合索引
mysql> drop index MultiColidx2 on test_table2;Query OK, 0 rows affected (0.12 sec)Records: 0  Duplicates: 0  Warnings: 0
幾個注意點:

#1.索引對資料庫的效能如此重要,如何使用它?

  • 如果索引列較少,則需要的磁碟空間和維護開銷都較少。
  • 如果在一個大表上建立了多種組合索引,索引檔案也會膨脹很快。另外索引較多,可覆蓋更多的查詢。
  • 嘗試新增、刪除、修改索引,不影響資料庫架構或應用程式設計。

2.盡量使用短索引

  • 對字串類型的欄位進行索引,如果可能應該指定一個前綴長度。例如,有一個char(255)的列,如果在前 10或30個字元內多數值是唯一的,就不需要對整個列進行索引。
  • 短索引不僅可以提高查詢速度,還能節省磁碟空間、減少I/O作業。

相關免費學習推薦:mysql資料庫(影片)

#

以上是詳解MySQL如何建立索引(案例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除