Menu Close

mysql like 搜索优化方案之索引优化

首先是数据,本次的数据来源于天涯,单表大约有100w数据。

  • 表格式如下:
  1. 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)

做好了正确的索引,性能会加大很多倍,你也可以动手尝试一下。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注