Mysql營運管理-建立索引知識及建立索引的多種方法實戰9

來源:互聯網
上載者:User

標籤:desc   mysql   body   問題   blog   配置   warnings   語句   com   

為表的欄位建立索引

索引就像書的目錄一樣,如果在欄位上建立索引,那麼以索引為條件時可以加快查詢資料的速度。 1 建立主鍵索引

查詢資料庫的內容,按主鍵查詢是最快的,每個表只能有一個主鍵,但是可以有多個普通索引列,主鍵列要求所有內容必須唯一,而索引列不要求內容唯一。
我們無論建立主鍵索引還是普通索引,都要在表的對應列上建立,可以對單列建立索引也可以對多列建立索引
建立主鍵索方法:

1.在建立表時,可以增加建立主鍵索引語句

[email protected] 04:0932->create table student(-> id int(4) not null AUTO_INCREMENT,-> name char(20) not null,-> age tinyint(2) NOT NULL default ‘0‘,-> dept varchar(16) default NULL,-> primary key(id),-> KEY index_name(name)-> );

提示:

a.AUTO_INCREMENT 自增

b.Primary key(id) 主鍵

c.KEY index_name(name) name 欄位普通索引

操作示範

[email protected] 04:2146->drop table student;Query OK, 0 rows affected (0.00 sec)[email protected] 04:2333->create table student(-> id int(4) not null AUTO_INCREMENT,-> name char(20) not null,-> age tinyint(2) NOT NULL default ‘0‘,-> dept varchar(16) default NULL,-> primary key(id),-> KEY index_name(name)-> );Query OK, 0 rows affected (0.00 sec)[email protected] 04:2609->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(4)  | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | MUL | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

提示:PRI為主鍵的標識,MUL為普通索引的標識。

2.建立表之後通過alter命令增加主鍵索引(不推薦這種做法)

a.主鍵列不能重複建立,必須先刪除上面的配置

[email protected] 04:3716->alter table student drop primary key;

b.建表時忘記加主鍵了利用alter命令增加id列為自增主鍵列

[email protected] 04:5948->alter table student change id id int primary key auto_increment;Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 05:0324->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(11) | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | MUL | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

提示:只有int類型且為primary key才可以使用auto_increment。 9.9.7.2 建立普通索引

1.在建表時,可以增加建立普通索引列的語句如下:

[email protected] 04:0932->create table student(-> id int(4) not null AUTO_INCREMENT,-> name char(20) not null,-> age tinyint(2) NOT NULL default ‘0‘,-> dept varchar(16) default NULL,-> primary key(id),-> KEY index_name(name)-> );

提示:

a.KEY index_name(name)name 欄位普通索引

操作示範:

[email protected] 04:2146->drop table student;Query OK, 0 rows affected (0.00 sec)[email protected] 04:2333->create table student(-> id int(4) not null AUTO_INCREMENT,-> name char(20) not null,-> age tinyint(2) NOT NULL default ‘0‘,-> dept varchar(16) default NULL,-> primary key(id),-> KEY index_name(name)-> );Query OK, 0 rows affected (0.00 sec)[email protected] 04:2609->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(4)  | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | MUL | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

2.建表後利用alter增加普通索引

刪除建表時建立的index_name索引

[email protected] 05:0333->alter table student drop index index_name;Query OK, 0 rows affected (0.01 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 06:0419->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(11) | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

在name列上添加索引,索引名為index_name

[email protected] 06:0431->alter table student add index index_name(name);Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 06:0456->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(11) | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | MUL | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)
3 對欄位的前n個字元建立普通索引

當遇到表中比較大的列時,列內容的前n個字元在所有內容中已經接近唯一時,這時可以對列的前n個字元建立索引而無需對整個列建立索引,這樣可以節省建立索引佔用的系統空間,以及降低和更新維護消耗的系統資源。

對欄位的前n個字元建立索引的文法:

Create index index_name on test(name(8)); 條件列前n個字元建立索引

實戰操作:

[email protected] 06:0838->create index index_dept on student(dept(8));Query OK, 0 rows affected (0.19 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 06:3650->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(11) | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | MUL | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | MUL | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)[email protected] 06:3847->show index from student\G*************************** 1. row ***************************   Table: student  Non_unique: 0Key_name: PRIMARYSeq_in_index: 1 Column_name: id   Collation: A Cardinality: 0Sub_part: NULL  Packed: NULLNull:   Index_type: BTREE Comment: *************************** 2. row ***************************   Table: student  Non_unique: 1Key_name: index_nameSeq_in_index: 1 Column_name: name   Collation: A Cardinality: NULLSub_part: NULL  Packed: NULLNull:   Index_type: BTREE Comment: *************************** 3. row ***************************   Table: student  Non_unique: 1Key_name: index_deptSeq_in_index: 1 Column_name: dept   Collation: A Cardinality: NULLSub_part: 8  Packed: NULLNull: YES  Index_type: BTREE Comment: 3 rows in set (0.00 sec)
4 為表的多個欄位建立聯合索引

如果查詢的條件是多列時,我們可以為多個查詢的列建立聯合索引,甚至可以為多列的前n個字元建立聯合索引,實戰示範如下:

為多個列建立索引

[email protected] 06:4019->create index ind_name_dept on student(name,dept);Query OK, 0 rows affected (0.01 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 06:4726->show index from student\G*************************** 1. row ***************************   Table: student  Non_unique: 0Key_name: PRIMARYSeq_in_index: 1 Column_name: id   Collation: A Cardinality: 0Sub_part: NULL  Packed: NULLNull:   Index_type: BTREE Comment: *************************** 2. row ***************************   Table: student  Non_unique: 1Key_name: index_nameSeq_in_index: 1 Column_name: name   Collation: A Cardinality: NULLSub_part: NULL  Packed: NULLNull:   Index_type: BTREE Comment: *************************** 3. row ***************************   Table: student  Non_unique: 1Key_name: index_deptSeq_in_index: 1 Column_name: dept   Collation: A Cardinality: NULLSub_part: 8  Packed: NULLNull: YES  Index_type: BTREE Comment: *************************** 4. row ***************************   Table: student  Non_unique: 1Key_name: ind_name_deptSeq_in_index: 1 Column_name: name   Collation: A Cardinality: NULLSub_part: NULL  Packed: NULLNull:   Index_type: BTREE Comment: *************************** 5. row ***************************   Table: student  Non_unique: 1Key_name: ind_name_deptSeq_in_index: 2 Column_name: dept   Collation: A Cardinality: NULLSub_part: NULL  Packed: NULLNull: YES  Index_type: BTREE Comment: 5   rows in set (0.00 sec)

可以對多個列的前n個字元建立聯合索引,實戰示範如下:

[email protected] 06:5144->drop index ind_name_dept on student; 刪除索引的另一種方法。Query OK, 0 rows affected (0.01 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 06:5158->create index ind_name_dept on student(name(8),dept(10));Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0

提示:

按條件列查詢資料時,聯合索引是有首碼生效特性的

Index(a,b,c)僅a,ab,abc三個查詢條件可以走索引。b,bc,ac,c不能走索引 5 建立唯一索引(非主鍵索引)

Create unique index index_age on student(age);

唯一索引是用來約束表的內容的,不能重複。

[email protected] 07:0828->create unique index uni_ind_name on student(name);Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0[email protected] 07:1010->desc student;+-------+-------------+------+-----+---------+----------------+| Field | Type| Null | Key | Default | Extra  |+-------+-------------+------+-----+---------+----------------+| id| int(11) | NO   | PRI | NULL| auto_increment || name  | char(20)| NO   | UNI | NULL||| age   | tinyint(2)  | NO   | | 0   ||| dept  | varchar(16) | YES  | MUL | NULL||+-------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)
6 索引表的建立及生效條件

問題1:既然索引可以加快查詢速度,那麼是不是就要給所有的列建立索引呢?

解答:因為索引你不但佔用系統空間,更新資料庫時還需要維護索引資料。因此索引是一把雙刃劍,並不是越多越好,例如:數十到幾百行的小表上無需建立索引,更新頻繁,讀取少的業務要少建立索引。

問題2:需要在哪些列上建立索引呢?

解答:Select user,host from mysql.user where host= ….索引一定要建立在where後的條件列上,而不是select後的選擇資料的列上,另外我們要盡量選擇在唯一值多的大表上建立索引。建立索引要和開發商量。 7 建立索引命令集合小結

1.建立索引相關命令集合

建立主鍵索引

alter table student change id id int primary key auto_increment;

刪除主鍵索引

alter table student drop primary key

建立普通索引

alter table student add index index_dept(dept(8))

根據列的前n個字元建立索引

create index index_dept on student(dept(8))

根據多個列建立聯合索引

create index ind_name_dept on student(name,dept);

根據多個列的前n個字元建立索引

create index ind_name_dept on student(name(8),dept(10));

刪除普通索引

Alter table student drop index index_deptdrop index ind_name_dept on student

建立唯一索引

create unique index uni_ind_name on student(name);

基本索引:

a.要在表的列上建立索引。

b.索引會加快查詢速度,但是會影響更新的速度,因為要維護索引。

c.索引不是越多越好,要在頻繁查詢的where後的條件列上建立索引。

d.小表或唯一值極少的列上不建索引,要在大表以及不同內容多的列上建立索引。

Mysql營運管理-建立索引知識及建立索引的多種方法實戰9

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.