共计 647 个字符,预计需要花费 2 分钟才能阅读完成。
首先是数据,本次的数据来源于天涯,单表大约有 100w 数据。
- 表格式如下:
- post_id content(text) 等等
略 略 楼主发帖以及回帖,长度比较大。
表占用硬盘大约为 2.4G, 因为数据一直还在爬取中.
当没有使用索引,用 like 查询时,返回结果大约需要 30 秒
mysql> select * from details where content like '%sdsdsd%';
Empty set (31.35 sec)
此时,增加索引至 content 字段. 由于 content 字段的类型为 text, 索引需要声明索引长度。
content 这个文字可能是很大的,比如说楼主发言有时候可能有上千字,本次索引长度暂定为 100.
此时注意,加了索引之后,文件大小略有变化,为 2.7G, 也就是说,加完索引,增加了 300M。
尝试使用之前的 sql 语句重新执行一次,返回结果不变, 也就是说 where like '%keyword%' 是不会使用索引
请使用 where like 'keyword%;
这样就会用到索引。
mysql> desc select * from details where content like '自己 %';
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | details | range | content_index | content_index | 402 | NULL | 1 | Using where |
2594 rows in set (0.60 sec)
做好了正确的索引,性能会加大很多倍,你也可以动手尝试一下。
正文完