I was under the assumption that looping was really bad. The more loops the worst the performance is.

 

I had 3 collections.

 

Let’s say communities, Products, and attachments.

Each community has products and each product have attachments.

We had like 600 communities with close to 10 products and each product has like 12 attachments.

 

We had the communities and products build. We had like 11,000 attachments which we had to add to the products.

We were looping through the 6000 community products and in each of the loop, we will loop through the 11,000 attachments.

It was taking like 23 seconds.

 

What we did was, we used Dictionaries with a key and an attachments collection.

In dictionaries, keys are stored in Hash.

 

Bucket

 

-----------------------------------------------------------

0               34 - abc                             1000 - Hashcode

                     34 - cdf

 

We build a dictionary of attachments with the key and the real attachments.

 

var attachDict = new Dictionary<string, List<IAttachment>>(StringComparer.Ordinal);

 

We build this collection by looping through the attachments.

 

foreach (var attach in Attachments)

         {

             var key = (attach.ModelId + attach.BuilderCode.Trim()).ToUpperInvariant();

             List<IAttachment> attachments;

 

             if (attachDict.TryGetValue(key, out attachments))

             {

                 attachments.Add(attach);

             }

             else

             {

                 attachments = new List<IAttachment>();

                 attachments.Add(attach);

                 attachDict.Add(key, attachments);

             }

         }

 

 

Then we looped through the communities and products and add the attachments where the key matches.

 

  foreach (var c in communities)

         {

             foreach (var prd in c.MarketingProducts)

             {

                 var key = (prd.MarketingProductId + c.BuilderCode.Trim()).ToUpperInvariant();

                 List<IAttachment> attachments;

                 if (attachDict.TryGetValue(key, out attachments))

                 {

                     prd.Attachments = new Collection<IAttachment>(attachments);

                 }

             }

         }

 

 

After making this change the query runs in < 1 sec.

----------------------------------------------------------------------------------

Sql - Please use distinct only when it is really required. When we use distinct it will copy the row to another temporary location to do a comparison

and see whether in the result set it exist, If it exists, it will skip that row otherwise it will add to the result set.


Design patterns.

December 22, 2010

What is a patterns and why do we need them ? This is a normal Question.

Patterns are a solution to a design problem.

There are a lot of patterns available, They are mainly divided to structural and behavioral patterns.

Let me a give a small Idea about factory pattern.

This is like a factory where we can build Objects as we want.We can tell the factory that I need an object of a particular type, by passing in a parameter. what the factory will do ? It will create the object and pass it on to you.

Advantages.

The end developer do not need to worry about the complexity of creation of objects. Creation of a object can be complex based on the type of object we are building. so all these process is abstracted from the end developer.