Skip to content

Commit 8409185

Browse files
authored
Create sql-performance.md
Create sql-performance.md
1 parent 3b20c3e commit 8409185

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

โ€Ždocs/sql-performance.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
๐—ง๐—ผ๐—ฝ ๐Ÿฎ๐Ÿฌ ๐—ฆ๐—ค๐—Ÿ ๐—พ๐˜‚๐—ฒ๐—ฟ๐˜† ๐—ผ๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐˜๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ๐˜€
2+
3+
Here is the list of the top 20 SQL query optimization techniques I found noteworthy:
4+
5+
1. Create an index on huge tables (>1.000.000) rows
6+
2. Use EXIST() instead of COUNT() to find an element in the table
7+
3. SELECT fields instead of using SELECT *
8+
4. Avoid Subqueries in WHERE Clause
9+
5. Avoid SELECT DISTINCT where possible
10+
6. Use WHERE Clause instead of HAVING
11+
7. Create joins with INNER JOIN (not WHERE)
12+
8. Use LIMIT to sample query results
13+
9. Use UNION ALL instead of UNION wherever possible
14+
10. Use UNION where instead of WHERE ... or ... query.
15+
11. Run your query during off-peak hours
16+
12. Avoid using OR in join queries
17+
14. Choose GROUP BY over window functions
18+
15. Use derived and temporary tables
19+
16. Drop the index before loading bulk data
20+
16. Use materialized views instead of views
21+
17. Avoid != or <> (not equal) operator
22+
18. Minimize the number of subqueries
23+
19. Use INNER join as little as possible when you can get the same output using LEFT/RIGHT join.
24+
20. Frequently try to use temporary sources to retrieve the same dataset.
25+
26+
Do you know what is ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜† ๐—ข๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ฒ๐—ฟ? Its primary function is to determine ๐˜๐—ต๐—ฒ ๐—บ๐—ผ๐˜€๐˜ ๐—ฒ๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฒ๐—ป๐˜ ๐˜„๐—ฎ๐˜† to execute a given SQL query by finding the best execution plan. The query optimizer takes the SQL query as input and analyzes it to determine how best to execute it. The first step is to parse the SQL query and create a syntax tree. The optimizer then analyzes the syntax tree to determine how to run the query.
27+
28+
Next, the optimizer generates ๐—ฎ๐—น๐˜๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฒ๐˜…๐—ฒ๐—ฐ๐˜‚๐˜๐—ถ๐—ผ๐—ป ๐—ฝ๐—น๐—ฎ๐—ป๐˜€, which are different ways of executing the same query. Each execution plan specifies the order in which the tables should be accessed, the join methods, and any filtering or sorting operations. The optimizer then assigns a ๐—ฐ๐—ผ๐˜€๐˜ to each execution plan based on the number of disk reads and the CPU time required to execute the query.
29+
30+
Finally, the optimizer ๐—ฐ๐—ต๐—ผ๐—ผ๐˜€๐—ฒ๐˜€ ๐˜๐—ต๐—ฒ ๐—ฒ๐˜…๐—ฒ๐—ฐ๐˜‚๐˜๐—ถ๐—ผ๐—ป ๐—ฝ๐—น๐—ฎ๐—ป with the lowest cost as the optimal execution plan for the query. This plan is then used to execute the query.
31+
32+
Check in the image the ๐—ผ๐—ฟ๐—ฑ๐—ฒ๐—ฟ ๐—ถ๐—ป ๐˜„๐—ต๐—ถ๐—ฐ๐—ต ๐—ฆ๐—ค๐—Ÿ ๐—พ๐˜‚๐—ฒ๐—ฟ๐—ถ๐—ฒ๐˜€ ๐—ฟ๐˜‚๐—ป.

0 commit comments

Comments
ย (0)