green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
4 stars 5 forks source link

[JAVA] [Team 2 millions] Persistence Java : Avoid Joints on non indexed columns #66

Open arhnabri opened 1 year ago

arhnabri commented 1 year ago

Index filtered database columns

Platform

OS OS version Langage
- - Java

Main caracteristics

ID Title Category Sub-category
IDC001 All entities mapping should be indexed ORM Index columns

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

All filtered columns should be indexed

Rule complete description

Text

Add @Index on @ManyToOne, @OneToMany, @ManyToMany or @OneToOne. Primary keys (@Id aren't concerned)

HTML

<p>Spring Data, avoid usage of joints on non primary keys or non Indexed columns. <br> The purpose is to avoid consuming unnecessary resource to collect data</p>
<h2>Noncompliant Code Example</h2>
<pre>
  @Entity
    class MyEntity {

 

        @ManyToOne
        private MyEntity subEntities; // Noncompliant {{Add @Index on foreign key}}

 

        @OneToMany
        private List<String> subEntity4; // Noncompliant {{Add @Index on foreign key}}
  }
</pre>
<h2>Compliant Solution</h2>
<pre>

 

  @Entity
    class MyEntity {

 

        @ManyToOne
        @Index(name = "indexSubEntity2", columnNames = "subEntity2Id")
        private MyEntity subEntities2;

 

        @OneToMany
        @Index(name = "indexSubEntity3", columnNames = "subEntity3Id")
        private List<String> subEntity3;
    }
</pre>

Implementation principle

arhnabri commented 1 year ago

Built by team 2 millions.

jhertout commented 7 months ago

Interesting idea but missing documentation or measure to justify it. May be we should try to measure it in the next hackaton.