Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java rule EC24: Optimize Database SQL Queries (Clause LIMIT) #279

Merged
merged 7 commits into from
Mar 17, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#239](https://github.com/green-code-initiative/ecoCode/issues/239) Add new Java rule EC80 : Optimize Database SQL Queries (Clause LIMIT)

### Changed

### Deleted
Expand Down
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Some are applicable for different technologies.
| | Avoid high accuracy geolocation | Avoid using high accuracy geolocation in web applications (linter key : `@ecocode/avoid-high-accuracy-geolocation`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 |
| | No import all from library | Should not import all from library (linter key : `@ecocode/no-import-all-from-library`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 |
| | Prefer collections with pagination | Prefer API collections with pagination (linter key : `@ecocode/prefer-collections-with-pagination`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 |
| EC80 | Optimize Database SQL Queries (Clause LIMIT) | Use less data and limit it to the bare minimum on SQL Queries (When possible, using the 'LIMIT' clause reduces the amount of transferred data) | [cnumr best practices (3rd edition) BP_075](https://github.com/cnumr/best-practices/blob/main/chapters/BP_075_en.md) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 |

## Rules to be reworked / measured / clarified

Expand Down
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC80/EC80.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"title": "Optimize Database Queries",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "15min"
},
"tags": [
"performance",
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Use less data and limit it to the bare minimum.
For example, the LIMIT clause limits the number of result rows in relational databases.
When possible, using the 'LIMIT' clause reduces the amount of transferred data.
Performance gains will be even more significant if records contain a large number of voluminous fields.

## Noncompliant Code Example

```java
String sql = "SELECT user FROM myTable";
```

## Compliant Solution

### Add `LIMIT` clause
```java
String sql = "SELECT user FROM myTable LIMIT 50";
```

### Create Index

```sql
CREATE INDEX idx_people_lastname_firstname ON people(lastname, firstname)
```
Loading