green-code-initiative / ecoCode

Reduce the environmental footprint of your software programs with SonarQube
https://ecocode.io
GNU General Public License v3.0
134 stars 75 forks source link

New Rule suggestion : Optimize Database Queries #239

Open tazemar opened 10 months ago

tazemar commented 10 months ago

New Rule : Optimize Database Queries

https://github.com/cnumr/best-practices/blob/main/chapters/BP_075_en.md

PR : https://github.com/green-code-initiative/ecoCode/pull/221

Use LIMIT Clause on SQL Queries. Other techniques are possible but we cannot verify with static analysis.

Rule complete description

Text

Databases are typically essential application components. As many queries are used to retrieve and store data, they end up having a significant impact on the solution's resource use when executed frequently.

With this in mind, it is important to pay attention for these queries and ensure, at least for the most expensive ones that they are well optimized.

The most common optimization tips are:

Optimized SQL: Optimize Database Queries

Platform

OS OS version Langage
- - Java, Javascript, Php, Python

Main caracteristics

ID Title Category Sub-category
X Optimize Database Queries Environment Optimized SQL

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Limit your Database Queries. You should use the "LIMIT" SQL element.

Rule complete description

Text

Use LIMIT on SQL request to control the size of your exchange.

HTML

<p></p>
<h2>Noncompliant Code Example</h2>
<pre>
 String sql = "SELECT user FROM myTable";
</pre>
<h2>Compliant Solution</h2>
<pre>
String sql = "SELECT user FROM myTable LIMIT 50";
</pre>
dedece35 commented 9 months ago

Hi @tazemar, thank you for issue. It's a good idea. Please take into account my review feedback on your PR.