MySQL Crash Course #12# Chapter 18. Full-Text Searching
INDEX
由于性能、智能结果等多方面原因,在搜索文本时,全文搜索一般要优于通配符和正则表达式,前者为指定列建立索引,以便快速找到对应行,并且将结果集智能排序。启用查询扩展可以让我们得到未必包含关键字的相关行,启用布尔模式可以让我们指定搜索内容不应包含的单词、各个关键词的权重等。
WARNING
不是所有数据库引擎都支持全文搜索。MyISAM 支持全文索引,InnoDB 不支持全文索引。
PS. 据说 MySQL 5.6 以上版本的 InnoDB 支持全文索引,语法格式和 MyISAM 的全文索引类似。
Understanding Full-Text Searching
通配符和正则表达式都很强大,但是它们有几个很严重的缺点:
- 性能:通配符和正则表达式总是和表中的每一行匹配,因此当行数增加时这两种匹配方式非常耗时。
- 精确控制:通配符和正则表达式很难精确控制匹配什么不匹配什么。
- 智能结果:例如说,不论字段内容中有一个匹配还是多个匹配,通配符和正则表达式都一视同仁的返回该行;如果字段内容不包含匹配,但包括相关(相近)词不会返回该行。
上述的缺点都可以通过全文索引来解决。当使用全文搜索的时候, MySQL 不需要单独地匹配每行,而是创建单词(在指定的列中)的索引,这样 MySQL 就可以快速而有效地确定哪些词匹配,哪些不匹配等等。
可以理解为 MySQL 为指定列生成了一个目录,该目录标注了 包含 xxx 单词的行是第1、4、8、9行(假设),借助这个目录,搜索 xxx 单词时就可以很快找到对应行1、4、8、9了,类似于用空间换取时间。
Using Full-Text Searching
CREATE TABLE productnotes
(
note_id int NOT NULL AUTO_INCREMENT,
prod_id char(10) NOT NULL,
note_date datetime NOT NULL,
note_text text NULL ,
PRIMARY KEY(note_id),
FULLTEXT(note_text)
) ENGINE=MyISAM;
PS. 全文索引多个字段也是可以的!
一旦定义了全文索引,MySQL 会自动维护这个索引,当增加记录、删除记录,索引会相应的变化
Don't Use FULLTEXT When Importing Data
Updating indexes takes timenot a lot of time, but time nonetheless. If you are importing data into a new table, you should not enable FULLTEXT indexing at that time. Rather, first import all of the data, and then modify the table to define FULLTEXT. This makes for a much faster data import (and the total time needed to index all data will be less than the sum of the time needed to index each row individually).
Performing Full-Text Searches
mysql> SELECT note_text
-> FROM productnotes
-> WHERE Match(note_text) Against('rabbit');
+----------------------------------------------------------------------------------------------------------------------+
| note_text |
+----------------------------------------------------------------------------------------------------------------------+
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now. |
| Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
+----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
Match(colunm_name必须和定义为 FULLTEXT的列一致) 和 Against('xxx') ,记住这两个函数!另外,搜索是大小写不敏感的!
事实上,利用通配符也可以很方便地达到上面的效果,不过性能和结果都会有不同:
mysql> SELECT note_text
-> FROM productnotes
-> WHERE note_text LIKE '%rabbit%';
+----------------------------------------------------------------------------------------------------------------------+
| note_text |
+----------------------------------------------------------------------------------------------------------------------+
| Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now. |
+----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
我们很震惊地发现,虽然行数是没错,不过顺序却不一致 。 。 。
这是因为 全文搜索有一个重要特性:结果排名(the ranking of results),排名高的优先返回。查看列的排名:
SELECT note_text,
Match(note_text) Against('rabbit') AS rank
FROM productnotes;
rank 越高的排名越前,这个与关键字出现与否、出现在前面还是后面等因素有关。
Using Query Expansion
When query expansion is used, MySQL makes two passes through the data and indexes to perform your search:
First, a basic full-text search is performed to find all rows that match the search criteria.
Next, MySQL examines those matched rows and selects all useful words (we'll explain how MySQL figures out what is useful and what is not shortly).
Then, MySQL performs the full-text search again, this time using not just the original criteria, but also all of the useful words.
Using query expansion you can therefore find results that might be relevant, even if they don't contain the exact words for which you were looking.
mysql> SELECT note_text
-> FROM productnotes
-> WHERE Match(note_text) Against('rabbit' WITH QUERY EXPANSION);
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now. |
| Customer complaint:
Circular hole in safe floor can apparently be easily cut with handsaw. |
| Customer complaint:
Sticks not individually wrapped, too easy to mistakenly detonate all at once.
Recommend individual wrapping. |
| Customer complaint:
Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead. |
| Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils. |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)
Boolean Text Searches
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('+rabbit +bait"' IN BOOLEAN MODE);
更多操作参见原书第 18 章,通过各种符号来指明关键词优先级、包含什么单词、排除什么单词等。
Full-Text Search Usage Notes
重要的说明:
- MySQL 不会给短单词建立索引,短单词默认定义为三个字母以内的。
- MySQL 有一个内建的停用词列表,它的下场和短单词一样,当然这个也是可以手动修改的,具体方法可以搜索资料、查查文档。
- 许多单词出现频繁,以至于搜索它们将毫无用处(返回的结果太多)。 因此,MySQL授予50%的规则,一个单词出现在50%或更多的行中,它被视为一个停用词并被有效地忽略。 (50%的规则不用于IN BOOLEAN模式)。
- 如果表中的行少于三行(也就是两行或者两行以下,因为每个但凡出现的单词的出现次数总是至少为行数的50%),全文搜索不返回任何结果。
- 单词内的单引号会被忽略,例如 don't 的索引为 dont
- 没有单词分隔符(包括日语和中文)的语言将不会正确返回全文结果。
那么如何实现中文分词&全文索引呢?待更新
MySQL Crash Course #12# Chapter 18. Full-Text Searching的更多相关文章
- MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API
索引 正则表达式:MySQL only supports a small subset of what is supported in most regular expression implemen ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- MySQL Crash Course #11# Chapter 20. Updating and Deleting Data
INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...
- MySQL Crash Course #10# Chapter 19. Inserting Data
INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
- MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE
索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
- MySQL Crash Course #20# Chapter 28. Managing Security
限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...
- MySQL Crash Course #17# Chapter 25. 触发器(Trigger)
推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...
随机推荐
- vue 报错./lib/html5-entities.js this relative module was not
原文参考http://www.bozhiyue.com/web/yuyan/2017/0501/1236324.html 然后就把他俩注视了,是不报错了,但是也没有运行不出来: 居然是因为早上我360 ...
- uid列表来讲讲我是如何利用php数组进行排重的
经常接到要对网站的会员进行站内信.手机短信.email进行群发信息的通知,用户列表一般由别的同事提供,当中难免会有重复,为了避免重复发送,所以我在进行发送信息前要对他们提供的用户列表进行排重. 假如得 ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- 敌兵布阵---hud1166(线段树或者树状数组模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 线段树中对某一点的值进行改变: #include<iostream> #includ ...
- Pots--poj(bfs,输出路径)
http://poj.org/problem?id=3414 题意: 给你两个容量为a,b的杯子:有3个操作: 1:FILL(i):把第i个杯子从水库中装满: 2:DROP(i):把第i个杯子清空: ...
- BZOJ4856 病毒感染 [Jsoi2016] dp
正解:区间dp+辅助dp 解题报告: 先放个传送门qwq 然后这题,又是一道看不懂题目的玩意儿:( 大概是语文太差 那就先解释下 其实只是一个点比较难明白就是它港 "假设JYY 进入i村庄并 ...
- struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号
1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...
- kettle 安装mysql 驱动
错误连接数据库 [mysql] : org.pentaho.di.core.exception.KettleDatabaseException: Error occurred while trying ...
- 14 jmeter性能测试实战--数据库MySQL
需求 测试用户表(对用户表select操作) 测试步骤 1.MySQL驱动下载并安装. 2.测试计划面板点击“浏览”按钮,将JDBC驱动(mysql-connector-java.jar)添加进来. ...
- [GDAL]写入shp
C#通过Wkt码构建shp,记录写不进去! static void WriteVectorFile() { string strVectorFile = "E:\\"; // 注册 ...