Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need some help optimizing my database schema

Here's a layout of my data:

Heading 1:
   Sub heading
   Sub heading
   Sub heading
   Sub heading
   Sub heading

Heading 2:
   Sub heading
   Sub heading
   Sub heading
   Sub heading
   Sub heading

Heading 3:
   Sub heading
   Sub heading
   Sub heading
   Sub heading
   Sub heading

Heading 4:
   Sub heading
   Sub heading
   Sub heading
   Sub heading
   Sub heading

Heading 5:
   Sub heading
   Sub heading
   Sub heading
   Sub heading
   Sub heading

These headings need to have a 'Completion Status' boolean value which gets linked to a user Id.

Currently, this is how my table looks:

id  |  userID  |  field_1  |  field_2  |  field_3  |  field_4  | etc...
-----------------------------------------------------------------------
1   |     1    |    0      |     0     |     1     |     0     |
-----------------------------------------------------------------------
2   |     2    |    1      |     0     |     1     |     1     |

Each field represents one Sub Heading. Having this many columns in my table looks awfully inefficient...

How can I go about optimizing this? I can't think of any way to neaten it up :/

like image 645
dave Avatar asked Jun 07 '26 06:06

dave


1 Answers

Don't use boolean values but simple relations:

table completion_status

id user_id field_id

1  1       3
2  2       1
3  2       3
4  2       4
...

From that it's easy to see that user 1 has completed field 3, and user 2 fields 1, 3 and 4.

This way you don't have to change your database schema whenever you want to change the amount of fields.

like image 188
Tatu Ulmanen Avatar answered Jun 09 '26 00:06

Tatu Ulmanen