|
| 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