mysql索引三(全文索引)

2021-08-19 21:15:46 字數 1233 閱讀 7223

前面分別介紹了mysql索引一(普通索引)、mysql索引二(唯一索引)。

本文學習mysql全文索引。

在mysql中,建立全文索引相對比較簡單。例如:我們有乙個文章表(article),其中有主鍵id(id)、文章標題(title)、文章內容(content)三個字段。現在我們希望能夠在title和content兩個列上建立全文索引,article表及全文索引的建立sql語句如下:

create table `article` (

`id` int(10) unsigned not null auto_increment,

`title` varchar(200) default null,

`content` text,

primary key (`id`),

fulltext key `title` (`title`,`content`)

) engine=myisam default charset=utf8;

上面就是在建立表的同時建立全文索引的sql示例。此外,如果我們要給已經存在的表的指定字段建立全文索引,同樣以article表為例,我們可以使用如下sql語句進行建立:

alter table article add fulltext(title, content)
有了全文索引,就可以用select查詢命令去檢索那些包含著乙個或多個給定單詞的資料記錄了。下面是這類查詢命令的基本語法:

select * from tablename 

where match(column1, column2) against('word1', 'word2', 'word3')

強烈注意:mysql自帶的

全文索引只能用於資料庫引擎為myisam的資料表,如果是其他資料引擎,則全文索引不會生效。此外,

mysql自帶的全文索引只能對英文進行全文檢索,目前無法對中文進行全文檢索。如果需要

對包含中文在內的文字資料進行全文檢索,我們需要採用sphinx(斯芬克斯)/coreseek技術來處理中文。

注:目前,使用mysql自帶的全文索引時,

如果查詢字串的長度過短將無法得到期望的搜尋結果。mysql全文索引所能找到的詞預設最小長度為4個字元。另外,如果查詢的字串包含停止詞,那麼該停止詞將會被忽略。

注:如果可能,請盡量先建立表並插入所有資料後再建立全文索引,而不要在建立表時就直接建立全文索引,因為前者比後者的全文索引效率要高。

mysql全文索引的坑 MySQL全文索引問題

我有乙個包含以下資料的 文章 mysql select from articles id title body 1 mysql tutorial dbms stands for database 2 how to use mysql well after you went through a 3 o...

mysql全文索引

了解 solr 之後 發現全文索引也能做檢索 故了解了下 筆記如下 建立全文索引 alter table table add fulltext index fulltext table 列1 列2 查詢方式 select from table where match 列1 列2 against 查詢...

mysql全文索引

舊版的mysql的全文索引只能用在myisam 的char varchar和text的字段上。不過新版的mysql5.6.24上innodb引擎也加入了全文索引,所以具體資訊要隨時關注官網,create table article id int auto increment not null pri...