Should you use adjacent tables for one-to-many , one-to-one relationships?

Is there any difference in performance if, for example, for the products table, in which there should be a code field, I will create a separate products_code table and write products_id and code respectively?

  • ps I know why we need different types of communication, so I’m wondering if I will be showered with slippers much if I do as in the example. - dasauser
  • one
    Strong. If you do not explain the meaning of this manipulation - Anton Shchyrov
  • there is a performance difference of course - Yaroslav
  • @Yaroslav, can you provide examples? - dasauser
  • the greater the normalization, the greater the brakes - Yaroslav

2 answers 2

If the relationship is 1: 1, then this technique is called vertical partitioning. It is used to separate the structure of the table vertically for optimization.

Example 1: for example, from the users table, the home address of the user address is rarely read. Then it is possible to divide this table into two, and store the address separately, reading it if necessary. This technique will reduce the size of the users table, and small tables work faster.

Example 2: in the same table there is a last_login field that is updated each time a user logs in. But every time the table is updated, the query cache stops working! By placing this field in a separate table, we will keep users updated to a minimum.

Example 3: the table is huge. ALTER TABLE cannot be done (the operation is very long, simple project). The extra column is rarely used. It makes sense to put it in a separate table. On the other hand, to solve a non-blocking ALTER TABLE problem, there is a pt-online-schema-change

But first, make sure that these separated tables do not have to be merged all the time , because, on the contrary, it can reduce performance.

  • one
    An Exhaustive Answer - Sterx
  • адрес читается редко - well, never read. Who does he interfere with in the table? About frequent updates I want to add more about the peculiarity of the work of locks. When the updated record to commit is blocked for reading. Example 3, IMHO, smacks of a crutch - Anton Shchyrov
  • one
    @AntonShchyrov, the data on the disk is stored in blocks. They are also read in memory in blocks (this is at iron damage). If part of the block is filled with the address, then the full name may require additional reading of one more block, since this field no longer fits on the block boundary. This is extra reading and extra memory. попахивает костылестроением - this is not realistic, I agree. But in reality, ALTER TABLE may take several hours / days, and at this time the table is locked. Do you know how to resolve such situations? If yes, write what you use - Total Pusher

If you want it to be correct and do not shower with slippers, bring everything to the third normal form. The concept is clearly mathematically defined, the algorithm is also mathematically defined. "No need to think." Forgive me for not including here the entire definition of forms and the casting algorithm.

According to your question - it is necessary to distinguish One-to – Zero-or-One from one-to-one. One-to – Zero-or-One: If the product may not have a code, then you need a separate table. one-to-one: If each product necessarily has a code, add the code field to the product table.

  • What is the "third form"? - dasauser
  • form of what? DB structure? tables? - dasauser
  • I strongly advise you to read about the basics of the relational theory of database, since you are engaged in them. Here's a form ru.wikipedia.org/wiki/… - user308670
  • OK. Thanks for the advice, I will take note. - dasauser