mysql 复合索引我应该使用它们吗

发布于 2024-08-21 19:33:44 字数 455 浏览 4 评论 0原文

我对 mysql 的索引策略有疑问 - 主要是何时使用复合索引

我有一个相当常见的关系数据库场景,这是我的表设置:

Maintable - table consisting of "products" including brandid, merchantid 

所以我创建一个表来存储品牌和商家

Brandtable - brandname, brandid
Merchanttable - merchantname, merchantid

当我查询 Maintable 时,我有时会查询用于brandid,有时用于merchantid,有时两者兼而有之。在这种情况下最好使用什么,每列上的单个索引,还是由两者组成的复合索引?

另外,如果我想在全文搜索中包含品牌名称和商家名称,我将如何实现这一目标?

干杯:)

I got question on index strategies for mysql - mainly when to use a composite index

I have a fairly common relational db scenario, heres my table set up:

Maintable - table consisting of "products" including brandid, merchantid 

So I create a table to store the brands and merchants

Brandtable - brandname, brandid
Merchanttable - merchantname, merchantid

When I query the Maintable I sometimes query for brandid, sometimes for merchantid and sometimes both. What is best to use in this scenario, a single index on each column, or composite index made up of both?

Also if I wanted to include brandname and merchantname in a fulltext search, how would I achieve this?

Cheers :)

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

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

发布评论

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

评论(1

酒与心事 2024-08-28 19:33:44

在这种情况下,您应该为每一列使用单独的索引。如果您的查询涉及复合索引中最左边的列,MySQL 只能使用复合索引。

例如,如果您有一个复合索引 (brandid,merchantid),则当您查询 brandidbrandid 和 <代码>merchantid。如果您仅在 merchantid 上查询,则不会

In this case you should use a separate index for each column. MySQL can only use a composite index if your queries involve the leftmost columns in that composite index.

e.g., if you have a composite index of (brandid,merchantid), that index will be considered if you query on brandid or brandid and merchantid. Not if you only query on merchantid

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