• Home
  • SQL Server
    • Articles
    • T-SQL Puzzles
    • Output Puzzles
    • Interview Questions
    • Performance Tuning
    • SQL SERVER On Linux
    • Resources
  • SSRS
    • SSRS Articles
    • Interview Questions
  • SSAS
    • SSAS Articles
    • DAX
  • SQL Puzzles
  • Interview Questions
    • SQL Interview Questions
    • Data Interview Questions
  • Python Interview Puzzles
  • New Features(SQL SERVER)
    • SQL SERVER 2017
    • SQL SERVER 2016
    • SQL SERVER On Linux
  • Social
    • Expert Exchange
      • Top Expert in SQL
      • Yearly Award
      • Certifications
      • Achievement List
      • Top Expert of the Week
    • HackerRank (SQL)
    • StackOverflow
    • About Me
      • Contact Me
      • Blog Rules

Improving my SQL BI Skills

Improving my SQL BI Skills

Daily Archives: August 14, 2015

SQL Server Optimization – Parallel Execution Plans / Parallelism in SQL Server

14 Friday Aug 2015

Posted by Pawan Kumar Khowal in SQL Performance Tuning

≈ 1 Comment

Tags

26 performance tuning questions and solutions, Avoid SQL Server functions in the WHERE clause for Performance, Avoid Using Function in WHERE Clause – Scan to Seek, Avoid Using Functions in WHERE Clause tutorial and example, Functions in the WHERE Clause, How to avoid convert function in the where clause, How to tune SQL queries, Increase SQL Server performance avoid using functions in Where Clause, Interview questions for SQL Server Performance Tuning, Looking for SQL Optimization Interview Questions, performance sql server, Performance tips for faster SQL queries, Performance Tuning, Performance Tuning for SQL Server, Query Optimization, Query Performance Tuning, sql - How do I avoid functions like UPPER in where clause, SQL Complex Queries, SQL Optimization Interview Questions, sql performance, sql performance and tuning, sql performance explained pdf, sql performance tips, SQL Performance Tuning, sql performance tuning and optimization, SQL Performance Tuning and Optimization - Where CTEs are stored ?, sql performance tuning interview questions, sql performance tuning tips, SQL Query Optimizer, SQL Query Tuning or Query Optimization, SQL Server 2008 Query Performance Tuning Distilled, SQL SERVER Interview questions, SQL Server Optimization - Parallel Execution Plans, SQL server optimization interview questions and answers, sql server performance query, sql server performance slow, SQL Server Performance Tuning, SQL Server Performance Tuning Tips, SQL Server Query Optimization: Parallel Plans, SQL SERVER Tips, SQL Tuning Overview, SQL Where Clause Performance, T-SQL Best Practices - Don't Use Scalar Value Functions in Where Clause, Tips for SQL Database Tuning and Performance, Top 10 performance tuning tips for relational databases, WHERE Clause Functions: A Word of Caution, Where CTEs are stored ?</, Which one is better for performance Table Variables or Temp Tables – Only in terms of performance optimization?


SQL Server Optimization – Parallel Execution Plans

What is Parallelism in execution plans?

Basics-

A parallel plan is any plan where at least a single iterator is split into multiple threads and these threads run in parallel. Please note that Not all operators are parallel aware. Meaning not everything can be execute in parallel.

This means SQL Server internally manages of the things in parallel while executing a query. Please note that there is no plan which is 100% parallel. We can have a plan which is 100% serial and we can have plan where some of the things happened in parallel. We will at least have one iterator which works in serial manner. That is the first operator. The first operator can be Select, Insert and Delete.

Now how SQL Server decides that it has go with the parallel plan or serial plan. Also note that sometimes parallel plans are better and sometimes serial plans are better because for parallelism also we have to pay for the cost of dividing the iterator task (shifting data) into multiple threads and then we have gather all the threads and make them one for the next iterator at some in time.

During query optimization, SQL Server checks queries that might benefit from parallel execution. SQL Server inserts exchange operators into the query execution plan to prepare the query for parallel execution.

An exchange operator is an operator in a query execution plan that provides process management, data redistribution, and flow control. The exchange operator includes the Distribute Streams, Repartition Streams, and Gather Streams logical operators as subtypes, one or more of which can appear in the Showplan output of a query plan for a parallel query.

The SQL Server query optimizer does not use a parallel execution plan for a query if any one of the following conditions is true:

  • The serial execution cost of the query is not high enough to consider an alternative, parallel execution plan.
  • A serial execution plan is considered faster than any possible parallel execution plan for the particular query.
  • The query contains scalar or relational operators that cannot be run in parallel. Certain operators can cause a section of the query plan to run in serial mode, or the whole plan to run in serial mode.

Parallel aware Iterators are

  • Clustered Index Seek
  • Clustered Index Scan
  • Parallelism ( Exchange Operators )
    • Distribute Streams
    • Repartition Streams and
    • Gather Streams

In the next post ( Parallelism – Part II ) I will write some more notes on parallelism.

Cheers!. Thanks for reading !

-Pawan Khowal

MSBISkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Server Interview Questions & Answers – # 3

14 Friday Aug 2015

Posted by Pawan Kumar Khowal in SQL Server Interview Questions

≈ Leave a comment

Tags

2.NonClustered Index, Advanced SQL tutorial pdf, change fill factor while creating NonClustered Index, clustered and non clustered index in sql, Clustered and Nonclustered Indexes Described, Clustered Index, clustered index in sql, Clustered Index vs. Non-Clustered Index in SQL Server, Clustered versus Non Clustered Index, Clustered vs. Nonclustered Index Structures in SQL Server, Covering Index, Covering Index in SQL, Difference Between Clustered and Non-Clustered Indexes, Difference between clustered and nonclustered index, difference between clustered and nonclustered index in sql, difference between clustered and nonclustered index in sql server with example, Differences between Clustered Index Vs NonClustered Index, Differences between Clustered Index Vs Primary Key, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Fill factor in Indexes, Filtered Index, Free Download SQL SERVER Interview questions, Indexes in detail, Learn Indexes in detail, NonClustered Index with Included columns, SQL, SQL Common Interview Questions, SQL Common Interview Questions and answers, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Queries asked in interviews, SQL Questions, SQL Server - General Interview Questions and Answers, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL SERVER Indexes, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, sql server interview questions and answers for net developers, SQL SERVER Tips, SQL Tips & Tricks, SQL Tips and Tricks, SQL Tricks, T-SQL Server Interview Questions, Types of Indexes, Types of Indexes in SQL


SQL Server Interview Questions

Recently I have given a technical interview for a company based out from Pune. So with out further delay will list the questions below-

1. We are going to get 1 lakh records per second in our database like we have in stock market example. So how do you design database for this kind of system.

UPDATE – Answer – https://msbiskills.com/2015/08/24/sql-server-interview-question-how-to-handle-large-number-of-insertions-in-sql-server/

2. Let’s say we are in a transaction, and the transaction got failed, so sometimes we have to commit that transaction and sometimes we have to rollback. E.g. we are withdrawing money from an ATM, we got the money and account is not updated. Then in this case how this transaction is committed or vice versa.

Answer – Will write a complete post for this question. Coming Soon…

3. Suppose you are inserting data in a table and the table has an identity column. How you do get that latest identity value from that table.

Answer – You can use Max(ID) from the column and easily find out. One more method is there IDENT_CURRENT( ‘TableName’ ).

4. How do you check the table definition in SQL Server?

Answer – Alt + F1

5. Let’s says we have a table with 3 columns say column a, column b and column c. Now we create Non Clustered index on b,c & a,b. Now there a query like select name from table WHERE b LIKE ‘Pawan’ , so which index it will use.

Answer – It will use index (b,c). For details please visit – https://msbiskills.com/2015/08/10/sql-server-interview-question-which-index-will-be-used/

6. There is a sentence and you have find out first word from that, how do you do that?

Answer –


DECLARE @F AS NVARCHAR(22) = N'abd cc sdsa'
DECLARE @F1 AS NVARCHAR(6) = N'abd'

SELECT SUBSTRING(@F,0,CHARINDEX(' ',@F))
SELECT CASE WHEN CHARINDEX(' ',@F1) = 0 THEN @F1 ELSE SUBSTRING(@F1,0,CHARINDEX(' ',@F1)) END 

7. There are two tables DEPT ( DeptID, DeptName ) and Emp ( EmpID, EmpName, DeptID, MgrID )



--Create Table
CREATE TABLE Employees  (EmpID INT, EmpName VARCHAR(20), DeptID INT ,ReportsTo INT)

--Insert Data
INSERT INTO Employees(EmpID, EmpName, ReportsTo, DeptId)
  SELECT 1, 'Jacob', NULL , NULL UNION ALL
  SELECT 2, 'Rui', NULL, NULL UNION ALL
  SELECT 3, 'Jacobson', NULL, NULL UNION ALL
  SELECT 4, 'Jess', 1, 1 UNION ALL
  SELECT 5, 'Steve', 1, 1 UNION ALL
  SELECT 6, 'Bob', 1, 1 UNION ALL
  SELECT 7, 'Smith', 2, 2 UNION ALL
  SELECT 8, 'Bobbey', 2, 2 UNION ALL
  SELECT 9, 'Steffi', 3, 3 UNION ALL
  SELECT 10, 'Bracha', 3, 3 UNION ALL
  SELECT 11, 'John', 5, 5 UNION ALL
  SELECT 12, 'Michael', 6, 2 UNION ALL
  SELECT 13, 'Paul', 6, 2 UNION ALL
  SELECT 14, 'Lana', 7, 3 UNION ALL
  SELECT 15, 'Johnson', 7, 2 UNION ALL
  SELECT 16, 'Mic', 8 , 3 UNION ALL
  SELECT 17, 'Stev', 8, 2 UNION ALL
  SELECT 18, 'Paulson', 9, 3 UNION ALL
  SELECT 19, 'Jessica', 10, 2
GO


CREATE TABLE DeptEmp
(
    DeptID INT
   ,DeptName VARCHAR(100)
)
GO

INSERT INTO DeptEmp(DeptID,DeptName)
VALUES (1,'IT'),(2,'HR'),(3,'Finance')
GO

Solution
-----------------

;WITH CTE(DeptID, EmpName , EmpId, Level,FullyQualifiedName) AS 
(
     Select DeptID , E.EmpName, E.EmpID, 0 Level
     , Cast('.'+E.EmpName+'.' as Varchar(MAX)) FullyQualifiedName  
     From Employees E Where E.ReportsTo IS NULL
     UNION ALL
     Select E.DeptID , E.EmpName, E.EmpID, c.Level + 1 , c.FullyQualifiedName+'.'+E.EmpName+'.' FullyQualifiedName
     From Employees E INNER JOIN CTE c on c.EmpID = e.ReportsTo 
)
,CTE1 AS 
(	
	SELECT D.DeptName , SPACE(LEVEL*4) + H.EmpName Hierarchy,SPACE(LEVEL*4) + CAST(H.EmpID AS VARCHAR(MAX)) 'Hierarchy/EmpId'
	, EmpName
	FROM CTE H LEFT JOIN DeptEmp D ON H.DeptID = D.DeptID 
)
SELECT * FROM CTE1


8. Suppose number of columns are not fixed ? How do you design database for this kind of system?

Answer -One way is to create a single column VARCHAR(MAX) with a separator. It works like below-


CREATE TABLE TestMulCols
(
       Data VARCHAR(MAX)
)
GO

INSERT INTO TestMulCols VALUES('a,b,c,d,e')
INSERT INTO TestMulCols VALUES('r,e')
INSERT INTO TestMulCols VALUES('d,q,j,k,o,i,i,i,i,2')

;WITH CTE AS
(
       SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) Rnk, Data FROM TestMulCols
)
,CTE1 AS
(
       SELECT tr.ID , tr.VALUE, CTE.Rnk FROM CTE
       CROSS APPLY (SELECT ID,Value from dbo.udf_Split(Data,',')) tr
)
SELECT * FROM CTE1
PIVOT 
(MAX([Value]) FOR ID In ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) p


Output

Rnk 1 2 3 4 5 6 7 8 9 10
1 a b c d e NULL NULL NULL NULL NULL
2 r e NULL NULL NULL NULL NULL NULL NULL NULL
3 d q j k o i i i i 2

9. You are not able to connect to your SQL Server from .Net application? What you will check and how you will rectify the connection problem.?

Answer-

You can start by checking following things-
1. First check whether you are able to connect to the server where SQL Server is installed.
2. Your SQL Server should be up and running. Check from Services.msc or SQL Server connection manager. Connect using SSMS and check.
3. TCP/IP should be enabled in SQL Server Configuration Manager. Default port is 1433.
4. Add a port under exception in windows firewall. Port Number should be 1433 (Default One)
5. Also allow remote connections from the server. ( Server -> Properties -> Connections -> Allow remote connections to this server )

Cheers, Thanks for reading !

-Pawan Khowal

MSBISkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

Blog Stats

  • 1,087,023 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,131 other subscribers

Pawan Khowal

502 SQL Puzzles with answers

Achievement - 500 PuzzlesJuly 18, 2018
The big day is here. Finally presented 500+ puzzles for SQL community.

200 SQL Server Puzzle with Answers

The Big DayAugust 19, 2016
The big day is here. Completed 200 SQL Puzzles today

Archives

August 2015
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« Jul   Sep »

Top Articles

  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…

Archives

  • October 2020 (29)
  • September 2018 (2)
  • August 2018 (6)
  • July 2018 (25)
  • June 2018 (22)
  • May 2018 (24)
  • April 2018 (33)
  • March 2018 (35)
  • February 2018 (53)
  • January 2018 (48)
  • December 2017 (32)
  • November 2017 (2)
  • October 2017 (20)
  • August 2017 (8)
  • June 2017 (2)
  • March 2017 (1)
  • February 2017 (18)
  • January 2017 (2)
  • December 2016 (5)
  • November 2016 (23)
  • October 2016 (2)
  • September 2016 (14)
  • August 2016 (6)
  • July 2016 (22)
  • June 2016 (27)
  • May 2016 (15)
  • April 2016 (7)
  • March 2016 (5)
  • February 2016 (7)
  • December 2015 (4)
  • October 2015 (23)
  • September 2015 (31)
  • August 2015 (14)
  • July 2015 (16)
  • June 2015 (29)
  • May 2015 (25)
  • April 2015 (44)
  • March 2015 (47)
  • November 2012 (1)
  • July 2012 (8)
  • September 2010 (26)
  • August 2010 (125)
  • July 2010 (2)

Article Categories

  • Analysis Services (6)
    • DAX (6)
  • Data (2)
    • Data warehousing (2)
  • Integration Services (2)
  • Magazines (3)
  • Python (29)
  • Reporting Services (4)
  • SQL SERVER (820)
    • Download SQL Interview Q's (212)
    • SQL Concepts (323)
    • SQL Performance Tuning (155)
    • SQL Puzzles (331)
    • SQL SERVER 2017 Linux (6)
    • SQL Server Interview Questions (308)
    • SQL SERVER Puzzles (332)
    • T SQL Puzzles (547)
    • Tricky SQL Queries (439)
  • UI (30)
    • ASP.NET (5)
    • C# (13)
    • CSS (9)
    • OOPS (3)
  • Uncategorized (5)

Recent Posts

  • Python | The Print and Divide Puzzle October 30, 2020
  • Python | Count consecutive 1’s from a list of 0’s and 1’s October 30, 2020
  • Python | How to convert a number into a list of its digits October 26, 2020
  • Python | Validate an IP Address-IPV6(Internet Protocol version 6) October 26, 2020
  • Python | Print the first non-recurring element in a list October 26, 2020
  • Python | Print the most recurring element in a list October 26, 2020
  • Python | Find the cumulative sum of elements in a list October 26, 2020
  • Python | Check a character is present in a string or not October 26, 2020
  • Python | Check whether a string is palindrome or not October 26, 2020
  • Python | Find the missing number in the array of Ints October 26, 2020
  • Python | How would you delete duplicates in a list October 26, 2020
  • Python | Check whether an array is Monotonic or not October 26, 2020
  • Python | Check whether a number is prime or not October 26, 2020
  • Python | Print list of prime numbers up to a number October 26, 2020
  • Python | Print elements from odd positions in a list October 26, 2020
  • Python | Print positions of a string present in another string October 26, 2020
  • Python | How to sort an array in ascending order October 26, 2020
  • Python | How to reverse an array October 26, 2020
  • Python | Find un-common words from two strings October 26, 2020
  • Python | How to convert a string to a list October 26, 2020
  • Python | Find unique words from a string October 26, 2020
  • Python | Calculate average word length from a string October 26, 2020
  • Python | Find common words from two strings October 26, 2020
  • Python | Find the number of times a substring present in a string October 26, 2020
  • Python | Find maximum value from a list October 26, 2020
  • Python | How to find GCF of two numbers October 26, 2020
  • Python | How to find LCM of two numbers October 26, 2020
  • Python | How to convert a list to a string October 26, 2020
  • Python | Replace NONE by its previous NON None value October 26, 2020
  • Microsoft SQL Server 2019 | Features added to SQL Server on Linux September 26, 2018

Create a website or blog at WordPress.com

  • Follow Following
    • Improving my SQL BI Skills
    • Join 231 other followers
    • Already have a WordPress.com account? Log in now.
    • Improving my SQL BI Skills
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar