我有一个包含100万条记录的表,并希望以更快的方式应用任何搜索查询来获取记录。因为我对mysql全文搜索很不好。
我做了以下测试:
MATCH AGAINS
,它快速返回结果。MATCH AGAINS
并返回非常慢。MATCH AGAINS
。它在第一次返回时非常慢,但在第二次尝试时使用相同的搜索词会相当快。我的查询是否有任何问题我应该如何通过更多优化来修改它?
select name, meaning, m.gender, m.similar
FROM
NAMES n
INNER JOIN meta m ON m.nameid = n.id
WHERE MATCH (nameandmeaning) AGAINST ('searchterm*' IN BOOLEAN MODE)
AND meaning IS NOT NULL
ORDER BY LENGTH(m.similar) DESC
LIMIT 0 , 10;
注意:nameandmeaning
是name, meaning
的组合。
我的表格结构如下:
CREATE TABLE NAMES (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
meaning VARCHAR(2000) DEFAULT NULL,
nameandmeaning VARCHAR(2000) DEFAULT NULL,
PRIMARY KEY (id),
FULLTEXT KEY constains_name(name,meaning),
FULLTEXT KEY contains_namemeaing (nameandmeaning)
) ENGINE=MYISAM AUTO_INCREMENT=67846 DEFAULT CHARSET=latin1;
答案 0 :(得分:0)
CREATE TABLE NAMES (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
meaning VARCHAR(2000) DEFAULT NULL,
-- skip this: nameandmeaning VARCHAR(2000) DEFAULT NULL,
PRIMARY KEY (id),
FULLTEXT KEY constains_name(name, meaning),
-- skip this: FULLTEXT KEY contains_namemeaing (nameandmeaning)
) ENGINE=MYISAM AUTO_INCREMENT=67846 DEFAULT CHARSET=latin1;
然后这些应该很快:
MATCH(name) AGAINST (...)
MATCH(meaning) AGAINST (...)
MATCH(name, meaning) AGAINST (...)
当你升级到InnoDB时,你应该拥有所有这三个(如果你正在做所有这三个MATCH):
FULLTEXT (name),
FULLTEXT (meaning),
FULLTEXT (name, meaning)