Sunday, September 6, 2015

Most of the time I get a question from Co-developer asking “Hey Bridewin, I am stuck with a query… I have written it but output is coming slow?!?... can you look into it?”

I look at them with a smile. I always ask a question, have you written it correctly? If yes is the answer then what could be wrong? Why is the output coming so slow?

What will I do to bring the output quickly? Definitely I won’t be coming and doing any magic. Well I would be carrying out some standard steps.

Check the query to see if it is bringing some unnecessary data.

Check execution plan.

Are the necessary Indexes present in all the required columns of “Where” clauses?

In this article I would be focusing on Indexes because most of the time 2 to 3 years experienced developers are always stuck with Indexes.

Prior to creating indexes make sure you have written your query in ANSI 92 compliant or it would also be fine if you have written Oracle specific queries or SQL server specific queries.

The advantage of writing ANSI 92 compliant query is that any (Oracle/ SQL Server/ Sybase/ MySQL) DBA can help you out with the query if you get stuck with optimization problem.

If you write Oracle specific query or SQL Server specific query then only Oracle DBA/Query Tuner or SQL Server DBA/Query Tuner can help you out.

Now let’s get back to the actual topic Indexes…

So firstly let’s understand why do we need an Index?

Yes let’s ask this question in a different way to our self… why do we need an Index in a book may be text book… Yes, so that we can quickly browse through that particular page instead of scanning through the entire book... Similarly in a table instead of scanning through all records we can simply point to a particular record using Index.

Now the second question would be there are plenty of Indexes available in a database which one do I choose?

Correct if you are up to creating an Index in a table creating a right index is very necessary otherwise it would be of no use! Your query may run slower than now or may show no performance improvement. Now Indexes are database dependent, names and type of indexes vary from database to database.

I will list down the required Indexes for SQL Server and Oracle and its uses one by one…

SQL Server:

Clustered Index- This is useful when you have a unique data in a particular column something like a column having primary key. Normally a column with primary key has clustered index.

Non-Clustered Index- This is useful where a table has a clustered Index and some query is going to be executed frequently. Then Non-clustered index can be created on other column.

Unique Index- This is useful where there is ample of unique data very similar on the lines of having unique keys.

Column Store Index- This is useful where there is high use of read select query and large amount of data is retrieved. Normally used in data warehouses.

Computed Index- This is useful when the data of the column is computed like (Column 1 * Column 2 = Column 3) here column 3 can have computed index.

Spatial Index- This useful in GIS databases where data type of the column is Geometry.

XML Index-This is useful where the column has XML datatype.

Full-Text index – This is useful where we want to do word search on a particular column having large data, containing lot of text etc…

Oracle:

In Oracle indexes are not that complicated compared to SQL server.

B-Tree Index- This is useful where there are lot of data in particular column. If you know that data in the column is going to be unique then use Unique keyword while creating the index this will optimum result.

Function based Index- Oracle does not have any restriction like SQL server it allows to create index on Functions. This is useful where functions are going to be applied on particular column something like upper/lower etc.. This Index is not suitable for Geo spatial (GIS) database.

Bit map Index- This is useful where there is large amount of data and ad hoc queries, but a low level of concurrent DML statements.

After creating the required indexes on daily basis statistics need to be gathered, otherwise these indexes would be of no use and performance of query will gradually decrease.

Now after reading and understanding you will be able to choose the correct index for your table and improve the performance of your query.

If you notice there is something that we are getting for free! a typical Indian culture that MVC is giving, by one get one free we created Controller and we got View along with it.

By selecting this template we got a 5 pages made automatically by Visual Studio 2013 itself.

Create.cshtml

Edit.cshtml

Delete.cshtml

Details.cshtml

Index.cshtml

Now a little bit of coding as promised I'll make it minimum not more than one line.

Open Views>Shared>_Layout.cshtml and then add a pointer that pointing to User Master so that it displays in the menu.

Simply copy paste this line into menu section.

As shown in below screenshot.

Now your main page should look like this once compiled in visual studio.

To add a record simply click on the User Master and then click Create.

Now add a record as shown below.

Click on create to create a new record.

Added record should be visible as shown below.

In case if you wish to Edit/Delete/View details click on the respective links.

As part of Indian Culture... Again something free... This site will also work fine in mobile browser because it is using Boot Strap to prove that simply re-size the browser and on the fly the menu will adjust automatically as per the size of the browser.

That's it we are done with the Creation of MVC Zero Coding Method or Database First Method. The advantage of this method is that this has minimum amount code to be written because this is highly reliable on the database part.

This method is suitable for creation of master or in places where there is less amount of complexity. Remember this may not be suitable for places with high amount of complexity otherwise you will land up scribbling code here there everywhere...

Similarly you will find some other methods like Code First Method etc... Its up-to you which method to follow.

I'm sure that you will be having your drink in the bottle to finish!!!

In case if you were unable run or complete this project successfully just download the code and check what have you missed.

Wednesday, December 31, 2014

It’s been almost approx. 5 days I am riding a Glamour bike,
well it’s a stylish bike! Ya the one which I was riding was of an ancient model
may be of 2006 or something not very sure! It took almost 2 days for me to
understand why I need to pull accelerator while doing kick start!! Oh! Ya.. It
does not have a self-start but it`s an awesome bike… Sometime it is very
rightly said Old is Gold and in case of bike if we maintain it correctly with
timely servicing and everything then yes it would be awesome!!!

As a not very experience person in riding bike… something I noticed
is that after a long ride I didn't come to know in which gear was I riding
the bike whether it is 2nd , 3rd or 4th.
Although I have used 4th gear very rarely but I wish there was an
indicator nearby speedometer stating the gear in which I was riding the bike! I
hope Motorcycle Company starts adding this feature inside the upcoming models!! Pardon me if it sounds foolish!!!

Now let’s start comparing what’s there in the Old Glamour
and the new Glamour (Programmed Fi):

Speedometer & Fuel Indicator: In old it is analogue and
in new Fuel Indicator is digital (Now a days almost all new bikes come with
digital Speedometer & Fuel Indicator well this bike is an exception?!?!) It is noticeable that is why I have written!!

Starting mechanism: This would be secondary thing the rider
would notice in earlier model there was only kick start, but the new one comes
with the Kick as well as self-start.

Fuel Engine: The owner of the old bike said it can hold up-to 15 litter
of fuel but the new one can only hold 12 litter + 1.5 litter! God knows whether
the company thought of taking off the extra fuel space! Good if they have actually increased the mileage!!

Last but not the least Brakes: Rear brakes remain the same whereas
the front brake has been changed to disc brakes! So be bit careful for the
front brake holders!!! or you may get a roller coaster effect!!!

Ahh..!! I can gulp all sorts of changes in the bike, car etc…
But not the Mars kind of roads we have in India… hope that improves!!!

That’s all for the timing folks stay tuned for the upcoming
posts...Cheers!!!

Saturday, July 27, 2013

Authentication

Authentication is the process of obtaining identification
credentials such as name and password from a user and validating those
credentials against some authority. If the credentials are valid, the entity
that submitted the credentials is considered an authenticated identity.

In windows authentication users are authenticated against
the users available in Windows operating system (local) or against that of
users available in Active Directory.

For windows authentication we need to set the authentication
mode to windows in web.config.

Now to impersonate the identity of IIS worker process to
that of provided by Windows operating system

We will have to set the
impersonate to True by adding <identityimpersonate="true" />

So the final code will look like this

Now the question is how do I determine whether I am truly
authenticated?Basically I can twist my
question into other way “How to get my username in Windows Authentication?”
Well that’s pretty much simple I guess.

Well for that we will have to use System.Security.Principal namespace.

The code for getting complete username along with domain
name as well as for getting only username/ userId is shown below:

C# Code:

VB Code:

So far we have seen how to get username/userid now the
question is how to determine whether the user is authenticated?

Well for that there is a simple property IsAuthenticated
in GetCurrent()
ofWindowsIdentity class.

A sample code for that is as below:

C# Code:

VB Code:

So far as I am concerned about authenticating a user things
are fine Now what if I want to check whether it really works for some other
user?

Ok for that case just go to Control Panel\User Accounts\User Accounts and inside that click on Manage
User Accounts and click on add for adding a user.