mysql 中低基数列(和无意义索引)的替代品?
所以我的网站上有一个用于照片的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是使用位图索引,但据我所知 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