MySQL筆記之索引的使用
索引是創(chuàng)建在表上的,對數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)
其作用主要在于提高查詢的速度,降低數(shù)據(jù)庫系統(tǒng)的性能開銷
通過索引,查詢數(shù)據(jù)不必讀完記錄的全部信息進(jìn)行匹配,而是只查詢索引列
索引相當(dāng)于字典中的音序表,要查詢某字時(shí)可以在音序表中找到
然后直接跳轉(zhuǎn)到那一音序所在位置,而不必從字典第一頁開始翻,逐字匹配
tips:索引雖能提高查詢速度,但在插入記錄時(shí)會按照索引進(jìn)行排序,因此降低了插入速度
最好的操作方式是先刪除索引,插入大量記錄后再創(chuàng)建索引
索引分類
1.普通索引:不附加任何限制條件,可創(chuàng)建在任何數(shù)據(jù)類型中
2.唯一性索引:使用unique參數(shù)可以設(shè)置索引為唯一性索引,在創(chuàng)建索引時(shí),限制該索引的值必須唯一,主鍵就是一種唯一性索引
3.全文索引:使用fulltext參數(shù)可以設(shè)置索引為全文索引。全文索引只能創(chuàng)建在char、varchar或text類型的字段上。查詢數(shù)據(jù)量較大的字符串類型字段時(shí),效果明顯。但只有MyISAM存儲引擎支持全文檢索
4.單列索引:在表中單個(gè)字段上創(chuàng)建的索引,單列索引可以是任何類型,只要保證索引只對應(yīng)一個(gè)一個(gè)字段
5.多列索引:在表中多個(gè)字段上創(chuàng)建的索引,該索引指向創(chuàng)建時(shí)對應(yīng)的多個(gè)字段
6.空間索引:使用spatial參數(shù)可以設(shè)置索引為空間索引,空間索引只能建立在空間數(shù)據(jù)類型上比如geometry,并且不能為空,目前只有MyISAM存儲引擎支持
在創(chuàng)建表時(shí)創(chuàng)建索引
創(chuàng)建普通索引
mysql> create table index1(
-> id int,
-> name varchar(20),
-> sex boolean,
-> index(id)
-> );
Query OK, 0 rows affected (0.11 sec)
此處在id字段上創(chuàng)建索引,show create table可查看
創(chuàng)建唯一性索引
mysql> create table index2(
-> id int unique,
-> name varchar(20),
-> unique index index2_id(id ASC)
-> );
Query OK, 0 rows affected (0.12 sec)
此處使用id字段創(chuàng)建了一個(gè)名為index2_id的索引
這里的id字段可以不設(shè)置唯一性約束,但這樣一來索引就沒有作用
創(chuàng)建全文索引
mysql> create table index3(
-> id int,
-> info varchar(20),
-> fulltext index index3_info(info)
-> )engine=MyISAM;
Query OK, 0 rows affected (0.07 sec)
要注意創(chuàng)建全文索引時(shí)只能使用MyISAM存儲引擎
創(chuàng)建單列索引
mysql> create table index4(
-> id int,
-> subject varchar(30),
-> index index4_st(subject(10))
-> );
Query OK, 0 rows affected (0.12 sec)
此處subject字段長度是30,而索引長度則是10
這么做的目的在于提高查詢速度,對于字符型的數(shù)據(jù)不用查詢?nèi)啃畔?/P>
創(chuàng)建多列索引
mysql> create table index5(
-> id int,
-> name varchar(20),
-> sex char(4),
-> index index5_ns(name,sex)
-> );
Query OK, 0 rows affected (0.10 sec)
可以看出,這里使用了name字段和sex字段創(chuàng)建索引列
創(chuàng)建空間索引
mysql> create table index6(
-> id int,
-> space geometry not null,
-> spatial index index6_sp(space)
-> )engine=MyISAM;
Query OK, 0 rows affected (0.07 sec)
這里需要注意空間space字段不能為空,還有存儲引擎
在已經(jīng)存在的表上創(chuàng)建索引
創(chuàng)建普通索引
mysql> create index index7_id on example0(id);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里在現(xiàn)有表的id字段上創(chuàng)建了一條名為index7_id的索引
創(chuàng)建唯一性索引
mysql> create unique index index8_id on example1(course_id);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
此處只需要在index關(guān)鍵字前面加上unique即可
至于表中的course_id字段,最要也設(shè)置唯一性約束條件
創(chuàng)建全文索引
mysql> create fulltext index index9_info on example2(info);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
fulltext關(guān)鍵字用來設(shè)置全文引擎,此處的表必須是MyISAM存儲引擎
創(chuàng)建單列索引
mysql> create index index10_addr on example3(address(4));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
此表中address字段的長度是20,這里只查詢4字節(jié),不需要全部查詢
創(chuàng)建多列索引
mysql> create index index11_na on example4(name,address);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
索引創(chuàng)建好之后,查詢中必須有name字段才能使用
創(chuàng)建空間索引
mysql> create spatial index index12_line on example5(space);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里需要注意存儲引擎是MyISAM,還有空間數(shù)據(jù)類型
用alter table語句來創(chuàng)建索引
創(chuàng)建普通索引
mysql> alter table example6 add index index13_n(name(20));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建唯一性索引
mysql> alter table example7 add unique index index14_id(id);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建全文索引
mysql> alter table example8 add fulltext index index15_info(info);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建單列索引
mysql> alter table example9 add index index16_addr(address(4));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建多列索引
mysql> alter table example10 add index index17_in(id,name);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建空間索引
mysql> alter table example11 add spatial index index18_space(space);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
到此,三種操作方式,每種索引類別的建立就都列舉了
對于索引,重要的是理解索引的概念,明白索引的種類
更多的是自己的使用經(jīng)驗(yàn)
最后來看看索引的刪除
刪除索引
mysql> drop index index18_space on example11;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里是剛剛創(chuàng)建的一條索引
其中index18_space是索引名,example11是表名
相關(guān)文章
Linux 安裝JDK Tomcat MySQL的教程(使用Mac遠(yuǎn)程訪問)
這篇文章主要介紹了Linux 安裝JDK Tomcat MySQL(使用Mac遠(yuǎn)程訪問),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
使用xtrabackup實(shí)現(xiàn)mysql備份
Xtrabackup 是percona公司的開源項(xiàng)目,用以實(shí)現(xiàn)類似innodb官方的熱備份工具InnoDB Hot Backup的功能,能夠非??焖俚貍浞菖c恢復(fù)MySQL數(shù)據(jù)庫。今天我們就來詳細(xì)討論下Xtrabackup的使用方法2016-11-11
系統(tǒng)高吞吐量下的數(shù)據(jù)庫重復(fù)寫入問題分析解決
這篇文章主要介紹了系統(tǒng)高吞吐量下的數(shù)據(jù)庫重復(fù)寫入問題分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
MySQL 根據(jù)條件多值更新的實(shí)現(xiàn)
本文主要介紹了MySQL 根據(jù)條件多值更新的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03

