Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster: Many rows or many columns?

In MySQL, is it generally faster/more efficient/scalable to return 100 rows with 3 columns, or 1 row with 100 columns?

In other words, when storing many key => value pairs related to a record, is it better to store each key => value pair in a separate row with with the record_id as a key, or to have one row per record_id with a column for each key?

Also, assume also that keys will need to be added/removed fairly regularly, which I assume would affect the long term maintainability of the many column approach once the table gets sufficiently large.

Edit: to clarify, by "a regular basis" I mean the addition or removal of a key once a month or so.

like image 334
Thody Avatar asked Dec 05 '25 17:12

Thody


2 Answers

You should never add or remove columns on a regular basis.

like image 166
Bjarke Freund-Hansen Avatar answered Dec 08 '25 08:12

Bjarke Freund-Hansen


http://en.wikipedia.org/wiki/Entity-Attribute-Value_model

There are a lot of bad things about this model and I would not use it if there was any other alternative. If you don't know the majority (except a few user customizable fields) of data columns you need for your application, then you need to spend more time in design and figure it out.

like image 40
HLGEM Avatar answered Dec 08 '25 09:12

HLGEM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!