Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris C 43 posts 184 karma points
    Jun 01, 2014 @ 02:14
    Chris C
    0

    Selecting media... super slow

    Hello again.  :-)  I have a macro on my site that is supposed to show the newest images that were uploaded to the gallery.  

    Functionally, it works fine.  But the problem is now that I have 300+ media items, the macro is taking forever to run.  (by forever, I mean ~1 second per request)

    I have tried the following two versions of this function, and the performance was equally bad.  First this -

    var photos = mediaFolder.Descendants("Image").OrderBy("CreateDate desc").Take(12);

    And this - 

    var folder = _helper.TypedMedia(1129);
    var photos = folder.Descendants("Image").OrderByDescending(x => x.CreateDate).Take(12);

    What I think is happening, is that Umbraco is taking all the records from the database, then sorting them, then taking 12.  Which is why as the number of media items grows, the queries get slower and slower.  Is there a way to get Umbraco to do this at the database level and/or speed things up a bit?  

    This is Umbraco 6, by the way.  Thanks for the help!

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 01, 2014 @ 08:06
    Jeavon Leopold
    0

    Hi Chris,

    Could you please post your entire Partial View Macro code, so we can see if any improvements can be made?

    Have you considered macro caching as a solution?

    Jeavon

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 01, 2014 @ 08:21
    Jeavon Leopold
    0

    I was thinking An Examine query would probably be a good solution for you and I just found a solution for the same problem here

  • Chris C 43 posts 184 karma points
    Jun 02, 2014 @ 06:56
    Chris C
    101

    Wow, thanks.  I adapted the "examine" code from that page.  And now the page load time is down from ~1000 ms to <100.  

    So now the macro looks something like this (I took out irrelevant code) - 

    var criteria = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"].CreateSearchCriteria(UmbracoExamine.IndexTypes.Media);
    var filter = criteria.NodeTypeAlias("Image").Compile();
    var results = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"].Search(filter).OrderByDescending(x => x.Fields["createDate"]).Take(12);
    
                <ul>
    
                    @foreach (Examine.SearchResult photo in results)
                    {
                        thumbUrl = string.Format(thumbUrlFormat, photo.Fields["umbracoFile"]);
    
                        <li>
                            <a href="@photoPageUrl" title="@photo.Fields["nodeName"]">
                                <img alt="@photo.Fields["nodeName"]" src="@thumbUrl" /></a></li>
    
                    }
    
                </ul>

    Now the last question is how to modify that search criteria, so that it only gets the media from a given folder.  I tried a couple things, but no dice.  (all my media for this gallery is contained in a specific subfolder)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 02, 2014 @ 18:45
    Jeavon Leopold
    1

    Hi Chris,

    Sounding good so far! Are all the items you want children of a single node or descendants?

    Jeavon

  • Chris C 43 posts 184 karma points
    Jun 02, 2014 @ 20:11
    Chris C
    0

    Yes, all the media items I want to view are under a single node.  

    So I think this line needs another criteria.  Could not find the exact option to use last night though.  

    var filter = criteria.NodeTypeAlias("Image").Compile();
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 02, 2014 @ 23:23
    Jeavon Leopold
    0

    I think you need "parentID" that will have your containing node id in it

  • Chris C 43 posts 184 karma points
    Jun 02, 2014 @ 23:59
    Chris C
    0

    ParentId would be easier, but it would be ancestor though.  Because my node structure is like this: Master Gallery Folder -> Subgallery folder -> Media images.  

    So I want to pull the newest media images that have Master Gallery Folder as an ancestor.  So far I haven't been able to find an Examine equivalent to the dynamic Descendants method.  

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 03, 2014 @ 00:16
    Jeavon Leopold
    1

    Ah ok, you are going to need to use the "path" field for that. But it will need splitting, see this old post for some pointers

  • Chris C 43 posts 184 karma points
    Jun 03, 2014 @ 05:06
    Chris C
    0

    Thanks Jeavon.  You're the best.

    Per that old post, I changed the following line (from above)...

    var filter = criteria.NodeTypeAlias("Image").Compile();

    To this...

    var filter = criteria.RawQuery("+__NodeTypeAlias:image +path:\\-1*1129*");

    For anyone that might find this, the 1129 is the ID of the media folder.  Seems to be pretty fast still, <100 ms on average.  And yes, I do cache that macro.  But was concerned that if the page wasn't accessed in a while, then the first visitor would experience a long delay.  


  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 03, 2014 @ 09:14
    Jeavon Leopold
    0

    Awesome!

Please Sign in or register to post replies

Write your reply to:

Draft