mysql 中低基数列(和无意义索引)的替代品?

发布于 2024-10-01 16:39:14 字数 412 浏览 1 评论 0原文

所以我的网站上有一个用于照片的 mysql 表。每张照片都与两组实体之一相关联,因此有两列; nEntity1FK、nEntity2FK。

每行中只有其中一列始终不为零。因此,nEntity1FK 中大约 50% 的行具有“0”,nEntity2FK 中大约 50% 的行具有“0”。

到目前为止一切都很好。直到我添加 WHERE nEntity1FK=0 子句。事情突然变得非常慢,即使我有一个LIMIT 0,50来配合它。

我有点明白为什么。我知道低基数索引很糟糕并且通常不会被使用。那么还有什么选择呢?我唯一能想到的就是将我的所有照片信息保存在两个单独的表中,而不是一个表中(并有一个视图将它们连接在一起以进行需要这两个表的查询)。这对我来说似乎很混乱/麻烦,有更好的选择吗?

谢谢大家。

So I have a mysql table for photos on my website. Every photo is tied to one of two sets of entities, so has two columns; nEntity1FK, nEntity2FK.

Exactly one of these columns is always non-zero in every row. As such about 50% of the rows have "0" in nEntity1FK, and about 50% have "0" in nEntity2FK.

All well and good so far. Until I add a WHERE nEntity1FK=0 clause. Things suddenly get pretty slow, even if I have a LIMIT 0,50 to go with it.

I kinda understand why. I get that low-cardinality indexes are bad and generally don't get used. What's the alternative though? The only thing I can think of is to keep all of my photo information in 2 separate tables instead of one (and have a view that joins them together for queries that need both). This seems messy/hassle to me, is there a better option?

Thanks all.

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

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

发布评论

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

评论(1

朦胧时间 2024-10-08 16:39:14

解决方案是使用位图索引,但据我所知 MySql 不支持它。
是否可以将数据拆分到两个表中?一种是 WHERE nEntity1FK=0,另一种是 WHERE nEntity1FK=1

The solution would be using bitmap index, but as I know MySql does not support it.
Would it be possible to split the data into two tables? One where WHERE nEntity1FK=0 and one WHERE nEntity1FK=1

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