Skip to content

Commit

Permalink
Merge pull request #279 from green-code-initiative/ISSUE_239
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 authored Mar 17, 2024
2 parents 7998230 + d2a2e36 commit 1064c24
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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 EC24 : Optimize Database SQL Queries (Clause LIMIT)

### Changed

### Deleted
Expand Down
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Some are applicable for different technologies.
| EC12 | Modify several CSS properties all at once | ESLint key : @ecocode/no-multiple-style-changes /// type : Front-end | | 🚫 ||| 🚫 | 🚫 ||
| EC13 | Prefer API collections with pagination | ESLint key : @ecocode/prefer-collections-with-pagination /// type : Back-end | |||||||
| EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 || 🚀 | 🚀 | 🚀 | 🚫 |
| EC24 | Limit the number of returns for a SQL query | ESLint key : @ecocode/limit-db-query-results /// type : Back-end | | 🚧 | 🚀 || 🚀 | 🚀 | 🚀 |
| EC24 | Limit the number of returns for a SQL query | ESLint key : @ecocode/limit-db-query-results /// type : Back-end | | | 🚀 || 🚀 | 🚀 | 🚀 |
| EC25 | Do not use an image with empty source attribute | ESLint key : @ecocode/no-empty-image-src-attribute /// type : Front-end | |||||||
| EC26 | Prefer shorthand CSS notations | ESLint key : @ecocode/prefer-shorthand-css-notations /// type : Front-end | |||||||
| EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
:!sectids:

== Why is this an issue?

SQL queries often involve processing large amounts of data, and fetching a large number of rows can consume significant CPU resources.
By limiting the number of rows returned, you reduce the amount of processing that needs to be done by the database engine, which in turn lowers CPU consumption.

Transmitting a large number of rows over a network can be resource-intensive.
By restricting the result set size, you reduce the amount of data that needs to be transferred between the database and the application, improving network efficiency.

If you store data about customers, you certainly don’t need to retrieve information of all at once, because the larger the table will be, the more elements the query will return.

[source,java,data-diff-id="1",data-diff-type="noncompliant"]
----
String sql = "SELECT user FROM myTable"; // Non-compliant
----

It may therefore be a good idea to limit the results and use pagination, for example.

[source,java,data-diff-id="1",data-diff-type="compliant"]
----
String sql = "SELECT user FROM myTable LIMIT 50"; // Compliant
----

... and create an index like that ...
[source,sql,data-diff-id="1",data-diff-type="compliant"]
----
CREATE INDEX idx_people_lastname_firstname ON people(lastname, firstname)
----

== Resources

=== Documentation

- https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html[MySQL Reference Manual] - LIMIT Query Optimization
- https://www.postgresql.org/docs/current/queries-limit.html[PostgreSQL: Documentation] - LIMIT and OFFSET

=== Articles & blog posts

- https://www.oreilly.com/library/view/high-performance-mysql/9780596101718/ch04.html[Query Performance Optimization]

0 comments on commit 1064c24

Please sign in to comment.