MySQL筆記之索引的使用_MySQL教程

      編輯Tag賺U幣
      教程Tag:MySQL索引添加

      推薦:MySQL筆記之修改表的實現方法
      我們在創建表的過程中難免會考慮不周,因此后期會修改表。本篇文章就介紹了在mysql中修改表的實現方法。需要的朋友參考下

      索引是創建在表上的,對數據庫表中一列或多列的值進行排序的一種結構

      其作用主要在于提高查詢的速度,降低數據庫系統的性能開銷

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

      索引相當于字典中的音序表,要查詢某字時可以在音序表中找到

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

      tips索引雖能提高查詢速度,但在插入記錄時會按照索引進行排序,因此降低了插入速度

          最好的操作方式是先刪除索引,插入大量記錄后再創建索引


      索引分類

      1.普通索引:不附加任何限制條件,可創建在任何數據類型中

      2.唯一性索引:使用unique參數可以設置索引為唯一性索引,在創建索引時,限制該索引的值必須唯一,主鍵就是一種唯一性索引

      3.全文索引:使用fulltext參數可以設置索引為全文索引。全文索引只能創建在char、varchar或text類型的字段上。查詢數據量較大的字符串類型字段時,效果明顯。但只有MyISAM存儲引擎支持全文檢索

      4.單列索引:在表中單個字段上創建的索引,單列索引可以是任何類型,只要保證索引只對應一個一個字段

      5.多列索引:在表中多個字段上創建的索引,該索引指向創建時對應的多個字段

      6.空間索引:使用spatial參數可以設置索引為空間索引,空間索引只能建立在空間數據類型上比如geometry,并且不能為空,目前只有MyISAM存儲引擎支持


      在創建表時創建索引
      創建普通索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> create table index1(
      -> id int,
      -> name varchar(20),
      -> sex boolean,
      -> index(id)
      -> );
      Query OK, 0 rows affected (0.11 sec)

      此處在id字段上創建索引,show create table可查看


      創建唯一性索引

      復制代碼 代碼如下:www.wf0088.com

      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字段創建了一個名為index2_id的索引

      這里的id字段可以不設置唯一性約束,但這樣一來索引就沒有作用


      創建全文索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> create table index3(
      -> id int,
      -> info varchar(20),
      -> fulltext index index3_info(info)
      -> )engine=MyISAM;
      Query OK, 0 rows affected (0.07 sec)

      要注意創建全文索引時只能使用MyISAM存儲引擎


      創建單列索引

      復制代碼 代碼如下:www.wf0088.com

      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

      這么做的目的在于提高查詢速度,對于字符型的數據不用查詢全部信息


      創建多列索引

      復制代碼 代碼如下:www.wf0088.com

      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字段創建索引列


      創建空間索引

      復制代碼 代碼如下:www.wf0088.com

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


      在已經存在的表上創建索引
      創建普通索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> create index index7_id on example0(id);
      Query OK, 0 rows affected (0.07 sec)
      Records: 0 Duplicates: 0 Warnings: 0

      這里在現有表的id字段上創建了一條名為index7_id的索引


      創建唯一性索引

      復制代碼 代碼如下:www.wf0088.com

      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關鍵字前面加上unique即可

      至于表中的course_id字段,最要也設置唯一性約束條件


      創建全文索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> create fulltext index index9_info on example2(info);
      Query OK, 0 rows affected (0.07 sec)
      Records: 0 Duplicates: 0 Warnings: 0

      fulltext關鍵字用來設置全文引擎,此處的表必須是MyISAM存儲引擎


      創建單列索引

      復制代碼 代碼如下:www.wf0088.com

      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字節,不需要全部查詢


      創建多列索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> create index index11_na on example4(name,address);
      Query OK, 0 rows affected (0.16 sec)
      Records: 0 Duplicates: 0 Warnings: 0

      索引創建好之后,查詢中必須有name字段才能使用


      創建空間索引

      復制代碼 代碼如下:www.wf0088.com

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

      這里需要注意存儲引擎是MyISAM,還有空間數據類型

      用alter table語句來創建索引
      創建普通索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example6 add index index13_n(name(20));
      Query OK, 0 rows affected (0.16 sec)
      Records: 0 Duplicates: 0 Warnings: 0


      創建唯一性索引
      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example7 add unique index index14_id(id);
      Query OK, 0 rows affected (0.20 sec)
      Records: 0 Duplicates: 0 Warnings: 0


      創建全文索引
      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example8 add fulltext index index15_info(info);
      Query OK, 0 rows affected (0.08 sec)
      Records: 0 Duplicates: 0 Warnings: 0


      創建單列索引
      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example9 add index index16_addr(address(4));
      Query OK, 0 rows affected (0.16 sec)
      Records: 0 Duplicates: 0 Warnings: 0


      創建多列索引
      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example10 add index index17_in(id,name);
      Query OK, 0 rows affected (0.16 sec)
      Records: 0 Duplicates: 0 Warnings: 0


      創建空間索引
      復制代碼 代碼如下:www.wf0088.com

      mysql> alter table example11 add spatial index index18_space(space);
      Query OK, 0 rows affected (0.06 sec)
      Records: 0 Duplicates: 0 Warnings: 0

      到此,三種操作方式,每種索引類別的建立就都列舉了

      對于索引,重要的是理解索引的概念,明白索引的種類

      更多的是自己的使用經驗

      最后來看看索引的刪除


      刪除索引

      復制代碼 代碼如下:www.wf0088.com

      mysql> drop index index18_space on example11;
      Query OK, 0 rows affected (0.08 sec)
      Records: 0 Duplicates: 0 Warnings: 0

      這里是剛剛創建的一條索引

      其中index18_space是索引名,example11是表名

      分享:PHP mysqli擴展庫 預處理技術的使用分析
      本篇文章,介紹了PHP mysqli擴展庫 預處理技術的使用分析。需要的朋友參考下

      來源:模板無憂//所屬分類:MySQL教程/更新時間:2013-05-04
      相關MySQL教程