-
-
Notifications
You must be signed in to change notification settings - Fork 122
Java - Rules "Optimize Database Queries" #221
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
Closed
tazemar
wants to merge
8
commits into
green-code-initiative:main
from
ThalesGroup:rule/OptimizeDatabaseQueries
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce99127
Init rule
tazemar a6999ad
Tests successfull
tazemar 3198beb
html to md
tazemar f62026a
Merge branch 'green-code-initiative:main' into rule/OptimizeDatabaseQ…
tazemar bf2306f
Adding OptimizeDatabase Queries rule
tazemar c7696cb
remove code smells
tazemar a3cbce5
Update main (#4)
tazemar d50a067
Merge branch 'main' into rule/OptimizeDatabaseQueries
tazemar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
ecocode-rules-specifications/src/main/rules/EC475/EC475.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "title": "Optimize Database Queries", | ||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "tags": [ | ||
| "performance", | ||
| "eco-design", | ||
| "ecocode" | ||
| ], | ||
| "defaultSeverity": "Minor" | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
ecocode-rules-specifications/src/main/rules/EC475/java/EC475.asciidoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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: | ||
|
|
||
| - 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. | ||
|
|
||
| - Only use necessary fields in the tables or documents in order to avoid needlessly transferring data that will not be used and to avoid using database server and application server resources to manipulate them. | ||
|
|
||
| - Add indexes on fields used as keys in your model. Adding them can completely change queries performance. Be careful: adding indexes makes writing longer as it must be updated for each added, modified or deleted document. This must be done if there are more reads than writes or if reads are particularly expensive. | ||
|
|
||
| - Use database management system tools to analyze queries in order to identify improvement areas, such as EXPLAIN for RDBMS. | ||
|
|
||
| - Cache the most expensive queries results as well as data that changes little or never (reference data). | ||
|
|
||
| - Optionally, modifying data models to be able to access information more easily without joins (denormalization) | ||
|
|
||
|
|
||
| ## Noncompliant Code Example | ||
|
|
||
| ```java | ||
| String sql = "SELECT user FROM myTable"; | ||
| ``` | ||
|
|
||
| ## Compliant Code Example | ||
|
|
||
| ```java | ||
| String sql = "SELECT user FROM myTable LIMIT 50"; | ||
| ``` | ||
|
|
||
| ### Index creation example | ||
|
|
||
| ```sql | ||
| CREATE INDEX idx_people_lastname_firstname ON people(lastname, firstname) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeDatabaseQueries.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package fr.greencodeinitiative.java.checks; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.LiteralTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.Tree.Kind; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Predicate; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
| import static java.util.regex.Pattern.CASE_INSENSITIVE; | ||
| import static java.util.regex.Pattern.compile; | ||
|
|
||
| @Rule(key = "EC475") | ||
| public class OptimizeDatabaseQueries extends IssuableSubscriptionVisitor{ | ||
| public static final String MESSAGE_RULE = "Optimize Database Queries (Clause LIMIT)"; | ||
| private static final Predicate<String> LIMIT_REGEXP = | ||
| compile("limit", CASE_INSENSITIVE).asPredicate(); | ||
| private static final Predicate<String> SELECT_REGEXP = | ||
| compile("select", CASE_INSENSITIVE).asPredicate(); | ||
| private static final Predicate<String> FROM_REGEXP = | ||
| compile("from", CASE_INSENSITIVE).asPredicate(); | ||
|
|
||
| @Override | ||
| public List<Kind> nodesToVisit() { | ||
| return singletonList(Kind.STRING_LITERAL); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| String value = ((LiteralTree) tree).value(); | ||
| if (SELECT_REGEXP.test(value) && FROM_REGEXP.test(value) && !LIMIT_REGEXP.test(value)) { | ||
| reportIssue(tree, MESSAGE_RULE); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class OptimizeDatabaseQueries { | ||
|
|
||
| OptimizeDatabaseQueries(OptimizeDatabaseQueries mc) { | ||
| } | ||
|
|
||
| public void literalSQLrequest() { | ||
| dummyCall("SELECT user FROM myTable"); // Noncompliant | ||
| dummyCall("SELECT user FROM myTable LIMIT 50"); // Compliant | ||
| } | ||
|
|
||
| @Query("select t from Todo t where t.status != 'COMPLETED'") // Noncompliant | ||
| @Query("select t from Todo t where t.status != 'COMPLETED' LIMIT 25") // Compliant | ||
|
|
||
| private void callQuery() { | ||
| String sql1 = "SELECT user FROM myTable"; // Noncompliant | ||
| String sql2 = "SELECT user FROM myTable LIMIT 50"; // Compliant | ||
| } | ||
|
|
||
| private void dummyCall(String request) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...-plugin/src/test/java/fr/greencodeinitiative/java/checks/OptimizeDatabaseQueriesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package fr.greencodeinitiative.java.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| class OptimizeDatabaseQueriesTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile("src/test/files/OptimizeDatabaseQueries.java") | ||
| .withCheck(new OptimizeDatabaseQueries()) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, for me title is quite wrong, like class names above, because you deal with database queries only for relational databases (and not NOSql databases for example). Thus, could you add "SQL" word in title, class names, descriptions, documentation, etc ... please
also, there are some lacks if you check DoD list https://github.com/green-code-initiative/ecoCode-common/blob/main/doc/starter-pack.md#definition-of-done-of-a-pr - example :