c# – What precautions should I take when creating tables with 1:1 relationship?

Question:

Basically I'm in doubt about when I should use 1:1 relationship or a big table.

In my case I would have the object

public class Person(){
  public Guid Id {get;set;}
  public string Name {get;set;}
  public string Cnpj {get;set;}
  public PersonAdress Adress {get;set;}
}

and then I would have the object

public class PersonAdress (){
  public Guid PersonAdressId {get;set;}
  public stirng Enderedo {get;set;}

  public virtual Person Person {get;set;}
}

Is this really good practice?

Or would it be better if I have a single entity

public class Person(){
  public Guid Id {get;set;}
  public string Name {get;set;}
  public string Cnpj {get;set;}
  public string Endereço {get;set;}
}

Of course I made a shortened version of more objects, they would have more fields and more 1:1 relationships.

I did some performance tests on SQLServer and there was a very small difference, in a universe of 20,000 records.

However, as I am aware of the DBA , I am worried.

Answer:

Trying to model smaller tables is undoubtedly a good practice, for that there are database normalization levels to guide you in this process. Your test does not have such noticeable results because it is a small example (20 thousand records for a SQL Server is practically nothing), the problem will scale along with the size of your project, both in the database and in your backend due to ORM

In the context of the database, normalizing serves to reduce redundancy and duplication of information within the database. I see a lot of people saying that dividing too much causes the need to do a lot of JOINS, but it is worth remembering that the join is one of the basic concepts of relational banking, being a very refined and performative operation.

In the class context, a large object typically stays much longer in memory and survives Garbage Collector inspections, becomes a gen2 object. The problem with this type of object is that it is necessary to do a FullGC to clean it, which is quite costly for performance.

Scroll to Top
AllEscort