【问题标题】:Any way to achieve fulltext-like search on InnoDB在 InnoDB 上实现类似全文搜索的任何方式
【发布时间】:2011-06-11 13:12:30
【问题描述】:

我有一个非常简单的查询:

SELECT ... WHERE row LIKE '%some%' OR row LIKE '%search%' OR  row LIKE '%string%'

要搜索some search string,但正如你所见,它单独搜索每个字符串,对性能也不利。

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

【问题讨论】:

  • innodb 不支持全文搜索...
  • 非常感谢您的启发,对您很有帮助:)

标签: sql mysql full-text-search


【解决方案1】:

使用 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;

dict是一个字典函数

【讨论】:

    【解决方案2】:

    使用 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');
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      InnoDB full-text search (FTS) is finally available in MySQL 5.6.4 release.

      这些索引在物理上表示为整个 InnoDB 表,由 SQL 关键字进行操作,例如 CREATE INDEX 语句的 FULLTEXT 子句、SELECT 语句中的 MATCH() ... AGAINST 语法和 OPTIMIZE TABLE 语句. 来自FULLTEXT Indexes

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 2021-10-24
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        相关资源
        最近更新 更多