Pages

Monday, September 7, 2009

Mindcracker Most Valuable Professional Award - 2009

Just got an award from one of the most popular Technical community...

Dear Vijaya,

Congratulations! You have received the Mindcracker Most Valuable Professional Award for 2009. This award is given for your extra ordinary contributions on C# Corner and Mindcracker Network web sites.

Thank you for your valuable contributions and time. We really appreciate it.

Please send us your latest shipping address (day time) and phone number and we will ship you the prizes.

Thank you again for being a part of most popular community for .NET developers.

Cheers!

Praveen Kumar
Editorial Director
Mindcracker Network

Tuesday, August 25, 2009

Implementation of NOT IN operator with out using NOT IN....

One question that people always ask is “Is there any other way to do it”. Even if you give all the possible solutions to a problem still the same question arises.

If some one is asking you a question, I have a problem with my SQL query using “NOT IN” operator. It is taking long time to run. Can you give me alternative solution to the “NOT IN”? This is very generic question and the best answer to this kind of question is "It Depends".

I am sure the question looks very familiar to most of us. In this post I am going to provide different ways to implement “NOT IN” operator. Although it’s going to be extremely difficult to say which one is better? It completely depends on the situation and need.

Using NOT IN Operator
==================
SELECT ProductID,
Name,
ProductNumber,
SellStartDate
FROM Production.Product
WHERE ProductID NOT IN (1, 3, 4, 10)

Using Logical Operators
===================

SELECT ProductID,
Name,
ProductNumber,
SellStartDate
FROM Production.Product
WHERE ProductID != 1 AND ProductID != 3
AND ProductID != 4 AND ProductID != 10

Using NOT EXISTS Operator with UNION ALL
==================================
SELECT p.ProductID,
p.Name,
p.ProductNumber,
p.SellStartDate
FROM Production.Product p
WHERE NOT EXISTS( SELECT * FROM (
SELECT 1 V UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 10 ) z
where z.v = p.ProductID )

Using Left Outer Join or Left Join
=========================


SELECT p.ProductID,
p.Name,
p.ProductNumber,
p.SellStartDate
FROM Production.Product p
left join
(
SELECT 1 V UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 10 ) z
on z.v = p.ProductID
WHERE z.v IS NULL

Tuesday, July 28, 2009

Database Answers To Real Life Questions -- Creative Joke

Q. What if your Dad loses his car keys ?
A. 'Parent keys not found !'

Q. What if your old girl friend spots you with your new one ?
A. 'Duplicate value on index !'

Q. What if the golf ball doesn't get into the hole at all ?
A. 'Value larger than specified precision !'

Q. What if you try to have fun with somebody else's girlfriend and get kicked out ?
A. 'Insufficient privileges on the specified object!'

Q. What if you don't get any response from the girl next door ?
A. 'No data found !' or 'Query caused no rows retrieved !'

Q. What if you get response from the girl next door and her Mom too ?
A. 'SELECT INTO returns too many rows !'

Q. What if you dial a wrong number ?
A. 'Invalid number' or ' Object doesn't exist !'

Q. What if you try to beat your own trumpet ?
A. 'Object is found mutating !'

Q. What if you are too late to office and the boss catches you ?
A. 'Discrete transaction failed !'

Q. What if you see 'theatre full' when you go to a movie ?
A. 'Maximum number of users exceeded !'

Q. What if you don't get table in the lunch room ?
A. 'System out of space !'

Parameter Sniffing & Stored Procedures Execution Plan

"Parameter sniffing" refers to a process whereby SQL Server's execution environment "sniffs" the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word "current" refers to the parameter values present in the statement call that caused a compilation or a recompilation.

Check out the complete article about Parameter sniffing

Thursday, July 23, 2009

Columnar Database - New Tech in BI space

I came accross very interesting database design to hold huge amounts of data. It's not a traditional RDBMS system. It's totally different.

A column-oriented DBMS is a database management system (DBMS) which stores its content by column rather than by row. This has advantages for databases such as data warehouses and library catalogues, where aggregates are computed over large numbers of similar data items. This approach is contrasted with row-oriented databases and with correlation databases, which use a value-based storage structure.

Check out the basics of Columnar database
Few points about this database from MVP and SQLServerBible Author, Paul Nielsen

Friday, July 17, 2009

Why We should Use EXISTS instead of DISTINCT to improve Query Performance

Today, I am going to give you a tip to improve the query performance when you are using DISTINCT to eliminate duplicate records.

My team member wants to find all the department names where each department has at least one employee.

So his query is:
SELECT distinct D.DEPARTMENT_ID,D.DEPARTMENT_NAME
FROM DEPARTMENT D INNER JOIN EMPLOYEE E
ON D.DEPARTMENT_ID = E.DEPARTMENT_ID


This is Ok where my team member is joining Employee and Department table and then taking distinct department Names and IDs.

Lets look at the execution plan:


Estimated Sub Tree Cost : 0.0182019
Query Cost : 73%


My Query is:
When I look at the requirement little bit close, the requrement says If the department has atleast one employee then return the department Name. The perfect solution which satisfies this requirement is with EXISTS.

So I modified the query with the EXISTS clause.

SELECT D.DEPARTMENT_ID,D.DEPARTMENT_NAME
FROM DEPARTMENT D WHERE EXISTS (SELECT 1 FROM EMPLOYEE E WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID)


Lets look at the execution plan:


Estimated Sub Tree Cost : 0.0067844
Query Cost : 27%


As you can see from the results there is signifact of difference in the Query Cost.

So use EXISTS instead of DISTINCT. Also it doesn't mean that it EXISTS can be used at all the places where you have DISTINCT. This basically on case by case.