Real time infrastructure monitoring, smart tracking and inventory reporting solution.

SQL Server Performance Tuning: Best Practices

No comments

Getting tired of slow queries? Wondering how you can improve SQL performance? The answer lies in SQL performance tuning or SQL server audit. Developers and database administrators (DBA) can significantly improve SQL server performance by fine-tuning how it carries out the queries. Optimal performance is crucial for relational databases and the applications that use them.

5 Best Practices for SQL Performance Tuning

Optimizing SQL server performance is not exactly an easy task, especially when dealing with large databases. However, with the insights shared in this post, even a developer with not so extensive knowledge of SQL can optimize server performance. Here are five widely used industry practices for optimizing SQL server and improving slow queries:

Indexing

Indexing is a standard industry practice aimed at improving data retrieval. Database designers typically index tables in the development phase. With indexing, selecting and sorting records is much faster than without an index. Typically, you don’t necessarily have to index all the tables in your database. 

Focus mostly on the tables frequently used for searches and ordering. The index can also serve as a primary key that ensures each record has a unique identifier. Indexing is not a great solution for tables that see many UPDATE, INSERT, and DELETE queries, as these queries modify the index each time, resulting in slower performance. 

Execution Plans

You can use execution plans in SQL Server Management Studio to see a graphical representation of the data retrieval. It will show you which methods were chosen by the SQL query optimizer. If a particular query takes too long, generate the execution plan for that. 

It’s very simple to get the execution plan. Before running the query, click on Query from the main menu, then click on ‘Include Actual Execution Plan.’ Alternatively, you can just press CTRL + M before running the query. You’ll see a new pane at the bottom with the results pane called Execution Plan

Here you can view a horizontal flowchart of the path the query took to find its result. An execution plan can help identify problems and errors. For instance, it’s also helpful with indexing problems. If an index is missing, it will identify where; you can find the missing index details by right-clicking. 

Query Optimization

A lot of the times, it’s simply the queries that are causing the performance slowdown. The queries are not wrong per se, but they use up extra resources or work in a slow way. This is something that both DBAs and developers should know and practice because, well, that’s what makes them professionals. 

The most common mistake people make when retrieving data is using ‘SELECT *’ instead of naming the columns they want to search. Sure, it works fine in retrieving the records from a table, but if the table has hundreds of columns, the retrieval can slow down. 

If you need a handful of columns, simply name them instead of using the asterisk to select all of them. Even if you need all the columns, try using the actual column names, especially when linking with another query. The database can always be updated with another column, just to ensure you name the column needed. 

Similarly, avoid counters in your code that run hundreds or thousands of queries on loop. This can slow down the server immensely. Instead of that, design queries that run once but add those hundred or thousands of values. So technically, only one query is running, even though it’s doing all the work. 

Monitoring Tools

You can use a monitoring tool to optimize the SQL server and find out how to improve your queries. Such tools, often termed Application Performance Management (APM), are great for getting to the root of the problem if any.  Even if SQL server isn’t particularly slow, you can tune its performance and make it better. 

You will need to get a reliable third-party monitoring solution to analyze your SQL server. VirtualMetric has an SQL Server Performance Monitoring tool that monitors databases, transactions, stored procedures, etc. It basically provides all the features needed to monitor, manage, and optimize SQL databases. 

VirtualMetric Dashboard

If you’re working with databases on a virtual machine and running those databases slows it down, perhaps you can use monitoring tools for the hypervisor itself. VirtualMetric also has monitoring solutions for VMware and Hyper-V. These monitoring solutions not only detect the issues with the databases but also the whole virtual machine, optimizing overall performance

You have to keep one thing in mind, the SQL server is extremely resource-intensive, so it’s likely going to eat up a lot of resources of the VM, causing lags and generally slow performance. So these practices are all the more relevant for such servers. 

Database Engine Tuning Advisor

The Database Engine Tuning Advisor tool within the SQL server environment is another handy tool to improve SQL performance. The tool not only helps tune the database in question but also recommends actions to improve performance further. 

This tool may be particularly useful for developers, especially newbies who have only really dipped their toes yet. It is easy to understand, so easy to start with. The gist of this tool is analyzing Transact-SQL statements, which may reveal a lot of useful and important information. 

You can access it from the Tools menu in SQL Server Profile or SQL Server Management Studio (don’t run this tool when in single-user mode). Actually, there’s a lot you can do with this tool, but it has a learning curve. This is why a lot of developers rather just use external monitoring tools. 

Conclusion

SQL performance tuning is unfortunately seen as a secondary task, especially by developers who tend to think it’s not their job. A fast, secure, and reliable database server benefits everyone, including developers who perhaps use the database on a daily basis for developing and testing. 

The above tips for optimization use a mix of tools and general practices that will go a long way in improving SQL server performance for virtually all the databases. More organizations should invest time and resources standardizing these practices. Follow the tips, and we are sure you can upgrade the performance.

Olga BurnaevaSQL Server Performance Tuning: Best Practices

Leave a Reply

Your email address will not be published. Required fields are marked *