I once used key-value pairs in a database to create a spreadsheet (used to enter data) in which the cashier summarized his activities with the work of the cash drawer. Each k / v pair was a named cell into which the user entered a sum of money. The main reason for this approach is that the spreadsheet is highly susceptible to change. New products and services were added regularly (new cells appeared). In addition, certain cells are not needed in certain situations and may be discarded.
The application that I wrote was rewritten by the application, which broke the cash register into separate sections, presented in another table. The problem was that when adding products and services, modifications to the circuit were needed. As with all design options, there are pros and cons with respect to a certain direction compared to another. My redesign, of course, performed more slowly and consumed disk space faster; however, he was very agile and allowed to add new products and services in minutes. The only note, however, was disk consumption; I could not remember other headaches.
As already mentioned, the reason I usually consider the key-value pair approach is that users — this may be the owner of the business — want to create their own types that have a set of attributes specific to the user. In such situations, I came to the following definition.
If there is no need to retrieve data for these attributes, or the search may be delayed by the application after extracting a piece of data, I recommend storing all attributes in one text field (using JSON, YAML, XML, etc.). If there is an urgent need to retrieve data for these attributes, it becomes erratic.
You can create a single attribute table (id, item_id, key, value, data_type, sort_value), where the sort column covers the actual value in a row-sorted view. (e.g. date: "2010-12-25 12:00:00", number: "0000000001"). Or you can create separate attribute tables by data type (e.g. string_attributes, date_attributes, number_attributes). Among the many pros and cons of both approaches: the first is simpler, the second is faster. Both will make you write ugly complex queries.
Mario Oct 12 '09 at 18:57 2009-10-12 18:57
source share