We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44234
    • 219 Posts
    Hi,

    I am trying to access a number of TV's using xPDO in an external PHP file. The script is outputing everything correctly, however it is taking around 18sec to load the output.

    I'm sure this is not correct, but I am quite new to using xPDO, so could anyone advise me on how to bring the load time down and make the script more efficient?

    <?php
    
    // add package so xpdo can be used:
    require_once '/home/foo/foo/foo/config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error','error.modError');
    
    $documents = $modx->getCollection('modResource',array('published' => 1, 'template' => '2'));
    foreach ($documents as $document) {
    	
    	$isbn = $document->getTVValue('bk-isbn');
    	$isbn = str_replace( ' ', '', $isbn);
    	$desc = $document->get('longtitle');
    	$ebkPrice = $document->getTVValue('ebk-price');
    	$ebkPriceUnder = $document->getTVValue('ebk-price-under');
    	$bndlPrice = $document->getTVValue('bndl-price');
    	$file = $document->getTVValue('ebk-file');
    	$rdate = $document->getTVValue('bk-reprintDate');
        $pdate = $document->getTVValue('bk-pubDate');
    	
    	$ebkoutput = $Products[] = "ebk$isbn,$desc - ebook,GBP=$ebkPrice,/home/foo/foo/$file,0,1440";
    	$bndloutput = $Products[] = "bndl$isbn,$desc - Bundle,GBP=$bndlPrice,/home/foo/foo/$file,0,1440";
    	}
    }
    return $ebkoutput;
    return $bndloutput;
    
    ?>


    Many Thanks

    This question has been answered by markh. See the first response.

      Find me on Twitter, GitHub or Google+
      • 22303 MODX Staff
      • 10,725 Posts
      First, how many Resources are you selecting and iterating over?
        • 44234
        • 219 Posts
        Sorry should have said, there are 562 resources.
          Find me on Twitter, GitHub or Google+
          • 44234
          • 219 Posts
          Could anyone with some experience of xPDO let me know if there is something obviously wrong with this script, or if the 18sec response time is expected behavior?

          Thanks
            Find me on Twitter, GitHub or Google+
          • discuss.answer
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            Do you realize you are making 3935 SQL queries with that bit of code? 18 seconds sounds like you've got quite a beefy server running smiley
            1 times select published resources with template 2
            562 x 7 = 3934 times get TV value for TV with a specific name for this resource

            You'll definitely want to optimize that with a more advanced query that already fetches the TVs in the single first query.

            Something along the lines of..
            $c = $modx->newQuery('modResource');
            $c->where(array(
              'published' => 1,
              'template' => 2,
            ));
            
            $c->innerJoin('modTemplateVarResource', 'TVISBN', 'modResource.id = TVISBN.contentid AND TVISBN.tmplvarid = 5'); // where 5 is the ID of the ISBN TV.
            $c->innerJoin('modTemplateVarResource', 'TVPRICE', 'modResource.id = TVPRICE.contentid AND TVPRICE.tmplvarid = 6'); // where 5 is the ID of the Price TV.
            // .. repeat
            
            $c->select(array(
              'modResource.*', 
              'isbn' => 'TVISBN.value',
              'price' => 'TVPRICE.value',
              //....
            ));
            
            foreach ($modx->getIterator('modResource', $c) as $document) {
              $values = $document->toArray('', false, true);
              var_dump($values);
            }

            Untested and probably broken, but you get the idea.

            http://rtfm.modx.com/display/xPDO20/xPDOQuery
            http://rtfm.modx.com/display/XPDO20/xPDOQuery.innerJoin
            http://rtfm.modx.com/display/xPDO20/toArray
              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.
              • 44234
              • 219 Posts
              Do you realize you are making 3935 SQL queries with that bit of code? 18 seconds sounds like you've got quite a beefy server running

              Its definitely beefier than my knowledge of PHP/SQL haha!

              Thanks for the reply, makes much more sence and gives me something to run with.
                Find me on Twitter, GitHub or Google+
                • 44234
                • 219 Posts
                Thanks again Mark, its working brilliantly now!
                  Find me on Twitter, GitHub or Google+
                  • 18373 ☆ A M B ☆
                  • 3,141 Posts
                  Great to hear! If you made any changes to the code I posted, it would be great if you could post your solution so others who stumble across this topic later can see how you solved it.
                    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.
                    • 44234
                    • 219 Posts
                    Yeah no problem, will do later
                      Find me on Twitter, GitHub or Google+