欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL筆記之索引的使用

 更新時(shí)間:2013年05月03日 17:58:11   作者:  
索引是創(chuàng)建在表上的,對數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)其作用主要在于提高查詢的速度,降低數(shù)據(jù)庫系統(tǒng)的性能開銷

索引是創(chuàng)建在表上的,對數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)

其作用主要在于提高查詢的速度,降低數(shù)據(jù)庫系統(tǒng)的性能開銷

通過索引,查詢數(shù)據(jù)不必讀完記錄的全部信息進(jìn)行匹配,而是只查詢索引列

索引相當(dāng)于字典中的音序表,要查詢某字時(shí)可以在音序表中找到

然后直接跳轉(zhuǎn)到那一音序所在位置,而不必從字典第一頁開始翻,逐字匹配

tips:索引雖能提高查詢速度,但在插入記錄時(shí)會(huì)按照索引進(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存儲(chǔ)引擎支持全文檢索

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存儲(chǔ)引擎支持


在創(chuàng)建表時(shí)創(chuàng)建索引
創(chuàng)建普通索引

復(fù)制代碼 代碼如下:

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)建唯一性索引

復(fù)制代碼 代碼如下:

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)建全文索引

復(fù)制代碼 代碼如下:

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存儲(chǔ)引擎


創(chuàng)建單列索引

復(fù)制代碼 代碼如下:

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)建多列索引

復(fù)制代碼 代碼如下:

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)建空間索引

復(fù)制代碼 代碼如下:

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字段不能為空,還有存儲(chǔ)引擎


在已經(jīng)存在的表上創(chuàng)建索引
創(chuàng)建普通索引

復(fù)制代碼 代碼如下:

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)建唯一性索引

復(fù)制代碼 代碼如下:

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)建全文索引

復(fù)制代碼 代碼如下:

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存儲(chǔ)引擎


創(chuàng)建單列索引

復(fù)制代碼 代碼如下:

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)建多列索引

復(fù)制代碼 代碼如下:

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)建空間索引

復(fù)制代碼 代碼如下:

mysql> create spatial index index12_line on example5(space);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

這里需要注意存儲(chǔ)引擎是MyISAM,還有空間數(shù)據(jù)類型

用alter table語句來創(chuàng)建索引
創(chuàng)建普通索引

復(fù)制代碼 代碼如下:

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)建唯一性索引
復(fù)制代碼 代碼如下:

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)建全文索引
復(fù)制代碼 代碼如下:

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)建單列索引
復(fù)制代碼 代碼如下:

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)建多列索引
復(fù)制代碼 代碼如下:

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)建空間索引
復(fù)制代碼 代碼如下:

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)

最后來看看索引的刪除


刪除索引

復(fù)制代碼 代碼如下:

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)文章

最新評論