Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 15, 2012 @ 11:41
    Bo Damgaard Mortensen
    0

    Performance when working with dates

    Hi all,

    I'm experiencing a small performance issue when working with dates in a Where clause. The load time for the macro peaks when accessing the startDate and endDate properties on the node.

    My code:

    @Get random contest *@
    Random rnd new Random();
    var contest contests.Children.OrderBy(rnd.Next()).Take(1).Where("startDate <= DateTime.Now && endDate >= DateTime.Now").Single();

    Anyone else experienced this? :-)

    In general, it seems/feels like performance is taking a small hit when you're using the Where clause, i.e. in a Navigation macro: .Where("visible") , so if you have 3 menus on your site which get rendered with .Where("showInLeftMenu") etc, it makes for quite some loadtimes.

    Have I misunderstodd something here?

    Thanks in advance.

    - Bo

  • Bert Loedeman 10 posts 30 karma points
    Nov 15, 2012 @ 12:17
    Bert Loedeman
    0

    Hi Bo,

    I have no problems at all using (some really complex) Where statements myself. Did you try only using the Where statement, without first ordering and the rest? I can imagine the problem is related to the stacking of operations, although I do not know how the Where method actually works. I would suggest performing the Where statement first, and after that order the result.

    Cheers, Bert

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 15, 2012 @ 12:20
    Dirk De Grave
    0

    Bo,

    I usually take a step in between to avoid having to work with dates in where clauses

    var contest = contests.Children
    .Select(node => new {
    Node = node,
    StartDate = DateTime.Parse(node.GetPropertyValue("startDate")),
    EndDate = DateTime.Parse(node.GetPropertyValue("endDate"))
    })
    .Where(item => item.StartDate <= DateTime.Now && DateTime.Now <= item.EndDate)
    .OrderBy(rnd.Next())
    .Take(1);

     

    Cheers,

    /Dirk

  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 15, 2012 @ 13:05
    Mads Krohn
    0

    Hi Bo

    There are a few things missing from your problem description. I pinged you on Twitter, so we now know you are running 4.7.2 :)
    Next thing, how many content items are we talking about? I played around with just 5 and haven't seen any performance issues ... :P

    I was a bit confused about the contruction of your query.
    I think I got it though, so I rewrote it to hopefully make it a bit more clear. Could you try it and see if the performance issues persists?

    Random rnd = new Random();
    var contest = contests.Children.Where("startDate <= DateTime.Now && endDate >= DateTime.Now").OrderBy(rnd.Next()).FirstOrDefault();
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 15, 2012 @ 13:14
    Jeroen Breuer
    0

    I've had a similar problem in this topic: http://our.umbraco.org/forum/developers/razor/28479-Razor-menu-performance-(v4). You might not like the solution, but that fixed it for me :-).

    Jeroen

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 15, 2012 @ 14:28
    Bo Damgaard Mortensen
    0

    Hi guys,

    Thanks for your replies :-)

    I have tried to tinker around with the code you wrote, but either way it's still hangin' :-/ The load time on startDate property is up to: 0,886731 when having a look at the page with umbDebugShowTrace, and that's on localhost.

    Got a few questions about what "contests" is. It's simply a DynamicNodeList which I get like this:

    var contests = Model.AncestorOrSelf().Contests.Single();

    @Jeroen, yeah.. I found your post in a search, but was hoping it had been fixed since :-/

    Thanks again!

    - Bo

  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 15, 2012 @ 14:38
    Mads Krohn
    0

    There does indeed seem to be quite a performance penalty for using dynamic .Where, at least in 4.7.2.

    Would it be possible for you to not run the query dynamic ?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 15, 2012 @ 14:39
    Jeroen Breuer
    0

    It should be fixed in 4.10 if you use the new Views. Shannon rewrote the code and if you use the strongly typed examples it should be faster.

    Jeroen

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 15, 2012 @ 14:42
    Bo Damgaard Mortensen
    0

    @Mads: yea, I'm working on a (nasty) hac... err.. solution to use Node instead :-)

    @Jeroen: Oh, that's good news! But upgrading is not an option at the current state of the site I'm afraid :-/

    Anyway, I'll post the code here if/when I come up with something that works.

    Thanks again!

    - Bo

  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 15, 2012 @ 14:48
    Mads Krohn
    0
    Random rnd = new Random();
    var contests = Current.AncestorOrSelf("Contests").GetChildrenAsList.Items;
    var contest = contests.Where(x => DateTime.Parse(x.GetPropertyValue("startDate")) <= DateTime.Now && DateTime.Parse(x.GetPropertyValue("endDate")) >= DateTime.Now).OrderBy(x => rnd.Next()).FirstOrDefault();

    Something like the above should do the trick ?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 15, 2012 @ 15:49
    Bo Damgaard Mortensen
    0

    Thanks Mads! I'll try it out :-)

    I just had a chance to try out my original code on the live site, Strange thing is that there's absolutely no performance hickups on the live site with my originally posted code. Doesn't make any sense to me, but as long as it works I guess everybody is happy ;-)

    - Bo

Please Sign in or register to post replies

Write your reply to:

Draft