如何索引该表(a_level、b_level、item_id)

发布于 2024-12-09 15:32:32 字数 624 浏览 1 评论 0原文

我有一个项目表,每个项目都有一个 a_level、b_level 和一个 item_id。任何 b_level 仅专用于一个 a_level(例如:b_level 14 仅是 a_level 2 的“子级”)

假设我们有数百万个项目,所有这些项目都被 INSERT 一次,然后仅请求 SELECT。

如果我根据 item_id 选择一个项目,那么我需要为 item_id 列建立索引。这将使 MySQL 查找所有数百万个项目,这很糟糕,因为我已经有了 a_level 和 b_level 信息。所以我想如果我根据特定级别选择一个项目并且我在该列上有一个索引,那么MySQL将不必查看所有数百万个项目,而只需查看具有该特定级别的项目。

如果我在a_level、b_level(当然还有item_id)上都进行索引并且SELECT WHERE a_level= b_level= item_id= 会很糟糕吗?我想只有 INDEX on b_level 和 item_id 以及 SELECT WHERE b_level= AND item_id= 就足够了/最好的解决方案?

那么,既然我有 a_level 和 b_level(正如我所说,任何 b_level 都是一个 a_level 的“子级”),那么为选取一项而创建的最有效的 SELECT 和 INDEX 是什么?

I have a table of items, each of it has an a_level, b_level, and an item_id. Any b_level is dedicated to only one a_level (example: b_level 14 is "child" of a_level 2 only)

Lets say we have million of items all of them are INSERTed once and then only SELECTs are requested.

If i SELECT an item based on item_id, then i need to index the item_id column. This will make the MySQL to look all millions of items, which is bad, since i already have the a_level and b_level information. So i guess if i SELECT an item based on a specific level and i have an index on that column, the MySQL will not have to look all millions of items, just the items with that particular level.

If i INDEX both on a_level, b_level (and of course item_id) and SELECT WHERE a_level= b_level= item_id= will it be bad? I guess only INDEX on b_level and item_id and SELECT WHERE b_level= AND item_id= will be enough/the best solution?

So, since i have a_level and b_level (which any b_level as i said is "child" of only one a_level) what will be the most efficient SELECT and INDEXes created for picking up an item?

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

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

发布评论

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

评论(2

书间行客 2024-12-16 15:32:32

您当然可以为每一列建立索引。如果这样做,MySQL将使用索引合并< /code>优化以将多个索引应用于单个查询。但是,为了提高效率,您可能需要使用复合索引(多个列上的单个索引)。 MySQL复合索引在优化时遵循左前缀规则< /a>.如果 SELECT 语句受到索引左前缀中包含的术语的限制,则使用该索引。例如,如果您有

SELECT * FROM t WHERE a_level = 1 AND b_level = 2

,则适当的索引必须包含 a_levelb_level 作为第一列。换句话说,(a_level, b_level) 的索引可以索引诸如

SELECT * FROM t WHERE a_level = 1
SELECT * FROM t WHERE a_level = 1 AND b_level = 2

但不能索引

SELECT * FROM t WHERE b_level = 2

的查询,因为 b_level 不是索引的左前缀。

您可能首先想要对最常执行的选择进行基准测试,并基于该选择创建索引,只要它们遵循左前缀规则即可。您可能希望对几个不同的 SELECT 查询使用多个索引,以免覆盖整个表。在不准确了解数据和查询的情况下,要完美回答这个问题并不容易。

但是,如果您确定永远不会再次写入表中,并且空间不是问题,您也可以用索引覆盖整个内容。

You can certainly index every column. If you do, MySQL will use index merge optimization to apply many indexes to a single query. However, for more efficiency, you might want to use composite indexes (single index on multiple columns). MySQL composite indexes are used in optimization by following the left-prefix rule. If the SELECT statement is restricted by terms that are included in a left-prefix of an index, that index is used. For example, if you have

SELECT * FROM t WHERE a_level = 1 AND b_level = 2

then the appropriate index would have to include a_level or b_level as the first columns. In other words, an index for (a_level, b_level) could index queries such as

SELECT * FROM t WHERE a_level = 1
SELECT * FROM t WHERE a_level = 1 AND b_level = 2

but not

SELECT * FROM t WHERE b_level = 2

because b_level is not a left-prefix of the index.

You'd probably first want to benchmark which of the selects you're performing most often and create indexes based on that, as long as they follow the left-prefix rule. You might want to use several indexes for a few of the different SELECT queries in order to keep from blanketing the entire table. It's not easy to perfectly answer this question without knowing the data and queries exactly.

However, if you're sure you're never going to write into the table again, you might as well cover the entire thing with an index, if space is not an issue.

GRAY°灰色天空 2024-12-16 15:32:32

如果您经常按一列或一组列进行选择,则对该列或一组列建立索引。索引不会查找所有数百万个项目,这就是它们是索引的原因(没有索引,它确实会查找所有数百万个项目)

if you do select by a column or set of colums frequently, then index that column or set of columns. Indexes don't look all millions of items, that's why they're indexes (without an index, it would indeed look all millions of items)

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