开发者

Dynamic ENUMs in 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


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:

CREATE TABLE `host` (
    `host_id` INT(10) NOT NULL AUTO_INCREMENT,
    `host_name` VARCHAR(50) NULL DEFAULT NULL,
    PRIMARY KEY (`host_id`)
)

CREATE TABLE `log` (
    `log_id` INT(10) NOT NULL AUTO_INCREMENT,
    `host_id` INT(10) NULL DEFAULT NULL,
    ...
    PRIMARY KEY (`log_id`),
    INDEX `FK__host` (`host_id`),
    CONSTRAINT `FK__host` FOREIGN KEY (`host_id`) REFERENCES `host` (`host_id`) ON UPDATE CASCADE ON DELETE CASCADE
)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜