25 August, 2010

SQL Optimization

Process and data modelingImage via WikipediaI didn't think about optimizing my SQL scripts until some recent events, where I was called by one of the guys in the IT Infra team told me some bad news: I'm in the top 3 list. And that isn't something to be proud of: it was the list of the scripts that is being monitored as being resource-hogging.

I immediately checked on the said script, and to my surprise, it was a very simple one!

Which means one thing: I never thought about writing SQL scripts that are optimized. for 10 years, I was simply writing scripts, not minding whether or not they are optimized.

But the good side of that is this: help was available.
The guy who flagged me of the problem is also the one who would eventually show me how to do optimization. I said I was one who didn't formally learn SQL scripting, and it was all self-study, trial and error. So the big error happened.

And to my surprise, he has the same story to tell! (Which means I have nowhere to run, but to optimize my queries...)

How did we go through the optimization? Just a few things really did happen, and I can say it worked, because my daily report running at midnight had a drastic runtime reduction - from 2.5hours to less than an hour. And I'm still optimizing the scripts, of course, with the script runtime still reducing from time to time.

And so, below are what we did, 'we' at the start, then 'me' to carry on the work, till today.


5 Simple Steps taken to optimize the scripts:
1. Table Indexing
2. Optimized Views
3. Local Tables
4. Break Where Predicates
5. Rearrange the filters

Table Indexing
My colleague pointed out that before you even write a script (and my thought raced back 10 years ago when I started writing queries, huh?) is to know whether the table holding the data I want is indexed or not. If the table(s) are indexed, go ahead and write your queries against it. if not, try the next one: optimized views

Optimized Views
Many a times an index will not be possible - due to system constraints. An index taxes the RDBMS to a certain degree, and indexing one table that has updates (insert, delete, update) frequently creates more problem than it would solve. The solution? Some top-gun guys who know the trade would just create optimized views, indexed for optimum run, and independent from the main table. Of course, these views would be updated regularly, like hourly, since that is the very reason they are created - to get away from the problem of instantaneous updates that will inhibit the creation of indexes. So don't forget that optimized views, while indexed, are usually updated on an hourly basis.

Local Tables
Sometimes there are no indexed tables, and though there are optimized views, these are enough, or simply, the data you want is not in them. Views cause the problem of additional system load if they are used with additonal criteria. A view is already a query, so additional filtering against a view will pose more system load.
So create your own local table. Create your own local tables to store temporary data. Run simple queries and store the data in your local tables. You'll be surprised how it helps!

Break Where Predicates
Okay, the very use of the local tables is when you take a complex WHERE clause, break it into individual queries, small enough to have one, single WHERE clause, and run it, saving the data in the local table. And don't forget that you should be indexing these local tables for optimum run!

For example, if you have 4 filters in the WHERE clause, break it, store the data in your local tables, and run against each previous local table.

Try out cascaded query style, running queries against the previous data, and see if that doesn't speed up your scripts.

Try using JOIN in the FROM clause instead of putting everything in the WHERE clause.

Rearrange the filters
If the filters in the WHERE clause can't be broken or can't be reduced, try rearranging the filters. Goal is to begin from the top with the filter that will immediately reduce the data size being captured, for the succeeding filters to work against as the script progresses. And it may not be done at first try, so this is a continual process, even many years after. (Hey, that is exactly my experience here).

So there you have it. 5 simple steps to optimizing queries. While these I have applied, and have worked for me, the optimization steps are not limited to these 5 alone. As in software development, the saying is true: "There is nothing permanent but change." Hopefully this helps you, and it helped me - a lot.


Till then!


Enhanced by Zemanta

12 July, 2010

XP Welcome Screen and Fast User Switching

Windows XP ProI was testing a new network connection from my cable modem since it is supposedly free. And I'm not that very familiar with this task, I know a bit of it here and there. I happen to install a network driver, and what happened next is something that I did not want: the login screen was changed, and it was asking now for a password.

I had a problem, since that computer was used primarily by my kids...

I searched the web, and for a time and a season it seems, I did not find what I was looking for - how to restore the XP Welcome Screen and the Fast User Switching function.

All that is being thrown up is either disabling the login screen or restoring it - which is not what I want.

What I was looking for is help on how to remove the now-unwanted login screen that asks for a password, when there isn't, and restore the Welcome Screen and Fast User Switching function.

I stopped searching. Tried the next day, and still failed to get an answer. Some search results seem to be a promise, but the moment you click on the link, you are 'offered' a trial membership, blah-blah-blah. I'd close that page immediately!

Microsoft Windows XP Home Edition FULL VERSION with SP2The third day, I think I got the answer.I tried typing for NetWare and XP login screen, but what it should be is "Welcome Screen and Fast User Switching" search text. That is what gave me the help I needed.

So I was able to fix the problem with the following steps:

1. Go to Settings, if you have it, then Control Panel.
2. Click on Network Connections.
3. Right-click on every network connection that you have, then on Properties.
4. Perform an uninstall for Client Services for Netware.

When this is done, go back to Control Panel.
1. Click on User Accounts.
2. Tick, as per your choice, the two options:
a. Use the Welcome Screen
b. use Fast User Switching
3. Click on Apply Options.

And we're all set!
-----

Enhanced by Zemanta

07 July, 2010

Running javac in Windows

Image representing NetBeans as depicted in Cru...Image via CrunchBase
I just began my self-study in java using Netbeans 6.9 IDE, and I just managed to get through the "Hello World!" code using the IDE, which is followed by running the same small program in the shell.

I typed the compiler as indicated in the directions, but I got an error, like 'javac is not recognized....' or something like that.

I searched the web, which is usually my primary source of assistance, as it is with my self-study matters, and I got the first page with the top results containing the solution to my problem.

It is simply including the javac location in the PATH declaration.

So I did.

I opened up a Windows Explorer, right click on My Computer, and on Properties, then on Advanced tab, then on Environment Variables, then on the top window, which is the user's "User variables".

Open up "PATH" or "path", whichever is the one in your PC, then include the javac directory, for my case, is "C:\Program Files\Java\jdk1.6.0_01\bin> javac". That means going to the end of the current definition, adding a semicolon, then the file location, which is again, "C:\Program Files\Java\jdk1.6.0_01\bin> javac".

Click on OK, then the other OK, and the final thrid OK.

That sets the path for javac. There is no need to restart your PC, as was the case for me. But if javac still isn't recognized, try to do a restart, to be sure.

Till then.

Enhanced by Zemanta