NULL对MySQL表有什么影响? (InnoDB)
最近,我一直在思考是否值得拥有 1 个可能包含大量 NULL 列的表,或者是否最好拥有更多不包含 NULL 的表。我听说 NULL 不存储在 InnoDB 上,所以我想知道大量包含大量 NULL 的行是否有任何缺点或问题。我一直听说常见的 NULL 很糟糕,但我从来没有真正了解为什么。顺便说一句,如果这很重要的话,那么外键上的那些将是 NULL。
第二个疑问是,当我在有大量 NULL 的列上使用 INNER JOIN 时,是否存在任何性能问题?例如,如果我有 4 个外键,并且我要执行 4 个 INNER JOIN,但很可能只有 1 个不为 NULL,这会影响性能吗?谢谢
Recently, I've been thinking if it's worth to have 1 table with perhaps a lot of NULL columns or if it's better to have more tables with no NULLs. I've heard that NULL isn't stored on InnoDB, so I was wondering if there is any downside or problem with having a bunch of rows with a lot of NULLs. I have always heard that common NULLs are bad, but I have never really learned why. By the way, those will be NULLs on foreign keys, if that matters at all.
On a second doubt, is there any performance issue when I'm using INNER JOIN on columns that have a lot of NULL? Like, if I have 4 foreign keys, and I'm going to do 4 INNER JOINs, but most likely only 1 of them is not NULL, is this going to affect perfomance? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅:
NULL 被索引。
在 InnoDB 中,您可以通过使用 NULL 来减少数据行的存储要求。
See:
NULLs are indexed.
In InnoDB, you can reduce the storage requirements for your data row by using NULL.
Baron Schwartz、Peter Zaitsev、Vadim Tkachenko、Jeremy Zawodny、Arjen Lentz 和 Derek J. Balling 所著的《高性能 MySQL:优化、备份、复制等》一书中提到,表列允许NULL 使 MySQL 更难优化引用可为空列的查询。当可为空列建立索引时,每个条目需要一个额外的字节,并且可能导致固定大小索引(例如单个整数列上的索引)转换为 MyISAM 中的可变大小索引。他们说,当列需要携带缺失值时,可以使用 0 表示整数、特殊字符或空字符串。尽可能避免 NULL 和 NOT NULL。
In the book High Performance MySQL: Optimization, Backups, Replication, and More by Baron Schwartz, Peter Zaitsev, Vadim Tkachenko, Jeremy Zawodny, Arjen Lentz, and Derek J. Balling, it says table columns that allow NULLs make it harder for MySQL to optimize queries that refer to nullable columns. And when a nullable column is indexed, it requires an extra byte per entry and can cause a fixed-size index (such as an index on a single integer column) to be converted to a variable-sized one in MyISAM. They say when columns need to carry a missing value, to use 0 for integers, a special character or an empty string instead. Avoid NULL and NOT NULL whenever possible.