Tuesday, August 30, 2016

SQL: How to DELETE or UPDATE large data in chunks to increase the performance with no locking on table

Let's say you want to update or delete large data think of millions and millions of records, you write a single update or delete statement. It will downgrade your query performance or unnecessarily it will hold the lock on the table which will cause the applications which are using that table will be in stuck. 

So to avoid such type of situations, we can delete or update the data in chunks which will help in better performance than the previous case. 

Let's say you have a table like below, here the data is less I have taken this for demo purpose. 



And we want to delete in the chunks of 2 which means at a time only 2 records should get updated or deleted.

Below is the sample query using while loop, first you declare a variable @rowcount = 1, and in the update statement, you are specifying the count in which amount of chunks it needs to be updated this can vary based on your requirement. and in the end, you will update @rowcount with the @@ROWCOUNT. 

You can see the result, how the records got updated. 






Sunday, August 28, 2016

Can Foreach Loop iterates backwards??

Technically speaking the answer is NO, Foreach loop is meant for iterating the list what ever you pass on to it. But to execute the list in backwards using forloop we have different approaches one of the approach is below..

In C# we have Reverse() function which will reverse the list of elements.
Call Reverse() on the list.
Now, iterate using foreach loop that's it your job is doe.

Example:

Class Program
    {
        static void Main(string[] args)
        {
           List<string>  objList =  new List<string>();
           objList.Add("1");
           objList.Add("2");
           objList.Add("3");
         
           objList.Reverse();


           foreach (string obj in objList)
            {
            Console.WriteLine(str.ToString());
             }
    }
Note: This is not efficient way to iterate the list in backwards.

Friday, August 26, 2016

C# Program: Reverse words in a given sentence


To Reverse words in a full of sentence first you would need to split that sentence by space(' ') and create list of strings and push each word by word in to that list. Then  by decrementing the index value of list iterate through the list. Append each string to string builder class and print that string builder object. That's it your done ..!!!

Class Program
    {
        static void Main(string[] args)
        {
            string sample = "My name is string";

            List<string>  smaplestrnigs =  new List<string>();
            smaplestrnigs = sample.Split(' ').ToList();

            StringBuilder str = new StringBuilder();

            for (int i = smaplestrnigs.Count-1; i > -1 ; i-- )
            {
                str.Append(smaplestrnigs[i]);
                str.Append(" ");
            }
            Console.WriteLine(str.ToString());
        }
    }

Output: string is name My

Monday, August 22, 2016

SQL Can BIT data type in SQL Server allows to insert NULL??

Don't get surprised with the answer but the answer is "Yes" :D, BIT data type allows NULL value to insert. As we know that BIT can store only 1's and 0's or True and False respectively, but when we don't specify the NULL check while we create a table, then system will automatically allows you to store the NULL Values. To avoid this, create  a column with condition that it should not allow the NULL values which means explicitly you are saying to system that, hey SQL don't store any null values in my column.