We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36467
    • 73 Posts
    Below is my code

    $resources = $modx->getObject("modResource",array("id" => $parentid, 'published' => 1, 'deleted' => 0));
    $nameid  = array();
    $details = array();
    // Find all childs from forums -> event calendar
    if($resources){
      $children = $resources->getMany('Children', array("published" => '1', "deleted" => '0'));
      if(count($children)){
          $index = 0;
    	  foreach ($children as $child) {
    	   $id  = $child->get('id');
    	   $longtitle  = $child->get('longtitle');
    	   $startDate = $child->getTVValue('startDate');
    	   $endDate = $child->getTVValue('endDate');
    	   $eventlink = $child->getTVValue('eventlink');
    	   $country = $child->getTVValue('country');
    	   
    	   $nameid[$index]['id'] = $id;
    	   $nameid[$index]['startDate'] = $startDate;
    	   $nameid[$index]['endDate'] = $endDate;
    	   $nameid[$index]['longtitle'] = $longtitle;
    	   $nameid[$index]['eventlink'] = $eventlink;
    	   $nameid[$index]['country'] = $country;
    	   $index++;
    	  }
      }
    }
    
    // sort list by tv start date
    usort($nameid, function($a, $b) {
        return strtotime($a['startDate']) - strtotime($b['startDate']);
    });
    
    // get last 5 items (which will be recent 5 events)
    $final = array();
    for($i = count($nameid); $i >= count($nameid) - 5; $i--){
     if(count($nameid[$i]))
        $final[$i] = $nameid[$i];
    }
    
    $final = array_reverse($final);
    
    foreach($final as $key=>$value){
        echo $modx->getChunk('ditto_upcomingevent-tab2017', $value);
    }
    


    I have above code which finds 5 nearest upcoming events (from 109 childs) based on TV 'startdate', I am using https://modx.com/extras/package/debugparser to check performance issue on the pages and found that above code fires 500+ queries which take about 12+ seconds, we have this call 2 times on page so our page takes around 30-55 seconds to load.

    Is there any issue with the above code? Does it really need to fire this much queries?
      • 3749
      • 24,544 Posts
      I don't think getMany() is your problem. I think it's the many calls to getTVValue() which is fairly slow.

      TVs are not a very good place to store data, especially if you need to use them in a search. It might speed things up considerably to combine all the TVs into one MIGX TV, which might also be more convenient for entering the data.

      Another option that might be faster (and lets you keep your existing TVs) would be a plugin connected to OnDocFormSave which would combine all the TVs into an associative array and save it in the resource's 'properties' field. Then, since you already have the child resources, you can replace all this:

       
             $id  = $child->get('id');
             $longtitle  = $child->get('longtitle');
             $startDate = $child->getTVValue('startDate');
             $endDate = $child->getTVValue('endDate');
             $eventlink = $child->getTVValue('eventlink');
             $country = $child->getTVValue('country');
              
             $nameid[$index]['id'] = $id;
             $nameid[$index]['startDate'] = $startDate;
             $nameid[$index]['endDate'] = $endDate;
             $nameid[$index]['longtitle'] = $longtitle;
             $nameid[$index]['eventlink'] = $eventlink;
             $nameid[$index]['country'] = $country;
             $index++;
      


      with this:

      $fields = $child->get('properties');
      $fields['id'] = $child->get('id');
      $nameid[$index] = $fields;
      


      BTW, you're only getting one resource at the beginning, so the variable name probably shouldn't be $resources.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36467
        • 73 Posts
        Bob,
        Sorry for delayed response. I was trying to find out more about property sets, there is very less info available about property sets. I played a little bit with property sets on my localhost but no success, maybe I will give it a fresh try again.
        I noticed that there is no "Properties" tab for resources, can we have a property sets for resources ? as far as i read about property sets, it can be added to chunks, snippets, templates (which are reusable items) etc but does property sets are applicable to resources also (which is not reusable).

        I will give it a try in next 1-2 days, if i cant then i might end up using 1 more tv which will store details of all other tvs in one place. this will surely reduce 500 calls to 200 i think (100 for resources and 100 for reading tv).
        • Property sets are not the same as storing information in the resource "properties" field, though the name confusion is very understandable.

          Bob's suggestion of writing to the properties field is quite elegant. An alternative approach would be to craft a special query that joins the different TVs so that you can get all the information in a single query. Some relevant threads related to that:

          - https://forums.modx.com/thread/31873/querying-for-a-list-of-resources-by-the-value-of-a-template-variable
          - https://forums.modx.com/thread/98520/how-would-you-get-resources-with-xpdo-based-on-the-value-of-a-tv#dis-post-532855

          You'd need to repeat the join, and define the relation for it back to the resource, for each unique TV you want. If you do that, you can probably add the ordering and limit into your query as well, to save on the processing in the PHP side. Then your one query would only return the rows you actually want..
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 36467
            • 73 Posts
            For the time being, i switched back to [[!getResources]] snippet call, it reduced the number of queries drastically.
              • 3749
              • 24,544 Posts
              getResources does it's own join on the TVs, though it's still relatively slow compared to a custom solution. That said, if the page and the getResources tag can be cached, it may work fine.

              You might get a performance gain with pdoResources (in the pdoTools package).
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 36467
                • 73 Posts
                Yeh it improved some speed but I needed more speed, so i came up with a smart solution. We have a gallery and recent posts widget which were querying database everytime page loads, which was not required and i though to get rid of that. So what I did is, I replaced all 'getResources' calls with static chunks, but whenever we post a new article, update the article or delete article this chunk should be updated with new html code having details about new articles.

                So I created a plugin which looks for onDocFormSave event and if the resource parent is specific like 122,233 then it executes 'getResources' snippet, generates new html and updates 2-3 chunks code. This idea improved site speed drastically because now we are printing static html code, querying database only when it's really needed and not everytime page loads.
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. Thanks for reporting back. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting