简体   繁体   中英

Key-{Key-Value} pair in Database table - how to model?

Having trouble modeling DB Table, whose key (in key-value) is configurable.

So Basically i want to read battery data, which has key=ParamName, KeyValue=value of param name. Also, each battery has its own IP address. Examples of ParamName would be 'BatteryStatus', 'BatteryTemperature', 'BatteryCurrent' etc.

My Java program would read the battery information using IP address, and get the values of all required ParamNames. Now I could have easily defined the table with each ParamName as a column: IP | BatteryStatus | BatteryTemperature| etc

But the problem is the ParamNames are defined in a configurable file, and I should be able to add new ParamNames or delete existing ParamNames without touchig code or DB. So I cannot use a fixed table structure then?

If I create something like below, it will duplicate the IP's

IP            | ParamName         |  ParamValue
102.103.123.1 | BatteryStatus     | "normal"
102.103.123.1 | BatteryTemperature| 32
102.103.123.1 | BatteryCurrent    | 220
102.103.123.2 | BatteryStatus     | "normal"
102.103.123.2 | BatteryTemperature| 35
etc.

As you can see, im trying to store a Key-{Key-Value} pair in DB. Any ideas how to do this effectively?

Having a triplet table as in your example is pretty close, assuming proper index on IP and ParamName .

The first optimisation I'd do is to replace ParamName with an INT column, with either another table that ParamName would be a foreign key to, or a lookup hashtable in your client code.

You could do the same for IP (a linked table, not the lookup hashtable), or you could just translate them to the numeric value and use it directly ( What type should I store IP addresses for MySQL? ), which is optimal both space- and performance-wise, just not (directly) very human-readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM