Friday, August 29, 2008

Clustered and non clustered index

Just to explain the main difference between Clustered and non clustered index with a simple example.

Clustered index makes data storage in a sorted order based on the key/s used for indexing, while non clustered index does not.

Just have a look to the below piece of T-SQL statements to remember this -


Clustered Index Example -


drop table Clustered_Table
Go

create table Clustered_Table
(
id int,

name varchar(100)
)
Go


--Create Clsutered Index
Create Clustered Index Clust_ind_id
on Clustered_Table(id)
GO


-- Insert few records
Insert into Clustered_Table Values (1, 'Avaneesh')
GO
Insert into Clustered_Table Values (5, 'Suresh')
GO
Insert into Clustered_Table Values (3, 'Dinesh')
GO
Insert into Clustered_Table Values (2, 'Mahesh')
GO
Insert into Clustered_Table Values (4, 'Vikash')
GO


O/P –


Id name
1 Avaneesh
2 Mahesh
3 Dinesh
4 Vikash
5 Suresh



Note the output is in sorted order.





Non Clustered Index Example -

drop table Non_Clustered_Table
Go
create table Non_Clustered_Table
(
id int,

name varchar(100)
)
Go


-- Create non clustered index
Create nonClustered Index test_Clust_ind1
on Non_Clustered_Table (id)
GO


-- Insert the records
Insert into Non_Clustered_Table Values (1, 'Avaneesh')
GO
Insert into Non_Clustered_Table Values (5, 'Suresh')
GO
Insert into Non_Clustered_Table Values (3, 'Dinesh')
GO
Insert into Non_Clustered_Table Values (2, 'Mahesh')
GO
Insert into Non_Clustered_Table Values (4, 'Vikash')
GO


-- get the records with non clustred index used
select * from Non_Clustered_Table



O/P –

Id Name
1 Avaneesh
5 Suresh
3 Dinesh
2 Mahesh
4 Vikash


Note the order of the records. Same as we had entered.

Hope it will help you somewhere.
Bye.

Monday, August 18, 2008

Façade pattern

Design patterns provide solutions to frequently occurring problems and also these patterns make the software efficient by suggesting effective ways.
FACADE is one the patterns which is commonly used.

What is Façade pattern ?
In this pattern the external access to a software system is made organized and efficient. A system containing no of sub systems (modules) can exposes many interfaces to the
external systems (clients ). Frequently for getting access to the required module, intelligence needs to be added at client side only. It requires internal knowledge of the
system being accessed and could make the client side little thick.
Facade pattern makes the system to expose only single interface i.e. point of contact and internally makes the decision about the redirection of the client request to the
correct module.

Advantages?
Thus the system access is more organized and easy for the client system.

The below example shows a Facade implementation. In this, there could be no of schemes in account system which have different formulae for interest rates. Depending upon the passed scheme code, correct scheme function is invoked to compute the interest rate.


#include "stdafx.h"
#include "iostream.h"

enum Scheme { A, B} ;

class SchemeA
{
public :

double GetInterest(float dbAmt, int intTenure)
{
return ( 0.02 * dbAmt / intTenure );
}
};


class SchemeB
{
public :

double GetInterest(float dbAmt, int intTenure)
{
return ( 0.03 * dbAmt / intTenure );
}
};

// Facade class with interface GetInterest(...)

class Facade
{
public:

double GetInterest(Scheme SchemeType, float dbAmt, int intTenure)
{

// decision making for reditrection of the client request
if (SchemeType == A)
{
SchemeA objA;

return (objA.GetInterest(dbAmt, intTenure));
}

if (SchemeType == B)
{
SchemeA objB;

return (objB.GetInterest( dbAmt, intTenure));
}

}
};


// client system for the above Facaded system
int main(int argc, char* argv[])
{
Facade objFacade;

// call to the Facade interface
cout<< objFacade.GetInterest(A, 5000, 14);
return 0;
}