0

I used to have an EAV shema with 4 tables in MySQl 5.7:

  1. articles
  2. article attributes
  3. attribute names
  4. attribute group names

After running into huge complexity, I learned from another question that this is not a good shema. So I got rid of table 2 where all the attributes have been stored and saved them either with values or value_ids directly into table one, as the STI model suggests.

Now I ended up with 3 tables:

  1. articles
  2. attribute names
  3. attribute group names

At first it looked like it made my live easier, but while trying to replace a simple query that was getting all attribute group names and attribute names of a specific article I figured that this is also not ideal.

My previous query looked like this:

SELECT 
    cag.name_de, 
    cag.attr_group_id, 
    attr.attr_de, 
    attr.attr_id 
FROM 
    articles_attr aa, 
    cat_attr attr, 
    cat_attr_groups cag 
WHERE 
    aa.article_id = '181206' 
    AND aa.attr_id = attr.attr_id 
    AND cag.attr_group_id = attr.attr_group_id

Now with the new schema, I would need at least this:

Get all group names like e.g. "color"

SELECT 
    name_de,
    attr_group_id 
FROM 
    cat_attr_groups

Get all indirect values which have an ID like e.g. "green"

SELECT 
    attr.attr_group_id, 
    attr.attr_de
FROM 
    articles a, 
    cat_attr attr
WHERE 
    a.article_id = '181206' 
    AND (
            (a.dial_c_id = attr.attr_id)
        OR  (a.dial_n_id = attr.attr_id)
        OR  (a.bracelet_color_id = attr.attr_id)
    )

// pseudo code
$attr[$row->attr_group_id] = $row->attr_de;

Get all direct values:

SELECT 
    jewels,
    vibrations
FROM 
    articles a
WHERE 
    a.article_id = '181206' 


// pseudo code
$attr[4] = $row->jewels;

Map group names with group ids

foreach($attr AS $key => $value){
    // somehow
}

This does not seem to be very elegant. How could I design my shema better or how could those queries be rewritten to retrieve the values in an acceptable query time?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
merlin
  • 2,717
  • 3
  • 29
  • 59
  • It looks like you're trying to implement a [semantic data model](https://en.wikipedia.org/wiki/Semantic_data_model) using a relational database. Inevitably, you have to develop a lot of complex code to do this, and the abstractions leak out into every query you run. You're practicing the [Inner-platform effect](https://en.wikipedia.org/wiki/Inner-platform_effect) — creating a system so customizable as to become a replica, and often a poor replica, of the software development platform they are using. – Bill Karwin Jan 03 '19 at 19:43
  • Interesting, thank you Bill for the input. I am developing a platform from scratch, which should be extendable to fitt upcomming attributes and also usable as a platform for other means. E.g. beeing a platform for cars and one for houses. The attributes either "horse power" or "bed rooms" should be configurable. Also the values. E.g. the colors to pick which one can select if creating a classified to sell a car are always the same.Thats why they have ids and fixed values. Is there a better way of doing this? – merlin Jan 03 '19 at 19:54
  • Read about `GROUP_CONCAT`. – Rick James Jan 03 '19 at 22:44

0 Answers0