As always with these questions, "it depends."
A table of terrible keys.
This approach is based on a table listing the fields and their values ββas separate records.
CustomFields(clientId int, fieldName sysname, fieldValue varbinary)
Benefits:
- Infinitely flexible
- Easy to implement
- Easy to index
- nonexistent values ββdo not take up space
Inconvenience:
- Displaying a list of all records with a complete list of fields is a very dirty query
Microsoft path
Microsoft's way of doing this is sparse columns (introduced in SQL 2008)
Benefits:
- Blessed by people designing SQL Server
- records can be requested without the need for mosaic vaults
- Fields without data do not take up disk space
Disadvantage:
Xml tax
You can add the xml field to the table, which will be used to store all the "additional" fields.
Benefits:
- unlimited flexibility
- can be indexed
- efficient storage (when it fits on the page)
- With some xpath gymnastics, fields can be included in a flat record set.
- can be implemented circuit with collections of circuits
Disadvantages:
- it is not clear that in the field
- SQL Server xquery support has gaps that sometimes cause your data to become a real nightmare.
There may be more solutions, but for me these are the main rivals. Which one to choose:
- The key value seems appropriate when the number of additional fields is limited. (say no more than 10-20 or so)
- Sparse columns are more suitable for data with many properties that are infrequently populated. It sounds more appropriate when you can have many extra fields. Column
- xml is very flexible, but a pain in the request. Suitable for solutions that are rarely written and rarely requested. i.e.: do not start aggregates, etc. to the data stored in this field.
source share