MySQL/JDBC 中的动态 ENUM
我即将为日志系统设计一个数据库。
许多字符串列的值数量有限,但事先并不知道: 例如发送消息的模块的名称或源主机名。
我想将它们存储为 MySQL 枚举以节省空间。 因此,我们的想法是,枚举会随着遇到新值而增长。
我将从这样的列开始:
host ENUM('localhost')
然后,在 Java 中,我会在启动时一次加载为主机名定义的枚举值(我如何使用 MySQL/JDBC 做到这一点??),并且每当我遇到新主人。
您认为这是可行的/好主意吗? 你曾经做过类似的事情吗?
预先感谢您的建议。
拉斐尔
I am about to design a database for a logging system.
Many of the String columns will have a limited amount of values, but not known in advance:
Like the name of the modules sending the messages, or the source hostnames.
I would like to store them as MySQL Enums to save space.
So the idea would be that the Enums grow as they encounter new values.
I would start with a column like :
host ENUM('localhost')
Then, in Java, I would load on startup the enum values defined for the hostnames at a time (how do I do that with MySQL/JDBC ??), and I would alter the Enum whenever I encounter a new host.
Do you think it is feasible / a good idea ?
Have you ever done something like that ?
Thanks in advance for your advice.
Raphael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个好主意。 ENUM 不是为此而设计的。
您可以创建单独的表(host_id,host_name)并在主表中使用引用。示例:
UPD:
我认为存储主机的最佳方法是 varchar/text 字段。这是最简单、最快的方法。我想你不用担心空间问题。
尽管如此。
对主机使用第二个表会减少大小,但会使写入日志变得复杂。使用 ENUM 会使写入变得复杂并显着降低性能。
This is a not good idea. ENUM designed not for that.
You can just create separate table (host_id, host_name) and use refference in main table. Example:
UPD:
I think the best way to storing host is varchar/text field. It is easiest and fastest way. I think you need not worry about the space.
Nonetheless.
Using the second table for hosts will reduce the size, but will complicate writing logs. Using ENUM complicate writing and significantly reduce the performance.