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.


Comments (3) -

Caitlin Umana

With thanks! Valuable information!

카지노

With thanks! Valuable information!

바카라사이트

here! Good luck for the next!

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading