SaaS CMS has officially launched! Learn more now.

Getting 200-500 Products and its Variants fast



Currently, the project that I am working on is on .NET 4.8 and Commerce 13.35. I would like to get many products (if possible, without batching/paging).

I am using the following code:

 var productLinks = _referenceConverter.GetContentLinks(codes).Values;
 var products = _contentLoader.GetItems(productLinks, CultureInfo.InvariantCulture).OfType<ProductData>();

I find the performance a bit slow (with getting the variants for each product it can go up to 10s).

Is there a better/faster way to get a large number of products loaded in one go?

May 02, 2024 11:22

GetItems is the correct way to do. As a rule of thumb, batching is the fastest way possible

I'm not seeing how you load the variants

Generally speaking - if you think some code is slow, the next step is to profile it to find the bottleneck, then optimize. you are going right direction with GetItems so continue on that path ... 

May 02, 2024 15:37


Well, this GetItems call usually takes pretty long.

But will consider what you said.

For variants I then use 


// or

var variantLink = _referenceConverter.GetContentLink(variantCode);
_contentLoader.TryGet(variantLink, out variant);

I did not find any way to optimize that call because I have some logic before getting the variants.

Edited, May 03, 2024 6:36

How many variants do you have for each product? If you have multiple, it could be faster to use IRelationRepository to get all variant links and get them in one go - again, with GetItems

How many items do you get in one go? I would suggest to start with 20 and optimize further. it sounds like you might get a big batch in one go and the db is the bottleneck. anyway, we can only rely on profiling results (I like dottrace) to make optimizations 

May 03, 2024 7:10

Thanks for the tip. I found some extra db calls that could be optimized after doing some profiling.

May 06, 2024 9:59
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.