EAV Model vs Rails

Hi,

I have a relational database (MySQL) with 200 tables in it. Now I want to reduce number of tables (If it helps in improving the application).

One approach is to use EAV database model. In which we can use a table to store ID and manadatory details and other table to store rest of the details in pair of attribute, and its value rowwise.

for example look to tables

Client Table ClientID FirstName LastName 1 Rocky B

ClientAttribute Table

ClientID(f.k) Attribute AttributeValue 1 Age 23 1 email sssp@gm.com

mean I am using rows to store attributes instead of columns.

So problem is I have to convert, the 200 table database to EAV. Has anybody worked with EAV model, know its pros cons, and how to do this in rails.

Please help in anyway.

Thanks and regards Sumeet

I have a relational database (MySQL) with 200 tables in it. Now I want

to reduce number of tables (If it helps in improving the

application).

One approach is to use EAV database model. In which we can use a table

to store ID and manadatory details and

other table to store rest of the details in pair of attribute, and its

value rowwise.

If you’re doing that a lot in your database, personally I’d recommend looking at one of the NoSQL types such as CouchDB or Cassandra. Most of my work is neatly normalised tables, but if I had a lot of really freeform data as you seem to, that’s where I’d look.

Cheers,

Andy

If you’re doing that a lot in your database, personally I’d recommend looking at one of the NoSQL types such as CouchDB or Cassandra. Most of my work is neatly normalised tables, but if I had a lot of really freeform data as you seem to, that’s where I’d look.

On the other hand, if you did keep it the way you want to/describe, you could do some really nice stuff with method_missing so you could write code like this:

client.email = “sssp@gm.com

puts client.email

and have it store and retrieve the attribute automatically. That’s a lot better than it would be in other languages.

Cheers,

Andy

The only EAV data model I've used in anger is the back-end of the PHP ecommerce system Magento. It's a very powerful pattern, but has its own limitations (not least that it's a total nightmare to query manually, and tends to be a little slower than row modelling patterns.

But, and I'm happy for the more computer science-y bods to correct me, Rails with ActiveRecord is very tightly bound to the row model of data - in your change, all the magic of associations and relationships won't work. It seems the task that faces you to implement EAV is a massive one... you're pretty much saying "I'm going to write a new Ruby-based web application framework, and use EAV". Of course, you might be able to make a "acts_as_eav" plugin, which could use AR associations of Attributes and Values with (as Andy says) method_missing? calls to act *like* an EAV model - so you can keep the core of AR, but include the flexibility of EAV at will. Maybe this "plugin" approach is likely to be easier with Rails 3 - which isn't necessarily so locked to AR...

Let's go back to the problem (as we are, at heart, looking for ways to implement the solution you've decided on, rather than figuring which solution to use to address a problem) - you have 200 tables (that's a lot!). Do they all map to AR associations? What do they all do? Is there not a easy ORM approach that will suffice rather than EAV?

I'd *love* to see an easy to use EAV option for Rails - but I'd imagine you need to be a "Zed Shaw"/"Hiro Protagonist" to write it...

Sumeet Panchal wrote:

Hi,

I have a relational database (MySQL) with 200 tables in it. Now I want to reduce number of tables (If it helps in improving the application).

Why? There's nothing wrong with having 200 tables if they're necessary to model your data properly.

On the other hand, 200 is a high number, and you could probably stand some normalization or something.

One approach is to use EAV database model.

[...]

And it's a *very* bad approach. Don't do this unless there is no other way to model your data sensibly. If you can say more about your schema, perhaps we can offer better suggestions.

Best,