I am developing a stock control system for a construction project. The storeman is responsible for adding new stock and distributing/returning it to/from employees. The items (and hence their attributes) will be very varied; e.g. steelwork, clothing, plant/machinery, tools, etc.
My question is whether to go for a Class/Concrete Table Inheritance or EAV based schema. I don't know what attributes items will have, so presumably a Class/Concrete Table Inheritance approach would require the end user to add new tables to the database (is this possible?).
Please see my proposed EAV schema. Would this be a reasonable solution? I think I'm right in saying that to identify an item of stock, it would be necessary to perform multiple self joins in a query on the 'EV' table?
N.B. Developing using PHP, MySQL and Zend Framework.
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.
来源:https://stackoverflow.com/questions/3410362/design-of-an-eav-or-class-concrete-table-inheritance-database-for-stock-cont