Design of an 'EAV' or 'Class/Concrete Table Inheritance' database for stock control

泄露秘密 提交于 2019-12-04 14:52:46
  • If attribute changes are few and far between, go for Table Inheritance, but the changes to the schema should be done by yourself or a DBA. Programmatically modifying your schema based on user input seems like a bad idea.

  • If attribute changes are fairly common, consider using a document-oriented database like MongoDB or CouchDB.

  • If attribute changes are common and you are restricted to relational databases, go with EAV.

I'd avoid EAV unless I'm building a medical repository with thousands of hundreds of products. Class Table Inheritance is a proper solution and altering schema at run-time isn't that bad idea.

Assume you're building an online shop were you have different products with different attributes. Usually a product is using a set of attributes. By using Class Table Inheritance pattern you can have one table per product set inheriting a common table with common product attributes. When a new attribute is needed you have two options:

  • alter the set table and add a new column with the attribute name
  • create a new set (i.e new table) with the new column and set the current product set to inherit the new one, thus adding the new attribute.

Altering table at run-time may indeed cause problems due to locking occurring during alter. However as of MySQL 5.6 a programmer can specify a LOCK type i.e LOCK=NONE and also ALTER ONLINE|OFFLINE. Obviously DB vendors are working in this direction to allow for database alternations during run-time.

Table Locking issues can also be avoided/minimized. Lock issues usually occur when altering huge tables i.e 100k+ records. However inheritance allows for distributing products in different sets and thus having a relatively small amount of records per table.

For instance if we have 10 different types of products, we have 10 different tables. Assume we have a 10 000 products per type, this means that we have over 100 000 products, however adding a new attribute in a set will be in effect adding a new column in a table with 10 000 rows only. Adding a new column in a table with 10k rows will take less then a second, so is altering database schema at run-time when dealing with Class Table Inheritance really bad idea?

I'd be happy to hear more opinions here on this one. Thanks guys.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!