MYSQL 百万条记录全文检索中文解决方案
<p style="border:0px;list-style:none;line-height:21px;color:rgb(50,62,50);font-family:simsun;font-size:14px;"> <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> 首先我的表默认是:innoDB,这种表的类型不支持全文检索,所以要先改变其类型为MyISAM。 <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> ?alter table?song engine=MyISAM; <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> 然后要在对应的要进行查找的字段上面建立全文检索的索引: <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> alter table add fulltext index(songname); <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> 如果要同时对多个字段进行检索可以这样: <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> alter table add fulltext index(songname,singername); <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> 这样做好以后呢,就可以对表进行全文检索了,速度提升是飞一般的感觉啊!现在的结果是秒出啊。可以这样去检索字段: <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> select from song where match(singername) against('周杰伦') ; <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> 或者多字段: <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> select * from song where match(singername,songname) against('风雨'); <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> ?<span style="line-height:24px;font-family:'黑体';font-size:16px;">但是,其实这个时候中文是没办法得到结果的!应为MYSQL目前只支持英文字符的全文搜索,这又叫我蛋疼了一下。后来看到一个论文说是按照把中文转化为拼音,就可以用英文检索对中文进行检索了。看了一下拼音的思路以后我就有了一个注意,<span style="line-height:24px;font-family:'黑体';font-size:16px;">在表里面为每一个需要检索的字段对应添加一个拼音字段,检索的时候直接对拼音检索就可以了。 <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> ??首先要解决的问题是在已有的表里面添加拼音字段,我用的是java,将中文转化为拼音不是难事。所以我写了一个小程序,修改了一下表的结构,为每一个需要查询的字段添加了一个对应的拼音字段。 <p style="border:0px;list-style:none;line-height:21px;color:rgb(50,50);font-family:simsun;font-size:14px;"> ??这样的话在查询的时候先把中文关键字先用小程序转化为拼音然后对拼音字段进行全文检索就能得到结果了。我的中文检索是这样子解决的。也许有其它办法,这个方法解决了我的小问题。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |