N
N
Neabramovich2015-12-05 13:35:35
Yii
Neabramovich, 2015-12-05 13:35:35

Is duplication of fields in the database justified?

Hello!
There is a project on Yii2 that implements a web interface for registered client companies. The database structure is approximately the following (one of the chains):
Company -> Department -> Employee -> Phone
Accordingly, when an employee of the client company logs in to the site, in his personal account he can see a list of all employees, departments and phones belonging to his company.
I implemented this by adding a separate company_id field to all tables (departments, employees, phones) for unambiguous binding to a specific company. Because, for example, when displaying all phones belonging to one company in the GridView, one would have to build a complex query to determine whether this number belongs to this company or not by the chain from the phone to the company.
But there are doubts about the "correctness" of this approach, given that the links between all the elements already exist and some kind of duplication is obtained.
Did I do the right thing? :)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
nelson, 2015-12-05
@Neabramovich

There are two approaches to organizing a database:
1) normalization - removes any duplication, ensures that you do not have inconsistent data if it changes, minimizes the amount of data and simplifies updating it.
2) denormalization - on the contrary, duplicates the data, for the convenience and speed of their reading. Very often used for highload.
Both approaches are good, but each solves its own problems, both have their pros and cons.
You also need to decide what is easier for you to implement:

  • or complex selects and simple update (choose normalized schema)
  • or simple selects and complex updates in several tables at once, support for all these relationships (choose a denormalized schema)

A
Arris, 2015-12-05
@Arris

Compare the benefit of redundancy (speed, in particular) and the overhead of structure complexity (say, you need to update one more field).

R
Rastishka, 2015-12-05
@Rastishka

On MySQL did the same, everything is fine.
But in Postresql, as far as I know, you can give rights to certain rows in tables, most likely it will be more correct.

S
Sergey, 2015-12-26
@sergey_privacy

The main task of any program is to work quickly. If without a duplicated field you have to make more complex queries that take much longer, then there is a point in duplication. I repeatedly initially "licked" the base at the design stage, excluding any duplication. When the load reached certain values, the server stopped coping with some requests and slowed down slightly. Query analysis showed that adding one extra field to one or two tables will reduce queries quite significantly. A voluminous package of operations, which used to take 1.5-3 minutes, began to be processed in 5-10 seconds. Normalization can be good with sufficient memory, processor power, a certain number of users.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question