Cogito, ergo sum

Here's hoping my musings can help you out!

Archive for June, 2006

SQL Server best practices

Posted by mnshankar on June 18, 2006

Shankar Manamalkav

Microsoft has an awesome product in the shape of MS Sql server that does an extraordinary job of managing data. It is so feature packed that the help file itself runs into several gigs!

Remember however that there is no real substitute for good database design.
Having a killer hardware configuration housing a database that is not well engineered is a recipe for disaster.

Do go over EACH one of your tables with a fine toothed comb and make sure

  1. Every table gets both a primary key and a clustered index. The clustered index should be narrow. Please don’t ‘use’ the clustered index as a sorting mechanism for your data. Use the ‘Order by’ clause in your queries for the purpose. From my experience, ‘date fields’ render themselves very useful as clustered indexes (as most queries on tables involving date fields use date ranges as a filtering mechanism)
  2. Each table can have only one clustered index .So research your tables and applications properly before making a decision. If you are unsure, use the rule of thumb – create a surrogate (autonumber) field to serve as both the Primary key and clustered index.
  3. Use the index tuning wizard and analyze queries and sargable predicates for efficient index creation and use. Your queries should always dictate what indexes you require on your table. Remember Index are NOT required.. they are a performance boosting measure.
  4. Never overdo it. There is such a thing as ‘Over engineering’ a database. DON’T create indexes on binary values or columns that have low selectivity. The overhead in maintaining an index in such cases may defeat their very purpose.
  5. Do setup an automatic DBCC Indexdefrag routine and run it when server load is low.

Why clustered indexes SHOULD be as narrow as possible

The reason for this will become apparent if you recall how the Btree index structure stores data. The major difference between a clustered and non clustered index boils down to what the leaf nodes contain. If a table has a clustered index defined on it, then every Non-clustered index will contain the cluster key in it leaf nodes. So, if you have a wide key, you are essentially wasting storage space.

This also explains why it is computationally more economical to have a sequential surrogate key (autonumber) as the clustered key instead of a natural value (like last name). Picture the case for multiple inserts. Because names statistically tend to peak for certain alphabets (for example, there are more names that begin with ‘A’ than say ‘Q’), it may become necessary to split pages and then balance the trees AND simultaneously update every non clustered index pointer! Clearly, something that can hit performance.

Posted in Database | Leave a Comment »

Automating DBCC INDEXDEFRAG

Posted by mnshankar on June 18, 2006

Shankar Manamalkav

This is the stored procedure that I use to keep the indexes in my databases squeaky clean. It has been fairly well documented.

Click on the following link for more details regarding the Sql Server INDEXPROPERTY function

Also, read the following Microsoft article regarding Index defragmentation to get a better idea about what goes on behind the scenes.

CREATE proc dbo.MyIndexDefrag
AS

–Declare local vars
Declare @TableName varchar(50), @IndexName varchar(50)
declare @SQL nvarchar(200)
declare @rc int

–sql explanation:
–unfortunately, indexes cannot be gathered from the INFORMATION_SCHEMA.. so query system tables sysindexes and sysobjects
–note that this proc assumes there are no text column indexes (indid = 255). They are external to db and cannot be defragged. To exclude them use filter ‘AND (i.indid255)’
–o.type=’U’ restricts our result to ‘User’ databases
–Use IndexProperty to remove system generated indexes from the operation
–remember to remove dtproperties from the resultset.. dtproperties is created by SQL server as a user database to store info regarding db diagrams

Declare C1 cursor for
SELECT o.name as TableName, i.name as IndexName
FROM dbo.sysindexes i
INNER JOIN dbo.sysobjects o
ON i.id = o.id
WHERE
o.type=’U’ AND
INDEXPROPERTY(i.id, i.name, ‘IsStatistics’) = 0 AND
INDEXPROPERTY(i.id, i.name, ‘IsHypothetical’) = 0 AND
o.name<>’dtproperties’

–Open the cursor
OPEN c1

–Fetch initial row
FETCH NEXT FROM c1
INTO @TableName, @IndexName

–Iterate while there are rows to process
WHILE @@FETCH_STATUS = 0
BEGIN

–dynamically generate the DBCC indexdefrag statement
–Note that DBCC indexdefrag is preferred to DBCC dbreindex

SET @SQL = ‘DBCC INDEXDEFRAG(‘ + db_name() + ‘,[' + @TableName + '], ‘ + @IndexName + ‘) WITH NO_INFOMSGS’

–print @SQL
–Execute the statement against the database
exec @rc = sp_executesql @SQL

–Close the loop
FETCH NEXT FROM c1
INTO @TableName, @IndexName
END

–free up resources. Extremely important with server side cursors
CLOSE c1
DEALLOCATE c1
GO

Posted in Database | Leave a Comment »

Bill Gates and Philanthropy

Posted by mnshankar on June 17, 2006

The man who single handedly revolutionized the world of computing has finally had enough and wants out. I don’t blame him.
For decades, his keen business acumen and technical savvy has been the envy of the entire world.

In a span of three decades he has accomplished pretty much everything that a human being possibly can- Enough fame and fortune to last a couple of generations(maybe more if genetics has anything to say!) and enough clout to make entire governments wake up and take notice.

The sheer amount of money this one man brings to the table can dwarf even the UN.

I am confident that if he brings the same ruthless determination to philanthropy as he did to software, the world will definitely be a better place.

This is probably the first person on this planet to be satiated with the amount of money that he has. I always felt money is one of those things you can NEVER have enough of. I stand corrected-about 100 billion dollars seems to be the breaking point!

That being said, there is really no such thing as ’selfless giving’.. You give so you feel good, attain more fame and leave behind a legacy.

Here’s a thought.. Maybe it is a business decision after all.. I would not feel bad buying a new copy of Vista just because I know the money is going to help a needy person (The same reason people in the US spend 300$ to cross-subsidize the $100 laptop intended for poor 3rd world kids).

Have a great weekend…

Posted in News and politics | Leave a Comment »

Shiloh, Jolie and mass hysteria

Posted by mnshankar on June 11, 2006

WTF is going on in America?

It’s highly improbable that you missed the Shiloh excitement unless you have been living under a rock!

For God’s sakes… they are not even a family! The kid was born outside of a wedding and they have no plans of getting married either (quite ‘unchristian’ I might add).

Brad and Jennifer broke up in January 2006.. Shiloh was born in June 2006.. It does not require an advanced math degree to figure out that Brad pitt was cheating on Jennifer with Jolie! And, I’m pretty sure both of them denied it at the time. The lies that celebrities live.. Unbelievable.

Seriously.. 4 million dollars for the baby’s first photograph? That’s more money than most of us will ever see in our lifetime! This borders on lunacy. Lunacy of an entire nation (make that 2. Africa is quite gaga over the kid too).

300K donated by Jolie receives more media coverage and appreciation than billions of dollars donated by the Gates foundation.. Can anyone explain that?

Just proves that ‘pretty’ people really have it going.

And you know what- genetically the ultimate baby would have been with Brad and Aniston. She is way more gorgeous and more importantly ‘normal’!

BTW, if Jolie is interested in adopting more kids, I’m available :-)

Posted in News and politics | Leave a Comment »

Why I chose C#

Posted by mnshankar on June 1, 2006

Why I chose C#.NET to implement ‘Nerve’ (a practice management system)

Being a programmer who has used a LOT of languages, I must say that
the benefits provided by ‘managed code’ outweigh  (by an overwhelming margin I might add) any other consideration.

It is just not worth my employers time to have me tracking down memory leaks. The magic of automatic garbage collection is here to stay.. Embrace it.

Choices now got narrowed down to .NET or Java.

Don’t get me wrong.. Amazing code can be cooked up in Java.. Take a look at Azureus – the bit torrent client.. absolutely mind blowing piece of software.. Cool graphics, fantastic algorithm. Why then did I NOT use Java for this project?
Two reasons.. Database management and reporting.

This was to be almost a 100% database driven app.

Reporting was a significant aspect of what I was trying to do. At some point, I could envision having an easy to use ad-hoc reporting mechanism that could be widely and easily used. What better tool than MS Access . That all pervading MS Office beauty that may one day end world hunger and may very well be the solution for melting polar ice caps :-)

Now, MS Access links well with most relational databases.

Within the application, some advanced reporting mechanism would save a lot of time and effort. Crystal reports perfectly fit the bill.

There it was.. The most contemporary technology that offered all I was looking was.. it had to be .NET talking to SQL server (The SQL server choice for the backend was clinched because of my fondness for MS Access ADP)

C#.NET and VB.NET: I am more syntactically comfortable with C# (VB.NET and C#.NET for the most part are functionally identical. The coding effort involved is about the same).

Now the cons of my decision:

1. Windows only environment.. Who are we kidding.. Windows is here to stay.
2. Requirement of a fairly heavyweight .NET runtime. Fortunately, all target systems are state of the art win XP/2K based systems (80+gigs HDD and 512MB+ram). Also, .NET comes preinstalled with XP. So, the runtime is ‘already there’ on almost all sytems.

Did I make the right decision? I think so.. but as they say, the proof is in the pudding. Let’s wait and watch :-)

Posted in .NET | Leave a Comment »