Tuesday, January 20, 2009

First level of Performance Tuning in SQL Server

A Simple way to performance tune your query:
a) Check if the WHERE clause is using INDEX or FULL TABLE scan using query plan
b) Ensure the columns used in WHERE clause is in the order of the index creation (if applicable)
c) Include the WHERE condition that would limit the number of records to be searched and then the JOIN condition

Recently I have created a query which took nearly 4 minutes to get executed. I did the following changes, which made my query get executed in 2 minutes.
(I have changed the table names for this sample)
Initial Query:
SELECT FROM Employee empINNER JOIN Address adr ON emp.EmpID = adr.EmpIDLEFT OUTER JOIN Phone ph ON emp.EmpID = ph.EmpIDLEFT OUTER JOIN FamilyMembers fly ON emp.EmpID = fly.EmpIDLEFT OUTER JOIN BankDet bk ON emp.EmpID = bk.EmpID
I commented all the JOINs and uncommented each JOINs and verified the execution time. Only for Phone it took more time. Hence I moved the JOIN condition for Phone at the end of the JOINs. This limited the records to be processed for the Phone table and reduced the timing.

Tuned Query:
SELECT FROM Employee empINNER JOIN Address adr ON emp.EmpID = adr.EmpIDLEFT OUTER JOIN FamilyMembers fly ON emp.EmpID = fly.EmpIDLEFT OUTER JOIN BankDet bk ON emp.EmpID = bk.EmpIDLEFT OUTER JOIN Phone ph ON emp.EmpID = ph.EmpID

Sunday, January 18, 2009

Covey's impressive answers (An extract from The Economic Times)



How can you be effective in time when negativism is so pervasive ? This is a perfect time to create long-term competitive advantage . It’s in times like these that people can adapt and make sacrifices . This is really an opportunity for creative businesses to gain long-term competitive advantage. Even if we find that we are not in control of anything, one always has the power of choice to do what one can do.

Every management expert says that tough times should be used to build competitive advantage. But is it easy to think about building competitive advantage when all you are thinking of is survival?

It’s tough because everything around people is negative. When the external factors over which one has no control in a way start to become negative it starts to affect our creative juices. You have to be creative in these times and take advantage of this ‘valley’ and turn it into a ‘peak’ , a new initiative can make a difference and the key to that is to move away from the industrial age model because the industrial age model is top down and command-and-control.

And tough times help unleash the creative force in people’s own professional lives and that feeds upon itself and the creative juices get going again. It’s not easy but it’s within reach. And it’s the perfect time to do so.

This profits vs principles war is seeing the wrong winner in some cases. Do you think there needs to be a rethink on the ‘hows’ of the business?
I think that’s part of it. People have to look up to themselves to begin with - an inside out approach. They must also learn how to involve other people all the way; to involve the entire organisation to look at these issues; to develop systems and structures which reward the right things.

What’s the new model of openness that you are advocating these days? It involves being clear and transparent. In taking the reality of what is happening with the people and then asking them: What do you think we can do in this situation? You will find that it will work on the basic underlying forces that produce trust and openness. We have had misaligned systems that rewarded people for wrong things and this has been happening all around the world.

This is the perfect time to unleash people’s creative energies and come up with new creative ideas and companies can do this with suppliers and their customers because everybody is feeding on the paranoia and the fear.

It’s like a cancer that feeds upon itself and people get victimised by all these forces. So they criticise, they complain, they compare, they become cynical, they compete. This is the perfect time to take advantage of this and turn it around. And you have to be creative on how to do that and that will be the main challenge.

Why have you focused on habits to bring about change? Because people co-relate habits of thought and habits of actions which are completely misaligned with these new realities. And it’s like trying to play tennis with a golf club. The habits, not only of people, but also the structures of the system (Covey calls them habits of organisations) of the organisation are misaligned. The result is that people get negative and pessimistic and discouraged and disparaged and that feeds on itself.

How does one prioritise one’s life when there are so many demands on one’s time because of one’s role as a father, husband, employee, boss?
You have to get everybody in the culture on the same page about what is really important. The thing that is important now is taking advantage of this difficult time by unleashing the creative energy but what’s happening now is that fear is sprayed everywhere. And people have no control over this situation. What is important is the need for a balanced life, in which you learn to say no to things that are urgent even though not important.

Most people spend half their lives doing things that are urgent even though not important and they know it. They don’t have the courage to say no to things that are not important. My Seven Habits book deals with personal and interpersonal, The Eight Habit deals with the organisational. And unless people wake up and change the structures and systems, the entire culture , it won’t happen.

Do you believe in fate? I believe in faith. One of the things that I have admired about India is the spiritualism of the people. That’s a great advantage and because of that spirituality, if one can learn to change the systems we have and learn to synergise with each other, then India can again become a dominant force in the world.

If we look at the progress India has made in the past decade, it’s tremendous. I admire that. You have such tremendous traits, only if you can get out the vindictiveness, defensiveness and the negative energy out of the way.

Full article: http://economictimes.indiatimes.com/Features/Corporate_Dossier/Be_effective_in_tough_times_and_win_back_trust_Stephen_Covey/articleshow/msid-3986583,curpg-1.cms

Friday, June 27, 2008

Further into IIS issues

Recently we faced an issue with the webapplications hosted on IIS. Something interesting to solve.
The webapplications were working perfectly fine but only in the LAN environment. When logged in from VPN they didnt work. Access denied messages were displayed. All the settings on IIS were perfectly fine with Anonymous user login, security settings for folders, etc. After a brief analysis the reasons for the issue was found out.
This issue occurs where there is a non-sync between the IUSR password in IIS and the IUSR password at the system level.

To fix this we can either change the password to make it same at system level and in IIS setting or simply restart the IIS Admin service which will recreate the IUSR account.

To sychronise the system level password and password in the IIS setting follow the steps given below:

The password can be change in the IIS settings by changing the entries in the adsutil.vbs

To know if the password entries are available in the adsutil.vbs use the following command:
cscript adsutil.vbs find anonymoususerpass
This will return a listing of all the nodes where the anonymoususerpass appears in the metabase. Ideally you should see this in only the W3SVC node (it may also appear in the MSFTPSVC node if you are running FTP). If you see any locations “under” W3SVC (i.e. W3SVC/1/root) then we should delete these entries.

To delete an entry use the following syntax:
cscript adsutil.vbs delete W3SVC/1/root/anonymoususerpass

Once we have made sure that we only have the password set at a single location, we need to “sync” the password with the one in the SAM database. The easiest way to do this is to simply open Computer Management if you are on a member server or Active Directory Users and Computers on a domain server and do a change password on the IUSR account. Then back at our command prompt, we simply set the password to match the one we just set by using the following command:

cscript adsutil.vbs set W3SVC/anonymoususerpass "passwordgoeshere"

The passwords should now be in sync and the site should now be accessible.

Wednesday, April 23, 2008

SQL Query Optimisation - Other than common ones

In addition to the common optimization tips for SQL Server query, the following are some of the useful tips.

- To view the row count from a table use, "SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2" query instead of count(*) or use sp_spaceused which will also provide the row count.
- Avoid having clause in group by and use where if necessary
- Avoid distinct clause and use it only when it is really mandatory
- Include SET NOCOUNT ON for stored procedures as it will reduce the transfer of rowcount data travelling from database to application
- In case you need to get only first n rows in a query use top or SET ROWCOUNT statement.
- Avoid using optimizer hints in any database. This may make the query faster initially but will really affect the performance as the data grows in the table.
- Try to use inner tables instead of creating temporary table whereever possible.
- Use a EXIST, NOT EXISTS or LEFT OUTER JOIN instead of a IN or NOT IN in where clause.

Tuesday, April 22, 2008

Removing Code Smells - An Example

Common code smells

Type codes and switch statements are common code smells. In addition, there are quite some other code smells that are also very common. Below is a summary list:

- Duplicate code.
- Too many comments.
- Type code.
- Switch or a long if-then-else-if.
- Cannot give a good name to a variable, method or class.
- Use names like XXXUtil, XXXManager, XXXController and etc.
- Use the words "And", "Or" and etc. in the name of a variable, method or class.
- Some instance variables are not always used.
- A method contains too much code.
- A class contains too much code.
- A method takes too many parameters.
- Two classes are using each other.

Monday, March 31, 2008

IIS Performance issue

The normal issue that comes up with the web applications hosted on IIS is related to performance. Coincidentally I came across these issues recently in my area of work. I hope this information will help to those who face similar issues.

Here are the basic steps to do when faced with performance issues in web applications:

- Check if the process Aspnet_wp.exe (IIS 5.0) or W3wp process (IIS 6.0) is consuming high memory in the server.
- If the memory level is higher, then it may affect any other services or applications running on that server.
- In case, the requirement of the application requires more memory then upgrade your system to have higher memory to support the application
- Check the Web.Config file if the Debug, Trace or both are enabled. To accomplish this, ensure that the web application is deployed to production always in release mode.

compilation debug="false"

trace enabled="false"

- Check if the executionTimeOut attribute is set to higher level. This should not be set to higher level in production environment. This will hold on to the thread till it timeout and increase the level of memory usage.
- The performance can also be improved at the coding level by avoiding any string concatenation process and instead use the StringBuilder for the purpose.
- On the software installation, ensure that .NET framework 1.1 SP1 is installed.

Here are some additional tips for writing High – Performance Web applications
http://msdn2.microsoft.com/en-us/magazine/cc163854.aspx