Showing posts with the label Sql TutorialShow All
What are Allocation Units in SQL Server?
Showing posts with label Sql Tutorial. Show all posts
Showing posts with label Sql Tutorial. Show all posts

Topics Sequence for Sql Tutorial

Database

DBMS and RDMBS

SQL Server

SQL Server Architecture

SSMS

Authentications Modes in SQL Server

System Databases in SQL Server

SINGLE_USER,READ_ONLY and MULTI_USER Modes in Sql Server Databases

Creating , Altering and Dropping Databases

MDF and LDF Files

Pages in Sql Server (Data Pages and Index Pages)

SQL Commands

Creating, Altering and Dropping Tables

Constarints in SQL Server

Primary Key and Foriegn Key Constraints

Cascading Referential Integrity

Not Null Constraint 

Default Constraint and It's Behaviour

Check Constraint

Unique Key Constraint

SQL Server DataTypes

SQL Server Variable

Identity Column

Retrieving Identity Column

SELECT

Group By

Where vs Having

Order of Statement Execution

Different Ways to Replace Null Values: ISNULL(), COALESCE(), CASE

Joins

Union , Union All, Except and Intersect

Store Procedures and It's Advantages

Output Parameter vs Return Values

String Functions

DateTime Functions

Cast vs Convert

Mathematical Functions

User Defined Functions: Scalar, Inline and MultiStatement UDF

Difference Between Stored Procedure and Functions

Temporary Tables: Local vs Global

Indexes

Views

DML Triggers

Derived Tables and CTE

Database Normalization

Pivot Operator

Error Handling

Transactions and ACID Properties, Nested Transactions

SubQueries and Types of SubQueries

Cursors

Merge Operator

Concurrency and Isolation Level

DeadLocks in SQL Server

Cross Apply and Outer Apply

DDL Triggers

Table Valued Parameters

GROUPING SETS, ROLLUP, CUBE

GROUPING() and GROUPING_ID()

OVER and PARTITION BY 

ROW_NUMBER(), RANK(), DENSE_RANK()

Window Functions

Rows vs Range

UnPivot

OFFSET FETCH

Identify Object Dependiencies

SEQUENCE

GUID

Dynamic SQL

QUOTENAME() and PARSENAME()

Output Parameter in Dynamic SQL

Temp tables in dynamic sql



What is Temporal tables? explain its purpose? in which case we can use this?

What is mean by Statistics and When to Update statistics?

 Statistics determine the selectivity of the indexes. If an indexed column has unique values then the selectivity of that index is more, as opposed to an index with non-unique values. The query optimizer uses these indexes in determining whether to choose an index or not while executing a query.

Some situations under which you should update statistics:

  1. If there is a significant change in the key values in the index
  2. If a large amount of data in an indexed column has been added, changed, or removed (that is if the distribution of key values has changed), or the table has been truncated using the TRUNCATE TABLE statement and then repopulated
  3. The database is upgraded from a previous version

Look up SQL Server books online for the following commands:

UPDATE STATISTICS,
STATS_DATE,
DBCC SHOW_STATISTICS,
CREATE STATISTICS,
DROP STATISTICS,
sp_autostats,
sp_createstats,
sp_updatestats

What is difference between Table Scan, Index Scan and Index Seek.

Explain Execution Plan in details?

 An execution plan is a graphical or textual way of showing how the SQL server breaks down a query to get the required result. It helps a user to determine why queries are taking more time to execute and based on the investigation user can update their queries for the maximum result.

Query Analyzer has an option, called “Show Execution Plan” (located on the Query drop-down menu). If this option is turned on, it will display a query execution plan in a separate window when the query is run again.


Q1: What is a SQL Server Execution Plan?

SQL Server Execution Plan is a binary representation of the steps that are followed by the SQL Server Engine to execute the query. It also is known as the most efficient roadmap for the query

For more information, check SQL Server Execution Plans overview

Q2: Which component of the SQL Server Engine is responsible for generating an Execution Plan for the submitted query?

A: The SQL Server Query Optimizer is responsible for creating the most efficient plan to execute the provided query

For more information, check SQL Server Execution Plans overview

Q3: Where will the generated Execution Plan be stored?

A: The SQL Server Execution Plan will be stored in the Plan Cache memory storage

For more information, check SQL Server Execution Plans overview

Q4: What is the goal behind storing the Execution Plan for the query in the Plan Cache?

A: The process of generating the most optimal execution plan is an expensive process. Instead of creating a new Execution Plan each time a new query is submitted, the SQL Server Query Optimizer will search in the plan cache storage for an existing Execution Plan for the submitted query and use it. If there is no plan that can be used for that query, the Query Optimizer will create a new plan, taking more time to execute that query. The Execution plans reuse mechanism is very helpful when there are stored procedures executed frequently

For more information, check SQL Server Execution Plans overview

Q5: What are the main types of Execution Plans that you can generate for a T-SQL query and what is the difference between these two types?

A: The Estimated Execution Plan. It is the plan that is generated by parsing the submitted query as an estimate of how the query will be executed, without being executed

The Actual Execution Plan, that is generated by executing the submitted query, displaying the actual steps that followed while executing the query

For more information, check SQL Server Execution Plans types

Q6: What are the three Execution Plan formats?

A: Graphical FormatText Format and XML Format

Q7: How could the Execution Plan help in identifying the missing index for a specific query?

A: Based on the available SQL Server statistics and the workload performed on the SQL Server, the SQL Server Query Optimizer will provide us with a suggested index, that may improve the performance of the submitted query in a calculated percentage. So, it will display that index as a recommendation with the query plan in green

For more information, check How to Analyze SQL Execution Plan Graphical Components

Q8: What is the correct way of reading the Execution Plan?

A: The correct way to read the SQL Execution Plan is to start from the right side of the plan to the left side and from the top to the bottom, and the most left SELECT operator contains the final result of the submitted query

For more information, check How to Analyze SQL Execution Plan Graphical Components

Q9: How could we use the arrow between the Execution Plan operators to read the plan?

A: You can make use of the arrows that are connecting the operators in identifying the direction and the amount of the data passed between the Execution Plan operators. In addition, the arrow is an indication of how much data passed between the operators

For more information, check How to Analyze SQL Execution Plan Graphical Components

Q10: Can the Estimated SQL Execution Plan result be trusted?

A: This depends on the statistics. If it is updated, the results should be the same. You need the Estimated SQL Execution Plan in case the query will take a long time to execute and you need to troubleshoot it

For more information, check How to Analyze SQL Execution Plan Graphical Components

Q11: What is the difference between the RID and the Key Lookup operators?

A: RID is a row locator that includes information about the location of that record such as the database file, the page, the slot numbers that helps to identify the location of the row quickly

The Key Lookup operator is the Clustered equivalent of the RID Lookup operator

For more information, check SQL Server Execution Plan Operators – Part 2

Q12: What is the Aggregate operator in the Execution Plan?

A: The Aggregate Operator is mainly used to calculate the aggregate expressions in the submitted query, by grouping the values of an aggregated column. The aggregate expressions include the MIN, MAX, COUNT, AVG, SUM operations

For more information, check SQL Server Execution Plan Operators – Part 3

Q13: What is the Compute Scalar operator in the Execution Plan?

A: The Compute Scalar operator is used to perform scalar computation operations in order to calculate a new value from the existing row value

For more information, check SQL Server Execution Plan Operators – Part 3

Q14: What is the Concatenation operator in the Execution Plan?

A: The Concatenation operator takes one or more data sets in sequence as inputs and returns all records from all the input data set. A good example of the concatenation operator is the UNION ALL T-SQL statement

For more information, check SQL Server Execution Plan Operators – Part 3

Q15: What is the ASSERT operator in the Execution Plan?

A: The Assert operator will verify whether the inserted values meet the defined CHECK or FOREIGN KEY constraints on the table called by the query

For more information, check SQL Server Execution Plan Operators – Part 3

Q16: What is the Hash Match operator in the Execution Plan?

A: Hashing table is used when the SQL Server engine divides the joined tables in the query into equally sized buckets, using a Hashing Function, so that it can access these data in a quick manner. In this case, the SQL Server Optimizer will use the Hash Match operator to perform that action

For more information, check SQL Server Execution Plan Operators – Part 3

Q17: What is the Lazy Spool operator in the Execution Plan?

A: The SQL Server Lazy Spool is used to build a temporary table on the TempDB and fill it in a lazy manner. In other words, it fills the table by reading and storing the data only when individual rows are required by the parent operator

For more information, check SQL Server Execution Plan Operators – Part 4

Q18: What is the Parallelism operator in the Execution Plan?

A: The parallel plan is used by the SQL Server Engine to execute the expensive queries faster. The SQL Server Engine decides to use a parallel plan to execute the query when the SQL Server is installed on a multi-processor server, the number of threads that are requested are available to be assigned, the value of the Maximum Degree of Parallelism option is not equal to 1 and the cost of the submitted query is larger than the Cost Threshold for Parallelism value. The Parallelism operator is used by the SQL Server Engine to execute the query using a parallel plan

For more information, check SQL Server Execution Plan Operators – Part 4

Q19: How could we take benefits from the SQL Server Execution Plan in tuning the T-SQL queries performance?

A: The SQL Server Execution Plan can be used in identifying the bad performance parts of the query. The first thing to look at is the most expensive operator with the highest cost, compared with the overall query cost. In addition, having a fat arrow, which is followed by a thin one, is an indication of the missing index that forced scanning a large amount of data to retrieve a small number of records

Next, you need to search for the extra operators, as its overhead will degrade the query performance. Also, the Scan operators that read the overall table or index is an indication of a missing index, the existing index is badly used, or the submitted query has no filtering condition. The Execution Plan Warnings messages are a sign of different query performance problems that should be checked

For more information, check Using the SQL Execution Plan for Query Performance Tuning

Q20: What is the SQL Server level option that can be used to enhance the Plan Cache usage performance and minimize the memory pressure when the majority of your system workload are ad-hoc queries?

A: You can enable the Optimize for Ad hoc Workloads option, to store the SQL Execution Plan of the query in the Plan Cache at the second execution of the query

For more information, check Saving your SQL Execution Plan

Q21: How long will the plan be stored in the Plan cache?

A: It is useless to keep the SQL Server Execution Plan in the Plan cache forever. The SQL Server Engine will automatically drop any plan from the Plan Cache whenever more memory is required by the system or when the plan becomes old and not called for a long time. The SQL Server Engine users the Lazy Writer system process to clean these aged plans

For more information, check Saving your SQL Execution Plan

Q22: How could we explicitly clear the Plan cache?

A: Using the DBCC FREEPROCCACHE T-SQL command

Could you please some items which you may see in an execution plan indicating the query is not optimized. 

  1. Index Scan or Table Scan
  2. Hash Joins
  3. Thick arrows (indicating large work tables)
  4. Parallel streams (Parallelism)
  5. Bookmark lookup (or key lookup)

What is Column Store Index?

 Ref:

https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/what-are-columnstore-indexes/

Difference between IN and IF EXISTS?

 

IN vs. EXISTS Comparison Chart

The following comparison chart explains their main differences in a quick manner:

SNIN OperatorEXISTS Operator
1.It is used to minimize the multiple OR conditions.It is used to check the existence of data in a subquery. In other words, it determines whether the value will be returned or not.
2.It compares the values between subquery (child query) and parent query.It does not compare the values between subquery and parent query.
3.It scans all values inside the IN block.It stops for further execution once the single positive condition is met.
4.It can return TRUE, FALSE, or NULL. Hence, we can use it to compare NULL values.It returns either TRUE or FALSE. Hence, we cannot use it to compare NULL values.
5.We can use it on subqueries as well as with values.We can use it only on subqueries.
6.It executes faster when the subquery result is less.It executes faster when the subquery result is large. It is more efficient than IN because it processes Boolean values rather than values itself.
7.
Syntax to use IN clause:
SELECT col_names 
FROM tab_name 
WHERE col_name IN (subquery);
Syntax to use EXISTS clause:
SELECT col_names
FROM tab_name
WHERE [NOT] EXISTS (subquery);

Ref:

What are Allocation Units in SQL Server?

 

In SQL Server 2000, there used to be hard limit on the data that can be stored in a single row, which is 8,060 bytes. So, if the data exceeds this limit, the update or insert operation would fail!

Fortunately, in later SQL Server versions, rows are dynamically managed to exceed this limit and the combined width of the row can now exceed the 8,060 byte limit. I wanted to refresh this in our memory as this will help us to better understand the allocation units concept.

Introduction

SQL Server organizes all the data in pages. That includes the actual table data, index data, large object data and row overflow data. The pages that make up a table are however not all assigned to the table itself. Instead, they are grouped in logical units called allocation units.

The Allocation Unit

A table can have many indexes. Each index or heap, including the base table can in turn have many partitions. In fact, in SQL Server every table or index is partitioned. However, if you do not specify an explicit partition scheme, all the data of the index or heap goes into a single partition.

Each partition stores the table or index rows that belong to that partition. All the pages required to store that information are grouped into an allocation unit. If the partition also contains row overflow data, another allocation unit is created to contain all pages with row overflow data. If large binary objects are present, all the pages for that type of data make up yet another allocation unit. That means, in SQL Server versions up to 2012, a single partition of an index or heap can contain up to three separate allocation units.

What are Allocation Units in SQL Server:

Every partition in a SQL Server table can contain 3 types of data, each stored on its own set of pages. And each of these types of pages is called an Allocation Unit. Below are the 3 types of Allocation Units.

  • IN_ROW_DATA
  • ROW_OVERFLOW_DATA
  • LOB_DATA

So, an Allocation Unit is basically just a set of particular type of pages. Now, let us try to understand each of these allocation units using a demo.

  • IN_ROW_DATA 

When the row size stays within the 8,060-byte limit, SQL Server stores all of the data in the IN_ROW_DATA allocation unit and usually this unit holds the majority of data in most of the applications.

To better explain the concept, I came up with this simple Demo:

--Create a sample db AllocationUnitsDemo
USE master
GO
CREATE DATABASE AllocationUnitsDemo
GO

--Cretae a sample table ProductDetails in the AllocationUnitsDemo db
--Total length of the row in this table is 1000 + 4000 = 5000 (< 8000)
Use AllocationUnitsDemo
GO
CREATE TABLE ProductDetails
(
ProductName varchar(1000),
ProductDesc varchar (4000), 
)
GO

--Check the allocation unit type
Use AllocationUnitsDemo
GO
SELECT type_desc, total_pages, used_pages,data_pages 
FROM sys.allocation_units
WHERE container_id = (SELECT partition_id FROM sys.partitions 
WHERE OBJECT_ID = OBJECT_ID('ProductDetails'))

Results:
In_Row_Data
  • ROW_OVERFLOW_DATA 

Remember the introduction? so, when the row exceeds the 8,060-byte limit, SQL Server then moves one or more of the variable-length columns to pages in the ROW_OVERFLOW_DATA allocation unit.

We still have a limitation here for the row size. Though the combined width of the row can exceed the 8,060 byte limit, the individual width of the  columns must be within the limit of 8,000 bytes. This means we can have a table with two columns defined as nvarchar(5000), nvarchar(5000), but we are not allowed nvarchar(10000)

Demo Continued..

--Add an extra column to the above table ProductDetails
--Make the total length of the row to become 5000 + 4000 = 9000 (>8000)
Use AllocationUnitsDemo
GO
ALTER TABLE ProductDetails ADD ProductSummary nvarchar(4000) 

--Now, Check the allocation unit type
Use AllocationUnitsDemo
GO
SELECT type_desc, total_pages, used_pages,data_pages 
FROM sys.allocation_units
WHERE container_id = (SELECT partition_id FROM sys.partitions 
WHERE OBJECT_ID = OBJECT_ID('ProductDetails'))

Results:
Row_OverFlow_Data
  • LOB_DATA 

If a column with LOB data type is defined, then SQL Server uses the LOB_DATA allocation unit. To know what data types are considered LOB and to get the list of LOB columns from a database, please refer my previous post: “SQL Server – Find all the LOB Data Type Columns in a Database Using T-SQL Script

Demo Continued..

--Add LOB data type column to the table ProductDetails
Use AllocationUnitsDemo
GO
ALTER TABLE ProductDetails ADD ProductImage Image

--Again, Check the allocation unit type
Use AllocationUnitsDemo
GO
SELECT type_desc, total_pages, used_pages,data_pages 
FROM sys.allocation_units
WHERE container_id = (SELECT partition_id FROM sys.partitions 
WHERE OBJECT_ID = OBJECT_ID('ProductDetails'))

Results:
LOB_Data
--Cleanup
Use master
GO
DROP DATABASE AllocationUnitsDemo

How many Allocation Units can a Table have?

It actually depends on the number of partitions and indexes on the table.

To simplify the concept, as shown in the below picture, assume there is one table having no indexes (HEAP) and no partitions. Having no partitions mean, all of the table’s contents are stored in a single partition, meaning every table has at-least 1 partition.

AllocationUnits_Figure1

Based on the above, we can have upto 3 allocation units for a table with no partitions and no indexes. And how about if we have partitions and Indexes? Below is the formula I came up with to get the maximum possible number of allocation units per table.

  • No of Allocation Units = No of Partitions × No of Indexes × 3

AllocationUnits_Count

So, as we see from the figures above, a table can have up to 45 million allocation units in SQL Server 2012!