Archive for June, 2010

Smart and Effective Use of SQL Server’s Pool of Shared Memory

Wednesday, June 30th, 2010

Microsoft SQL Server has an effective pool of shared memory which ideally is used for two purposes .

  1. Data buffer;
  2. Execution plan cache.

It is commonly known that a query executed second time always gives response faster than the first time. In this post I’m going to discuss what works behind this and how can we make sure that the shared memory is effectively used.

What works behind this?
The shared memory part which is commonly known as execution plan cache (also known as procedure cache) save the execution plans of all the queries executed in SQL Server. When a query is executed this area of memory is looked into to determine if any of the existing execution plan can be re-used for this query. If the plan is not found then the query is parsed, compiled, optimized for execution and then the execution plan of the same is stored in the execution plan cache. That is the reason of queries working faster in subsequent time than the first time.

How to make effective use of execution plans
For each query executed in SQL Server an execution plan is saved but its not necessary that the execution plan stored is useful enough that its used for other queries also. To better understand this lets have an look how SQL Server saves the execution plan.

SELECT UseCounts, CacheObjType, ObjType, ST.Text FROM sys.dm_exec_cached_plans
cross apply sys.dm_exec_sql_text(plan_handle) as ST
ORDER BY ST.text



In the above screenshot let’s concentrate on the area which is in red. These three execution plans are the result of non-parameterized ad-hoc queries executed from the code of application inside an iteration. In this case none of the execution plans are effective, for each query a new execution plan is required. The query in the code was written something like below.

string sql = “SELECT * FROM URls where urlid=” + a;
where “a” is the varying value of the iteration

Had this query been a parametrized query only one execution plan would have been created and used for all the iteration. Let’s take an example to understand this better. Now my query would be something like.

string sql = “SELECT * FROM URls where urlid=@a”;
SqlCommand cmd = new SqlCommand(sql, myConn);
cmd.Parameters.Add(new SqlParameter(“@a”, a));

Now let’s have a look at the execution plans

In the above screenshot you would realize that only one execution plan has been created and its “usecounts” is 3, that means this plan has been used three time. Reusable execution plans means lesser CPU utilization , lesser utilization of memory for execution plan cache there by leaving more memory available for data buffer.

Conclusion
Though SQL Server maintains the execution plan of each query but making effective use of it is some what in our hands. There should be no piece of code in the application which is making use of ad-hoc queries it should always be parameterized queries. Though I personally prefer to use stored procedure since allowing direct access to the tables can be a security risk.

Entity Framework – Microsoft’s Step Towards ORM Building

Saturday, June 19th, 2010

Whenever we develop a new application, lots of things play key role to decide application architecture. Among these, database interaction is the major. In earlier days of .NET 2.0, no significant tools were available from Microsoft to create data access layer for database interaction. People had to use third party utilities for conceptual data modeling over the physical database.

Realizing this need Microsoft introduced LINQ to SQL for conceptual data modeling, though it comes with limited set of functionalities and can be used by SQL Server database only. To overcome this situation, Microsoft has taken steps to release new feature called Entity Framework with ADO.NET 3.5 SP1 release.

ADO.NET Entity Framework is an Object – Relational Mapping (ORM) framework. It builds on the concept of Entity Relationship Model by introducing a conceptual model, called the Entity Data Model (EDM) which sits between your code and the underlying schema (database). This abstraction helps a lot to reduce the efforts as well the complexity for moving relational data to object oriented programming.

Entity Framework architecture:

Here is how the Entity Framework is layered to abstract the relational schema of a database and present a conceptual model.

Entity Framework Architecture
  • Data Source: It represents one or many databases.
  • ADO.NET Data Providers: It will access data from data source.
  • Entity Data Model (EDM): EDM contains entity classes, storage container and mapping of the entity with the table.
  • Entity Client: It is an ADO.NET managed provider that supports accessing data described in an EDM.
  • Object Services: This component enables you to query, insert, update, and delete data, expressed as strongly typed CLR objects that are instances of entity types.
  • Entity SQL (ESQL): It is a derivative of Transact-SQL, designed to query and manipulate entities. Both Object Services and Entity Client components can execute Entity SQL statements.
  • LINQ to Entities: It is used to query against entities defined in the EDM.

Advantages:

  • You can use the Entity Framework against not only SQL Server but also Oracle, DB2, Informix, MySQL, Postgres etc with the help of its data providers.
  • You can create data access layer with the ease of Entity Framework wizards. Developer can work with data rather than coding against rows and columns.
  • It is truly a foundation stone for Microsoft’s data access platform. one of the use of this platform  is REST based ADO.NET data service aka Astoria.

How to build: (more…)

Cloud Computing – IaaS, PaaS, SaaS – The Game Changers!

Monday, June 14th, 2010

Cloud computing means Internet based computing mechanism where Software, Hardware and other resources which are available in shared mode and can be utilized as and when required.  These are virtual resources which offer dynamic scalability over the web – this is the essence of the Cloud computing.

There are many cloud services that can be envisioned, but the main ones are IaaS (Infrastructure-as-a-Service), PaaS (Platform-as-a-Service), and SaaS (Software-as-a-Service).

Consider this scenario:

You have an E-Commerce application which sales grocery products. You have deployed it on a dedicated server which has high CPU speed, high memory and necessary disk space. You noticed that you have major traffic on your website every evening at around 6 to 8 PM and on weekends. During this period you receive 70% of your orders. Still there are many customers who would not order on your website due to over-traffic and connectivity issues. Either you need to add a new server or need to leave that chunk of business.

Here, Cloud Computing gives you the flexibility where in you can select extra resources for your peak business hours (which are 6 to 8 PM and the weekends in this case), there by avoiding the need of extra full-time servers for managing extra traffic. This way you will be able to save dollars on your hardware and infrastructure cost and keep your clients happy by giving them needed speed and better user experience.

Cloud computing offers great value in such situations.

Cloud at its best:

We are close to that virtual world where-in we will have very light-weight PCs and all the useful local machine data will be stored in some kind of a cloud, thereby giving access to the data 24×7, 365 days from any location. Web is going to be significantly different and more effective.

Cloud and SaaS:

If you are using a SaaS model application, you don’t need to buy that application. Instead, you only need to pay a subscription fee. You need not to worry about the maintenance of that application or server where it is hosted. All you need to do is to pay your subscription fee and start using that application. SaaS application providers host it on a set of servers and sell its subscriptions to the various customers. Tomorrow, if you come and ask the SaaS provider that you need to have an account for your additional few hundreds of employees for the same SaaS application you’re using, they will change it from SaaS model to the Cloud Computing. This is how Google works.

Cloud development tools:

Microsoft Visual Studio 2010 has cloud development tools which allows the developers to write their cloud centric web applications and services. Developing scalable web applications and services have become easier now and can be tested on Windows Azure simulation environment.

Microsoft SQL Azure is a cloud based relational database service built on SQL Server technology. It provides highly scalable database service.

Interesting thing is that, any existing ASP.Net application can be converted to cloud application in few steps, with the help of above tools. See here

Summary:

Cloud computing is a fantastic approach to manage your business applications. It’s really helpful to small, medium and big sized organizations to manage their data and infrastructure securely, easily and in cost effective fashion. HP, IBM, VMware, Amazon, Google and Microsoft are extensively using cloud computing. In fact they are cloud service providers as well.

Photo Credit: betsyweber’s Flickr photostream

I am proud to be an “Environment Friendly” person! Are you?

Tuesday, June 8th, 2010

“Go GREEN” is Buzzword around the globe.

World celebrated “Environment Day” recently on June 5, 2010.

Always growing energy demand, requirements of higher productivity, greater and greater urbanization, mobility, smarter communication media and Environment Protection are few important challenges of our times.

While we are reading and brushing through ways for the environment protection, let’s see how each one of us can make a small contribution for the cause and be a part of big effect which will be for the benefits of everybody on mother earth.

Adopting a GREEN lifestyle at home is a best “first step” toward living healthier and helping to save the planet, but you can extend your influence even more by going green at work too.

Consider these ways to reduce fatal impact on the environment:

  • Think twice before lowering the temperature in summers and increasing it in winter while programming the Air Conditioners. Each degree warmer you leave the thermostat in summer, and each degree cooler you set it in the winter can save 6 to 8 % in energy costs.
  • Be a guardian of one potted plant in the home / office. Artificial greenery might be low-maintenance, but the real plants add liveliness and oxygen to your surroundings.
  • Can we adopt a policy of “bring-your-own coffee mug” for the coffee breaks and avoid using the plastic cups. Your company will reduce plastic waste.
  • Keep pushing for online meetings instead of personal meetings whenever possible, as virtual meetings can save lot of time, money and fuel.
  • Reduce the use of energy for using computers by putting them on “Power Saving Mode”
  • Save Paper and Ink. Avoid printing documents that could be emailed, and as far as possible print necessary papers on both sides. Producing a single sheet of virgin paper requires an average of 20 watt-hours
  • Using one side printed “Not-So-Important” papers for internal note pads. Switch to recycled paper and water-based inks.
  • Consider using LCD monitors or laptops in place of CRT monitors, which can use up to 70 % less electricity and generate less heat waste.
  • Use Energy Star certified equipment for energy saving.
  • Turn off all electrical equipment when not in use.
  • Do not leave water taps dripping; make sure to close them tightly after use. One drop of water wasted every second amounts to 10,000 litres per year.
  • Educate your colleague about the use of environment friendly work ideas if he/she is not following them.
  • Use CFL bulbs, they consume 1/3 energy and last 10 times longer
  • If you have option of multiple elevators, do not call all elevators at a time. See which one is nearer and call for use.
  • Use Bicycles for shorter distance tavel
  • Use cotton bags in place of plastic / paper bags
  • I know it may sound pretty cliché, but if each of us would do just a couple of small things to reduce our impact, we could probably do a lot to help stave off the global environmental crisis

Anyone can go green; there are many reasons beyond protecting environment such as

  1. Reverse Climate Change, reduce Global Warming and the hole in the Ozone Layer
  2. To ensure the bright future of next generations
  3. To preserve the limited amount of natural resources
  4. To reduce pollution which harms us by contaminating Air, Water and Soil

If you like and follow these, you’re an” environmentally conscious” person. You reduce, reuse, and recycle in your home and office, and you’ve taught your children / Colleagues / Friends to do these things, too.

Go GREEN, Feel Proud.