Monday, February 22, 2016

Does ISNULL check improve query performance

Today I came across something very interesting:

I had a query with a inner join between 2 tables and few more criteria in the where condition along with one condition like "int1

Both the tables had about 1 million records each. A simple select on both tables individually as well as joined returned all the records within 20 seconds. However when full query was executed on sql server, it continued to run for 3 hours, taking 100% CPU cycle and no results. I was wondering, why filtering should take that long.

I started adding conditions to where clause one after another and ran the query to see which where clause is taking more time. Everything worked fine. At the end I added the condition "int1

But a nagging feeling was there that something is not alright. Why should addition of an extra check improve performance. Am I missing something?

Next started the Google research "Does ISNULL check improve query performance". I landed on a link with exact same question.

The answer startled me :).

when I added the ISNULL condition, it did not use the existing execution plan. it created a new one for itself based on latest SQL server statistics.  Hence it completed fast. Execution plan for my query was based on old statistics and hence it was slow.

I update the statistics of my database by executing following:

EXEC sp_updatestats

This forced all the execution plan to get recreated again. A after this my original query ran as fast as other queries.

Conclusion: ISNULL check does not improve performance.

Monday, August 17, 2015

Hackerrank : Caeser Cipher in C#

//caeser cipher
int n = Convert.ToInt32(Console.ReadLine());

string s = Console.ReadLine();

int offset = Convert.ToInt32(Console.ReadLine());

char[] final = new char[n];

for (int i = 0; i < n; i++)

//bool ischar = false;

bool isSmall = false;

int curchar = (int)s[i];

if(curchar >= 97 && curchar <= 122)

isSmall = true;

curchar = curchar - 32;

if (curchar >= 65 && curchar <= 90)

int newChar = curchar + offset;

while(newChar >90)

{ newChar = newChar - 26; }
final[i] = isSmall ? (char)(newChar + 32) : (char)(newChar);




final[i] = s[i];



Find if 2 strings are anagram or not

/* find if 2 strings are anagram or not like silent and listen*/
string str1 = Console.ReadLine();
string str2 = Console.ReadLine();
bool isAnagram = true;
int strLen = str1.Length;
int[] arr = new int[256];
if(strLen == str2.Length)
for (int i = 0; i < strLen; i++)
foreach (int i in arr)
if (i > 0)
isAnagram = false;
isAnagram = false;

Linked list: Pairwise swap

private static void SwapEveryTwoNodes(LinkedList l)
            if (l == null || l.head == null)
            Node cur = l.head;
            Node next =;
            Node pre = null;
            if (cur == null || next == null)
            l.head =;
            //in every iteration, there should be minimum 3 pointer change. Before next iteration begins,
            //ensure that the pointer to previos node is saved.           
            while (cur != null && next != null)
                // cur =1, next = 2
       =; //
       = cur; //
                if(pre!=null) // this check is only for first iteration
           = next; //connect the left side of the LL with the swapped nodes
                pre = cur; //Save previous step
                cur =; //next odd numbered node
                next = cur!=null?; //next even numbered node              

Monday, April 8, 2013

Unrecognized configuration section system.web.extensions when upgrading to ASP.NET 4.0

Recently I upgraded my solution to VS 2010. My solution contained almost 30 projects. After upgrading when I ran the solution for the first time I got following error:

Unrecognized configuration section system.web.extensions when upgrading to ASP.NET 4.0

After some googling it was certain that the error was due to upgrade but I could not find exact solution. Then I opened my machine.config file (located at C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config).
It had the "system.web.extensions" section. Suddenly I noticed that the entire text in the machine.config was in small letters(system.web.extensions) whereas in my projects' we.config, the first letter of every word was in capital letters. (System.Web.Extensions). When I changed my web.config to small case, the solution worked.

One complete day wasted on such small silly thing!!!!

Thursday, May 31, 2012

Find if backup file is complete or not

Frequently we get back up files from our clients to resolve their issues. But sometimes by the time the backup file reaches us, it gets corrupted either while creating the back up or while compressing or while uploading to ftp site or while downloading from the ftp site.
When we try to restore such file, we may get different error messages.

One message that I got recently was: Specified cast is not valid. (SqlManagerUI)

One very good way to find out if the backup file is incomplete is to execute following command


It will show something like the below message which implies that the backup is not complete:

Similarly if you want to see what will be the space requirement for the restored back file it terms of the mdf and ldf, you can execute following:


Monday, December 12, 2011

How to Archive SQL error Logs

To create a new error log, execute following:

1. Restart the sql server service. It will create a new error log
2. Execute following:
Exec ('DBCC ErrorLog')
3. Execute following:
Exec Sp_Cycle_Errorlog