Try our conversational search powered by Generative AI!

Daniel Ovaska
Mar 5, 2019
  3429
(2 votes)

Performance - GetChildren vs GetBySegment

GetChildren is a decent method that is also cached in the background. But if you have 1000s of children you will get some performance hits. Another good option if you are only searching for a single content item is to use GetBySegment method. This one works well with large amounts of children with excellent performance. 

I tried it on folder that has 13000+ children (yes, bad idea) and these were the results when running on local machine

  1. GetChildren
    
    foreach (var folder in ContentRepository.GetChildren<ContentFolder>(parent)​)
    {
       if (folder.Name == customerId)
       {
          ...                    
       }
    }
    17.857s
  2. GetBySegment
     var customerFolder = ContentRepository.GetBySegment(parent, customerId, LanguageSelector.AutoDetect());​
    0.147s

So that's a pretty huge performance gain by more than a factor 100. Hope it helps someone out there to pick the right tool for the job. 

Few children => GetChildren
Many children => GetBySegment or Episerver Find...

Happy coding everyone!

Mar 05, 2019

Comments

K Khan
K Khan Mar 5, 2019 02:29 PM

Thanks for sharing, any suggestion for contentRepository.GetAncestors?

Praful Jangid
Praful Jangid Mar 5, 2019 03:07 PM

So, is this GetBySegment(...) works as a filter by ContentId you passed on (second parameter)?

But, if I see the definition here, that is accepting urlSegment as second parameter. What is that? Or do we have overloaded functions for this?

Daniel Ovaska
Daniel Ovaska Mar 5, 2019 03:17 PM

K Khan: Don't think that method will be a problem. Unless you have very very deep tree structure. Have you seen any performance issues with that one?

Praful Jangid: It's a filter but on the url segment yes. Url segment is the part of the url for that page that is visible in browser. For instance, the url segment for this blog post is performance---getchildren as you can see above in the browser. So if you know the url segment (or route segment as it's also called) you can use that to fetch the page.

Daniel Ovaska
Daniel Ovaska Mar 5, 2019 03:22 PM

So for instance, if I'm building this blog on Episerver world and I want get the 2019 folder for blogs and I have this structure

https://world.episerver.com/blogs/Daniel-Ovaska/Dates/2019/3/performance---getchildren/

If I have the parent page with url 

https://world.episerver.com/blogs/Daniel-Ovaska/Dates/

I can then use contentRepo.GetBySegment(parent,"2019",LanguageManager.AutoDetect()) to get the page with url

https://world.episerver.com/blogs/Daniel-Ovaska/Dates/2019/

Erik Norberg
Erik Norberg Mar 6, 2019 01:37 PM

If you are going to use GetBysegment you have to use caution.

I recently filed a bug about it that has been deemed "by design". If GetBySegment doesn't get a match in the provided language it will return a match for any other language it can find. It might not always be the expected or desired behaviour.

Daniel Ovaska
Daniel Ovaska Mar 14, 2019 08:52 AM

Good to know! 

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024