U4-10686 - TypedMediaAtRoot().Descendants() calls SQL

Created by Morten Røermose 15 Nov 2017, 08:58:42 Updated by Sebastiaan Janssen 19 Dec 2017, 17:26:03

Tags: Gold partner

Subtask of: U4-9609

I am searching for a media with a specific name. To do this I use UmbracoHelper.TypeMediaAtRoot.FirstOrDefault(x => x.DocumentTypeAlias == "productImageFolder") to get image folder. I then use rootFolder.Descendants().FirstOrDefault(x => x.Name == filename) to find a media with a specific name. I have created a new MediaType (productImage) for theese images.

I expected that TypeMediaAtRoot and Descendants used Examine to search and fetch media according to this post by Shannon: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/74560-querying-umbraco-for-a-specific-media-node-best-practise

What actually is happening is that it uses SQL to search for media, even though I can find the media in both InternalIndexer and ExternalIndexer. I is a problem both in my local installation and in Umbraco Cloud.

I have attached screenshots.

Umbraco version 7.7.1 using Umbraco Cloud Using MSSQL server instead of SQLCE.

2 Attachments


Shannon Deminick 15 Dec 2017, 05:03:16

Please note that anytime you do anything like rootFolder.Descendants().FirstOrDefault(x => x.Name == filename) it's going to perform very poorly if you have a lot of items. Doesn't matter if this is in Examine or not, it's still going to allocate a ton of objects and incur a ton of lookups. I've mentioned this in the article you've referenced.

PR is here: https://github.com/umbraco/Umbraco-CMS/pull/2350 There was a "TODO" describing that we should fix this :P

To test, use the Umbraco.TypedMediaAtRoot() method call and ensure that the values returned are accurate. To be sure, add a breakpoint to the PublishedMediaCache.GetAtRoot and see that the results come from Examine

Sebastiaan Janssen 19 Dec 2017, 17:26:03

Quick test also shows it's about 10x faster. Looks good!

Priority: Normal

Type: Bug

State: Fixed


Difficulty: Normal


Backwards Compatible: True

Fix Submitted:

Affected versions: 7.7.1

Due in version: 7.7.8

Sprint: Sprint 74

Story Points: 1

Cycle: 6