Best Practices in SQL Coding
Vivek Johari
Technical Lead @ Johnson Controls | Expert in SQL, Database Optimization, Cloud Infrastructure, & Data Solutions | Data Science & ML | Python | Tableau | Power BI | Azure SQL
For any programming language, just writing the code is not well enough. It should be written using the best practices for getting optimise performance . In this article, I will try to explain the disadvantages of writing code without using Best Practices and later how best practices can be implemented in our database code.
Code written without using best practices has the following disadvantages: -
1) Difficult to maintain: - If the code is not written properly, it will be very difficult to modify the code in future and sometimes it become so messy that we required to rewrite the code again even for a small change.
2) Lot of unusable code left which makes the code unnecessary lengthy: – If we do not do the designing part correctly, we keep changing the code again and again which result in lot of reworking. Due to this most of the time it leads to a situation where some stored procedures are left with function or block of query which is not required but it keep executing and decrease their performance.
3) Difficult to understand the code: – One should write a code which can be easily understandable by the other team members. If we do not write the proper comments for each block of code, it becomes difficult to understand the purpose behind the block of code.
4) Poor performance due to improper written complex queries: – In database, there can be multiple ways to write a query which fetch/update/delete records from the tables, but performance depends on how we write the queries. For example, a lengthy complex SQL query containing joins with many tables or SQL query containing distinct command for fetching millions of records where distinct value not required or storing millions of records in a table variable will going to badly affects the performance of the application.
5) Unexpected error or behaviours due to poor error & transaction handling: - If exception handling is not done properly, query execution may break in midway and wrong data is inserted into the tables which will corrupt the database.
6) Locking of tables for long duration due to poor transaction handling: – Transactions should be used so that in case of any exception or error, uncommitted data must be rolled back. But sometimes we use transaction so poorly that it locked the main tables for longer time, and it resulted in the deadlock situation and long duration of locking period result in poor application performance.
7) Difficult to debug: – If the code is written in a very messy way with lot of unusable codes, it becomes difficult to debug the code and find the error.
Best practices for writing SQL code
Easy to understand
Stored procedure and functions
Optimize Joins
SQL Indexes
Avoid Functions on where clause columns
Transaction Handling
领英推荐
Error Handling
Fetching data optimisation
Avoid Nested Loops
Avoid Cursors
Keys and constraints
Temporary table vs Table variable
Appropriate use of datatype
Use of "set not count on"
Monitor and Optimize Regularly
Summary
This article tried to show the disadvantages of not following the best practices and put lights on some best practices which we can use during SQL coding to make our database code more flexible, reusable, easy to understand and more optimised.
For more arcticles on SQL Server, you can visit Technology with Vivek Johari
Would love to hear in the comments ??.
Repost this & follow me Vivek Johari for more tips and articles.
#SQLOptimization #SQLPerformance #DatabaseTuning #QueryOptimization #SQLBestPractices #SQLTuning #DatabasePerformance #SQLTips #TechTips
Software Engineer @Johnson Controls | Knight(2107) @leetcode
3 个月Very informative
Data Architect || Data Governance || Data Science & Cloud Engineering || AI & ML
4 个月Thanks for sharing and very helpful!!!
Senior Manager - Data Platform at Adani Digital Labs
4 个月very informative blog ??