Tuesday, June 30, 2009

Strong Assembly and Weak Assembly

Both compailer are created by Compailer and AL.exe(Assembly linker,which helps to create the assembly in your project also helps to add new modules.).But the difference is only the Private and public keys.

Weak Assembly : can deploy in your application directory.Not globally(ie in GAC)
Strong Assembly: Can deploy both in application directory and globally.

if you want to create your application as strong ,you should give sigining from your project properties.it will create a public key after that only you can deploy in GAC.

sigining :sn.exe(Strong name tool) is responsible for creating the public key ,which is a part of .Net SDK(.NET Software Development Kit) .More Details

Note : the combination of file name,assembly version ,culture and the public key gives the strong name,which is guaranteed to be unique.

you can check the public key value by take command prompt of visual studio
and map your dll path(C:\Documents and Settings\anish\My Documents\Visual Studio 2008\Projects\AnishClassDll\AnishClassDll\bin\Debug) then type sn -T demo.dll.


this public key is you are given in the web config file to map this to the particular location.

Note :when you create a strong name it create a unique key like this
070200000024000052534132000400000100010031f38d3b2e55454ed52c5d2469 11011be59543

it is difficult to read the developer so modified to a human readable form like
3e95326fea73ef1783b777


Why the strong name is needed ?

This is the way that the compailer finds which assembly it wants to load.only one public key with same name is accepted in GAC.

Extension Method

This is the new feature of C# 3.0

Static method can invoke using instance method syntax.

static class contain a parameter with this key word.

E.g:

public static class NewFeature

{

public static int TointNewFeature(this int i,int j)

{

return i+j;

}

public static int TointOldFeature(int i, int j)

{

return i+j;

}

}

class App

{

static void Main()

{

int iDemo = 2.TointNewFeature(3);

Console.WriteLine(iDemo);

iDemo = NewFeature.TointOldFeature(2,3);

Console.WriteLine(iDemo);

}

Features of Extension Method

  • Contains the keyword this
  • here the parameter can be leave.ie parameter should not be provided
  • can be called only by an instant variable.

In ILDASM you can see system.runtime.compilerservices .extensionAttribute for the

TointNewFeature method.ie during runtime the extension is flagged and do the rest of operation.

Using Keyword in C#

When you use the using statement the Compiler automatically throws try and finally block.inside the Finally block compiler automatically cast to IDisposable inrerface and automatically calls dispose method.

Note : using can be used only which it supports IDisposable interface.So it reduce the syntax and help to dispose method automatically.reduce the number of errors.

E,g

Using(SqlConnection con = new SqlConnection())

{

…………….

}

The compailer automatically added try - finally to ‘con’ . and do the previous operation by using IDisposable.

Common Type System(CTS)


Common Type System(CTS)

Types are mechanism by which code one programming language and can be talk to different programming language.

CTS contains contains one or more member.They are fields,method,property and events

CTS also maintains access modifiers like Private,Family,Family and Assembly,Assembly,Family or assembly and public.

Note 1) CTS rules inheritance ,vrtual functions ,object life time etc.

2) CTS rules all the type must inherit from system.object.

Conclusion : Manages the type

Common Language Specification(CLS)

CLR integrates all the language and consider as same type. i.e CLR produce

self-describing type information(metadata) and a common executable environment.

Note: Some of the things we can easily write in one language not in others .CLS makes the

development easy ,i.e metada can read all other language that uses the CLR as compailer.

Conclution : Manages different languages to support.













Temporary table in SQL

There are two types of temporary tables are available in sql server

1) Local Temporary table
2) Global Temporary table

Local Temporary table: They are visible to only the current connection of the user.For each user it will create a temporary table along with a session value and delete from the system temp table once the inistance of the user to the particular table has gone.To add this table add a sign(#) before the table.

E.g:CREATE TABLE #tblTemp
(
col1 int,etc
)

2) Global Temporary table:They are visible to any user after they are created and delete when the user references the table disconnect from the sql server.To add this table add a sign(##) before the table.


E.g:CREATE TABLE ##tblTemp
(
col1 int,etc
)


Advantages :by using this we can avoid cursors and can do as a prameterized views.

Limitations :dont use properly it will reduce the overall perfromance.

Using Temporary Tables Effectively

If you do not have any option other than to use temporary tables, use them affectively. There are few steps to be taken.

  • Only include the necessary columns and rows rather than using all the columns and all the data which will not make sense of using temporary tables. Always filter your data into the temporary tables.
  • When creating temporary tables, do not use SELECT INTO statements, Instead of SELECT INTO statements, create the table using DDL statement and use INSERT INTO to populate the temporary table.

e.g :select a into #tblani from dbo.table_execplan --Dont use this one
e.g:
CREATE TABLE #tbl_ani1 (col1 nchar) INSERT #tbl_ani1 (col1) (select a from dbo.table_execplan)--try to use this one

  • After you finish the using your temporary table, delete them. This will free the tempdb resources. Temporary tables are deleted when connection is ended. but do not wait until such time.
  • When creating a temporary table do not create them with a transaction. If you create it with a transaction, it will lock some system tables (syscolumns, sysindexes, syscomments). This will prevent others from executing the same query.

Monday, June 29, 2009

linked server


Linked server is used to execute command against OLEDB datasource on different server.

Advantages :

· Remote server access

· Distibuted query ,update,command and transaction on heterogeneous datasource across the enterprice.

· Ability to address diverse datasource similarity

Linked Server components :

Oledb provider and oledb datasource

Oledb provider-is a dll that manages and interact with specific


Linked server details






When a client appln execute a distributed query through a linked server ,sql server break down into command and send it to rowset and req to oledb. The row set exec and open the table from the provider.

Managing linked server Definition



To work in linked server ,register the connection formula and datasource information to sql server .After registration datasource ll always reffer to the logical name provided.

e.g

EXEC

sp_addlinkedserver @server='74.55.278.251',
@srvproduct='',
@provider='SQLNCLI',
@provstr = 'DRIVER={SQL server }; server=databasename;UID=username;PWD=password;'
@datasrc='74.55.278.251'

by using proc we can add and delete the linked server

sp_addlinkedserver – to add the linked server.

Sp_linkedservers –to see the information in the linked serer.

Sp_addlinkedserver and sp_linkedservers to see more information

The tables affected are

For server details:

select * from sys.servers

For linked server Login Details :

select * from sys.linked_logins

sp_dropserver – used to delete the added server and also we can remove the remote server.


By using sql server enterprise Manager


I am not explaining this.


Identifying a Data Source Using a Linked Server Name


After registered a linked server four part name that help for the transaction.


Part Name Desc


Linked_server_name Linked server referencing the the oleDB data source


Catalog Catalog in oledb datasource Containing object


Schema Schema in the catalog contain the object


Object_name Data object in the schema




How CLR Locates and binds assemblies


.NET loads the assemblies in a structured manner.CLR is responsible to locating and binding assemblies.

Step 1: Check for the referred assembly upon the name and version.

Step 2: Check in previous referred assembly

Step 3: check in GAC

Step 4: Code base or probing

Here the information can over ride the pervious version. Here I am explaining what is happening in each step.

Step 1: Check referred assembly name and version.




· Check in the application config after the manifest check.

· Check for publishers policy (when the shared/public assembly has new version).this can be managed by

· Check in machine config(manages all .NET files in the machine)

Step 2:check in previous referred assembly

If CLR uses the same assembly it will take this only. why it is not first step means ,to examine the which version is required.

Step 3: Check in GAC (global assembly cache)

If the assembly is not found in the first two steps it should be a strong name, so it goes to GAC.

Step 4: code base or probing

This inform to CLR which version is required.

Code base: codebase is the tag in the application config .CLR checks the defined location only. if the assembly is not there probing has terminated.

Probing : if the no codebase tag is config.CLR starts probing

Subdirectories: search the application sub directories.CLR terminates probing as soon as the assembly is found.


if u have dought mail me anishmarokey@gmail.com


Sunday, June 28, 2009

Portable executable(PE)



Portable executable(PE) is a dll or exe file created after the compilation.

Compiling source code architecture for C# is as Shown in figure



When you write the code and compiles the code the compiler(in the second block) check the syntax and produce a managed module(PE)


Parts Of PE

PE has mainly four parts

  1. PE Header : indicates the type of file (GUI or .dll) and it also contains time of the file created.Also contains the information of the native CPU code.
  2. CLR Header: Contains the information of version of managed module(PE),some flags,metadata ,the size of the metadata,strong name etc.
  3. Metadata: contains two tables,one contains types(eg:class used) and members defined in the source code and the second table contains members and types reffered by the source code)
  4. Intermediate Source Code(IL) : Compiler produce the compiled source code.the CLR then compiles the IL to native CPU instructions.
If you want to see the PE data.intermediate language deassembler(ILDASM) helps you to open the PE.

Open the visual studio command prompt and type ildasm and the PE(.dll or exe)will open the IL DASM from the open you can locate the PE and see how your PE is.





But you cant able to open the win32 dlls because it doesnot contain the CLR header and it is not created in this manner.so you cannot able to open it

If you have any dought in this mail me :anishmarokey@gmail.com

Saturday, June 27, 2009

What is Execution plan? in sql server 2005

Some of the times your proc is running slow. Because you are not looked into the execution plan.Here i explain how the execution plan is created.

Execution plan: Result of an query optimizer(that calculate the most efficient way to implement the T-sql query you submitted).By looking into this u can find out what is the problem in your query, if there is any problem.

What happens when a query is submitted?

The Query split into two stages

1) Processes that occur in relational engine.

2) Process that occur in Storage Engine.

Relational Engine

Relational engine parse the query and processed by query optimizer and produce an Execution plan.

Storage Engine

The result produce from the execution plan is sent(is a binary data) to storage engine, where data are stored and do operations like (locking, indexing transactions etc occur).

Execution plan

Execution plan is happens in relational Engine.

The following steps are happened in relational engine to produce the Execution plan.

a) Query parsing

Process: Checks the syntax

Output: Parse tree or query tree or process tree

Note: the parse tree contain the logical steps to the necessary operations for query.

If T-Sql is not a DML. So there are no opportunities to improve the performance. if T-sql is a DML So the parse tree is passing through the algebrizer.

DML: which helps developers to retrieve, insert, delete and update data in database.

Algebrizer: (process in query parsing)

Process: Resolves various name object, tables and columns.

Note: identify the each column fields (e.g.: varchar (200), int etc).It also determines the location of aggregate (group by and max etc) in the query and the process is called aggregate binding.

Algebrizer output: binary tree called query processor tree, which passed on to query optimizer.

b) Query optimizer

Which is an essential process model and says which relational engine works. Based on query processor tree and relational engine it produces the execution plan.

Cost based plan

This steps analyze which joins are used. Which calculate the cost of the execution plan, in terms of CPU.

In this step the optimizer plans which is the best plan to reduce the execution time.based on the cpu and i/o it produces the new query.

Trivial plan

If you submit a very simple query – for example, a single table with no indexes and with no aggregates or calculations within the query – then rather than spend time trying to calculate the absolute optimal plan, the optimizer will simply apply a single, trivial plan to these types of queries

Non Trivial plan

If it is a non trivial plan it performs cost based calculation. For this looks statistics maintained by sql.

Statistics: collection of data columns and indexers in data base and data distribution among them. The information for statistic is maintained by a histogram.

Histogram: tabulation of occurrence of particular values.

Plan cache: once optimizer creates execution plan this plan stored in a memory known as plan cache. if the identical plan is not there in the cache it goes to generate potential plan.

Query Execution

Once the execution plan is created it goes to storage engine and execute according to the plan.

Estimated and actual Execution plan

Two types of execution plans are there

Estimated plan: output from the query optimizer.

Actual plan: Actual query is execution.

Reuse of execution plan

The execution plan is stored in plan cache. The plan won’t store forever .as they aged out based on the age formula, it calculates the number of times this has been used. Lazywirter process(as garbage collector in .net) internally checks the all types of cache periodically scans the objects in the cache.

Following criteria removes the memory from the cache .

· More memory required by the system.

· The “age” plan reaches zero

· Plan is not being referred by the existing collection.

Plan can cause recompiled in the following ways

  • Changing the structure or schema of a table referred by the query.
  • Changing the index used by the query.
  • Dropping index used by the query.
  • Updating statistics used by the query.
  • Calling sp_recompile
  • Mixing DDL and DML .

Note: DDL (data definition language) descries the records, fields and sets makeup by the user. E.g.: create table alter table etc.

 
Counter