Sql 有没有办法在InnoDB上实现类似全文的搜索

Sql 有没有办法在InnoDB上实现类似全文的搜索,sql,mysql,full-text-search,Sql,Mysql,Full Text Search,我有一个非常简单的问题: SELECT ... WHERE row LIKE '%some%' OR row LIKE '%search%' OR row LIKE '%string%' 搜索一些搜索字符串,但正如您所看到的,它会单独搜索每个字符串,这对性能也没有好处 是否有一种方法可以在InnoDB表上使用like重新创建类似全文的搜索。当然,我知道我可以使用类似Sphinx的东西来实现这一点,但我正在寻找一个纯MySQL解决方案。使用PHP来构造查询。这是一次可怕的黑客攻击。一旦被看见,

我有一个非常简单的问题:

SELECT ... WHERE row LIKE '%some%' OR row LIKE '%search%' OR  row LIKE '%string%'
搜索
一些搜索字符串,但正如您所看到的,它会单独搜索每个字符串,这对性能也没有好处


是否有一种方法可以在InnoDB表上使用like重新创建类似全文的搜索。当然,我知道我可以使用类似Sphinx的东西来实现这一点,但我正在寻找一个纯MySQL解决方案。

使用PHP来构造查询。这是一次可怕的黑客攻击。一旦被看见,它就不可能被看不见

$words=dict($userQuery);
$numwords = sizeof($words);
$innerquery="";
for($i=0;$i<$numwords;$i++) {
    $words[$i] = mysql_real_escape_string($words[$i]);
    if($i>0) $innerquery .= " AND ";
    $innerquery .= "
        (
            field1 LIKE \"%$words[$i]%\" OR
            field2 LIKE \"%$words[$i]%\" OR
            field3 LIKE \"%$words[$i]%\" OR
            field4 LIKE \"%$words[$i]%\"
        )
    ";
}


SELECT fields FROM table WHERE $innerquery AND whatever;
$words=dict($userQuery);
$numwords=sizeof($words);
$innerquery=“”;
对于($i=0;$i0)$innerquery.=“AND”;
$innerquery.=”
(
字段1类似\“%$words[$i]%”或
字段2类似\“%$words[$i]%”或
字段3类似\“%$words[$i]%”或
字段4类似\“%$words[$i]%”
)
";
}
从表中选择字段,其中包含$innerquery和其他内容;

dict是一个字典功能

使用myisam全文表将索引返回到innodb表中,例如:

使用innodb构建您的系统:

create table users (...) engine=innodb;

create table forums (...) engine=innodb;

create table threads
(
forum_id smallint unsigned not null,
thread_id int unsigned not null default 0,
user_id int unsigned not null,
subject varchar(255) not null, -- gonna want to search this... !!
created_date datetime not null,
next_reply_id int unsigned not null default 0,
view_count int unsigned not null default 0,
primary key (forum_id, thread_id) -- composite clustered PK index
)
engine=innodb;
现在是全文搜索表,我们将使用它将索引返回到innodb表中。您可以使用触发器或夜间批处理更新等来维护此表中的行

create table threads_ft
(
forum_id smallint unsigned not null,
thread_id int unsigned not null default 0,
subject varchar(255) not null,
fulltext (subject), -- fulltext index on subject
primary key (forum_id, thread_id) -- composite non-clustered index 
)
engine=myisam;
最后是从php/应用程序调用的搜索存储过程:

drop procedure if exists ft_search_threads;
delimiter #

create procedure ft_search_threads
(
in p_search varchar(255)
)
begin

select
 t.*,
 f.title as forum_title,
 u.username,
 match(tft.subject) against (p_search in boolean mode) as rank
from
 threads_ft tft
inner join threads t on tft.forum_id = t.forum_id and tft.thread_id = t.thread_id
inner join forums f on t.forum_id = f.forum_id
inner join users u on t.user_id = u.user_id
where
 match(tft.subject) against (p_search in boolean mode) 
order by 
 rank desc
limit 100;

end;

call ft_search_threads('+innodb +clustered +index');
希望这有帮助:)

这些索引在物理上表示为整个InnoDB表,这些表由SQL关键字(如CREATEINDEX语句的FULLTEXT子句、MATCH()…)作用。。。对照SELECT语句和OPTIMIZE TABLE语句中的语法。
From

innodb不支持全文搜索……非常感谢您的启发,非常感谢您的帮助:)