Finding all Stored Procedures that calls a Function in SQL Server | SQL Server Tutorial

The below Stored Procedure searches within all the stored procedures in the SQL server database, views, and functions for the given function name. This stored procedure also search......

Read More

Finding Stored Procedures that Accesses Particular Table or Column in SQL Server | SQL Server Tutorial

By querying the Procedures table in Sys as given below, we can find the SQL Server stored procedures that refers particular column are tables. SELECT Name FROM sys.procedures WHERE......

Read More

Writing Effective SQL Queries | SQL Query 10 Best Practices Tips | SQL Server Programmer Guide

1. Eliminate Cursors from the Query

Try to remove cursors from the query and use set-based query; set-based query is more efficient than cursor-based. If there is a need to use cursors, then avoid dynamic cursors as they tend to limit the choice of plans available to the query optimizer. For example, dynamic cursor limits the optimizer to using nested loopjoins.

2. Avoid Use of Non-correlated Scalar Sub Query

You can re-write your query to remove non-correlated scalar sub query as a separate query instead of part of the main query and store the output in a variable, which can be referred to in the main query or later part of the batch. This will give better options to Optimizer, which may help to return accurate cardinality estimates along with a better plan.

3. Avoid Multi-statement Table Valued Functions (TVFs)

Multi-statement TVFs are more costly than inline TFVs. SQL Server expands inline TFVs into the main query like it expands views but evaluates multi-statement TVFs in a separate context from the main query and materializes the results of multi-statement into temporary work tables. The separate context and work table make multi-statement TVFs costly.

4. Creation and Use of Indexes

We are aware of the fact that Index can magically reduce the data retrieval time but have a reverse effect on DML operations, which may degrade query performance. With this fact, Indexing is a challenging task, but could help to improve SQL query performance and give you best query response time.

5. Understand the Data

Understand the data, its type and how queries are being performed to retrieve the data before making any decision to create an index. If you understand the behavior of data thoroughly, it will help you to decide which column should have either a clustered index or non-clustered index. If a clustered index is not on a unique column then SQL Server will maintain uniqueness by adding a unique identifier to every duplicate key, which leads to overhead. To avoid this type of overhead, choose the column correctly or make the appropriate changes.

6. Create a Highly Selective Index

Selectivity define the percentage of qualifying rows in the table (qualifying number of rows/total number of rows). If the ratio of the qualifying number of rows to the total number of rows is low, the index is highly selective and is most useful. A non-clustered index is most useful if the ratio is around 5% or less, which means if the index can eliminate 95% of the rows from consideration. If index is returning more than 5% of the rows in a table, it probably will not be used; either a different index will be chosen or created or the table will be scanned.

7. Position a Column in an Index

Order or position of a column in an index also plays a vital role to improve SQL query performance. An index can help to improve the SQL query performance if the criteria of the query matches the columns that are left most in the index key. As a best practice, most selective columns should be placed leftmost in the key of a non-clustered index.

8. Drop Unused Indexes

Dropping unused indexes can help to speed up data modifications without affecting data retrieval. Also, you need to define a strategy for batch processes that run infrequently and use certain indexes. In such cases, creating indexes in advance of batch processes and then dropping them when the batch processes are done helps to reduce the overhead on the database.

9. Statistic Creation and Updates

You need to take care of statistic creation and regular updates for computed columns and multi-columns referred in the query; the query optimizer uses information about the distribution of values in one or more columns of a table statistics to estimate the cardinality, or number of rows, in the query result. These cardinality estimates enable the query optimizer to create a high-quality query plan.

10. Revisit Your Schema Definitions

Last but not least, revisit your schema definitions; keep an eye out that appropriate FOREIGN KEYNOT NULL and CHECK constraints are in place or not. Availability of the right constraint at the right place always helps to improve the query performance, like FOREIGN KEYconstraint helps to simplify joins by converting some outer or semi-joins to inner joins and CHECK constraint also helps a bit by removing unnecessary or redundant predicates.

What is UNIQUE KEY constraint? | SQL Server Interview Question

,

A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity......

Read More

What is PRIMARY KEY? | SQL Server Interview Question

,

A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary......

Read More

What is SQL Server Agent? | SQL Server Interview Question

,

SQL Server agent plays an important role in the day-to-day tasks of a database administrator (DBA). It is often overlooked as one of the main tools for SQL Server management. Its......

Read More

What are the Types of Authentication Modes in SQL Server? How can it be changed? | SQL Server Interview Question

,

Windows mode and Mixed Mode – SQL & Windows. To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server and click SQL Enterprise Manager to run......

Read More

What is SQL Profiler? | SQL Server Interview Question

,

SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server. You can capture and save data about each event to a......

Read More

What Are The Different Types of Sub-Queries? | SQL Server Interview Question

,

Single-row sub-query, where the sub-query returns only one row. Multiple-row sub-query, where the sub-query returns multiple rows,. and Multiple column sub-query, where the sub-query......

Read More

Explain the Properties of Sub-Queries? | SQL Server Interview Question

,

A sub-query must be enclosed in the parenthesis. A sub-query must be put in the right hand of the comparison operator, and A sub-query cannot contain an ORDER-BY clause. A query......

Read More

Difference Between HAVING CLAUSE and WHERE CLAUSE? | SQL Server Interview Question

,

They specify a search condition for a group or an aggregate. But the difference is that HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY......

Read More

Usage of UPDATE_STATISTICS command? | SQL Server Interview Question

,

This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to......

Read More

Difference Between DELETE & TRUNCATE Commands? | SQL Server Interview Question

,

Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be......

Read More

Difference Between Primary Key and Unique Key? | SQL Server Interview Question

,

Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique......

Read More

What is OLTP (Online Transaction Processing)? | SQL Server Interview Question

,

In OLTP – online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order......

Read More

What are different types of Collation Sensitivity? | SQL Server Interview Question

,

Case sensitivity – A and a, B and b, etc. Accent sensitivity – a and á, o and ó, etc. Kana Sensitivity – When Japanese kana characters Hiragana and Katakana are treated......

Read More

Index Type Configurable to a Table? | SQL Server Interview Question

,

A table can have one of the following index configurations: No indexes A clustered index A clustered index and many nonclustered indexes A nonclustered index Many nonclustered......

Read More

Difference Between Clustered and Non-Clustered Index? | SQL Server Interview Question

,

A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes......

Read More

What are the properties of the Relational tables? | SQL Server Interview Question

,

Relational tables have six properties: Values are atomic. Column values are of the same kind. Each row is unique. The sequence of columns is insignificant. The sequence of rows......

Read More

What is RDBMS? | SQL Server Interview Question

,

Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across......

Read More