I need help optimizing my database schema

Here is the location 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 headers should have the logical value "Completion Status", which is associated with the user ID.

Currently my table looks like this:

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-head. Having this many columns in my table looks terribly inefficient ...

How can I optimize this? I can’t figure out how to do this: /

+3
source share
3 answers

Do not use booleans, but simple relationships:

table completion_status

id user_id field_id

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

From this it is easy to see that user 1 completed field 3, and user 2 completed fields 1, 3, and 4.

, , .

+3

, .

id  |  userID  |  field_id | 
-----------------------------
1   |     1    |    0      | 
-----------------------------
2   |     2    |    1      | 

field

id  | field_1 | field_2   | etc..
-----------------------------
1   |     1   |    0      | etc..
0

Why aren't you making such a table?

id  | User Id | Field ID | Status
---------------------------------
1   |   1     |   1      | 0   
2   |   1     |   2      | 1  

You may have a different main table for fields with field names

0
source

Source: https://habr.com/ru/post/1748284/


All Articles