MySQL 全文搜索 innoDB 表的解决方法

发布于 2024-08-29 01:33:02 字数 324 浏览 11 评论 0原文

我正在设计一个使用 MySQL 作为后端数据库的内部 Web 应用程序。数据的完整性至关重要,因此我使用 innoDB 引擎来实现其外键约束功能。

我想对一种类型的记录进行全文搜索,而 innoDB 表本身不支持这种搜索。我不愿意转向 MyISAM 表,因为它们缺乏外键支持,而且它们的锁定是针对每个表而不是针对每行。

使用 MyISAM 引擎创建我需要搜索的记录的镜像表并将其用于全文搜索是否是不好的做法?这样,我只是搜索数据的副本,如果该数据发生任何事情,也没什么大不了的,因为它总是可以重新创建。

或者这是一种应该避免的尴尬方式?

谢谢。

I'm designing an internal web application that uses MySQL as its backend database. The integrity of the data is crucial, so I am using the innoDB engine for its foreign key constraint features.

I want to do a full-text search of one type of records, and that is not supported natively with innoDB tables. I'm not willing to move to MyISAM tables due to their lack of foreign key support and due to the fact that their locking is per table, not per row.

Would it be bad practice to create a mirrored table of the records I need to search using the MyISAM engine and use that for the full-text search? This way I'm just searching a copy of the data and if anything happens to that data it's not as big of a deal because it can always be re-created.

Or is this an awkward way of doing this that should be avoided?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

触ぅ动初心 2024-09-05 01:33:02

您也许可以使用触发器进行某种数据同步(如果您的 mysql 版本支持它们)。它们允许您在某些点运行 SQL 的小片段,例如在将数据插入表或从表中删除数据之后。

例如……

create trigger TRIGGER_NAME after insert on INNODB_TABLE
insert into MYISAM_TABLE select * from INNODB_TABLE
where id = last_insert_id();

每当向INNODB表插入数据时,相同的数据会自动插入到MYISAM表中。

You might be able to do some kind of data sync using triggers (if your version of mysql supports them). They allow you to run small snippets of SQL at certain points such as after data is inserted into or deleted from a table.

For example...

create trigger TRIGGER_NAME after insert on INNODB_TABLE
insert into MYISAM_TABLE select * from INNODB_TABLE
where id = last_insert_id();

... Whenever data is inserted into the INNODB table, the same data is automatically inserted into the MYISAM table.

离旧人 2024-09-05 01:33:02

我认为这确实很尴尬。也就是说,我的“快速原型可能会意外地成为生产代码”这样做的方法是这样的:

CREATE TEMPORARY TABLE search_mirror (FULLTEXT INDEX (col1, col2, ...)) Engine=MyISAM SELECT * FROM original_innodb_table;

SELECT * FROM search_mirror WHERE MATCH(col1, col2, ...) AGAINST ('foo');

DROP TEMPORARY TABLE search_mirror;

对于奖励积分,如果适合您的喜好,您可以在交易中完成所有这些操作(如果您使用的是双倍奖励)非持久连接,每个连接仅搜索一次,因为您可以消除 drop 语句)。

是的,我意识到这不是真正的镜像/复制。是的,我意识到复制表格可能会很昂贵(这里的数据集相对较小)。就像我说的,快速而肮脏的原型。青年MMV

I think its truly awkward. That said, my "quick prototype that will probably accidentally become production code" method of doing this is something like this:

CREATE TEMPORARY TABLE search_mirror (FULLTEXT INDEX (col1, col2, ...)) Engine=MyISAM SELECT * FROM original_innodb_table;

SELECT * FROM search_mirror WHERE MATCH(col1, col2, ...) AGAINST ('foo');

DROP TEMPORARY TABLE search_mirror;

And for bonus points you could do all of that inside a transaction should that suit your fancy (double bonus if you're using non-persistent connections and only searching once per connect, as you can then eliminate the drop statement).

Yes I realize that this is not true mirroring/replication. Yes I realize duping the table can be expensive (relatively small datasets here). Like I said, quick and dirty prototype. YMMV

姜生凉生 2024-09-05 01:33:02

您可以创建镜像表。这可能不太理想,因为 MyISAM 表不会尊重您的事务(如果 InnoDB 上的事务失败,您在该事务中对该 MyISAM 所做的更改仍然会出现)。

您可以使用专用的全文搜索系统,例如 Sphinx,这就是我用于全文搜索的系统搜索(因为我的数据库是InnoDB)。

You can create a mirror table. That's probably less than ideal, as the MyISAM table will not respect your transactions (if a transaction failed on InnoDB, your changes made to MyISAM in that transaction will still appear).

You could use a dedicated full-text search system like Sphinx, which is what I have used for full-text searching (Since my database is InnoDB).

忆梦 2024-09-05 01:33:02

好消息!在 MySQL 5.6 及更高版本中,全文索引可以与 InnoDB 表一起使用。如果尚未更新,您应该考虑将 MySQL 更新到 5.6 或更高版本。

对于我的应用程序来说,全文搜索非常重要,因此我只使用了 MyISAM。现在,我已将 MySQL 更新到 5.6,将数据库转换为 InnoDB 并添加了正确的约束。最好的麻烦世界。

MySQL 5.6手册-全文搜索功能

Good news! In MySQL 5.6 and up, full-text indexes can be used with InnoDB tables. You should consider updating your MySQL to 5.6 or up if you haven't as yet.

With my application full-text search was very important so I just used MyISAM. Now, I have updated MySQL to 5.6, converted the database to InnoDB and added the correct constraints. Best of bother worlds.

MySQL 5.6 Manual - Full-Text Search Functions

苦妄 2024-09-05 01:33:02

我认为解决这个问题的最简单的解决方案是创建一个用于搜索的索引表,并带有一个指向包含真实数据的表的指针。我面临着完全相同的问题,并且我不想在我的系统中使用 MyISAM 表,因为 InnoDB 表可以让我安心。

因此,我计划解决我的问题是使用 MyISAM 创建一个索引表,这样我就只能在其上建立索引的信息。同步将使用触发器来完成,这是最简单的方法。我不想复制整个表,因为这会花费大量空间。然而,仅复制所需的字段会消耗空间,从而牺牲搜索引擎的功能。

这个索引表可以理解为搜索设施的索引。与任何索引一样,它都会消耗空间。作为一种优化,该索引表上插入的数据可以只是术语,但是这样需要额外的处理来清理无用的单词以进行搜索。

I feel that the simplest solution for this problem is the creation of a index table which will be used for searches, with a pointer back to the table which contains the real data. I'm facing exactly the same problem and I do not want to use MyISAM tables for my system because of the peace of mind given by InnoDB tables.

So, what I'm planning to do with my problem is to create an index table using MyISAM, so I can have only the information to be indexed on it. Synchronization will be done using triggers, which is the most simplest way to do so. I do not want to replicate the entire table, since it will cost a lot of space. However, replicating only the desired fields will cost space at the expense of the search engine facility.

This index table can be understood as an index for searching facilities. As any index, it will cost space. As an optimization, the inserted data on this index table can be only terms, but this way extra processing is needed in order to clean useless word for searching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文