HiveBrain v1.2.0
Get Started
← Back to all entries
principleMinor

Array as a column vs lookup table

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
arraylookuptablecolumn

Problem

Hi I have two tables that has one to many relationship.

Table 1 Users:                     

 -------------
| id  |  name |
 -------------
| 01  |  John |
 -------------
| 02  | Harry |
 -------------

  Table 2 Cars:                     

 -------------
| id  | Brand |
 -------------
| 01  |  GM   |
 -------------
| 02  | Honda |
 -------------


Now any user might have multiple cars. Which one would be faster and performance efficient?

Having a array columns named carOwned in User column:

Table 1 Users:                     

 ------------------------
| id  |  name | carOwned |
 ------------------------
| 01  |  John | [01, 02 ]| 
 ------------------------
| 02  | Harry | [02]     |
 ------------------------


Or a lookup table like?

Table 3 CarOwnership:                     

 -----------------
| userId  | carId |
 -----------------
| 01      |  01   |
 -----------------
| 01      |  02   |
 -----------------
| 02      |  02   |
 -----------------

Solution

You will want to use the Lookup Table approach. If absolutely necessary there are tools in most database implementations you can use to turn rows into an array like result for another application to use. But as far as Databases are concerned stick to the row method to maintain a nice clean relational model you can JOIN easily on.

Context

StackExchange Database Administrators Q#130350, answer score: 4

Revisions (0)

No revisions yet.