A lot of import scripts I’ve worked with previously are very slow. It’s true to say that Magento is a bit of a memory and process hog, but there are a few things that we can do to speed up import processes. It’s worth noting that these don’t apply if you’re doing direct database updating; they only apply if you use the API/Dataflow.
It is very important to have a good specification of server to run Magento in the first place, and imports are likely to push the server to its limits. Instead of a transactional lifestyle, the server is going to be hit repeatedly until the data you want to import has been added.
I wouldn’t recommend doing any sort of import onto a live box unless you absolutely must. Simply because it will impact on the performance of your main site for a sustained period. You can of course run imports as scheduled tasks out of normal operating hours, or importing into a staging version of your site and then merge across at a time of convenience – which can be quite tricky.
There are a few things you can do to improve the speed of imports, and to make sure that your imports don’t fail.
Memory limit
Set the the memory limit for your script so that it has enough to get the job done, and not too much that it will impact on your site. Simply add this at the top of your script, setting the size to be something appropriate:
Code:
ini_set('memory_limit', '1024M');
This all depends on how much memory is in your box, how many products/orders etc you’re working on with your script.
Magento Store
Set the current store for Magento before beginning your import. In this case, we only have one store ID in our Magento setup, so we can just set it to the admin store id.
Code:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Indexing
Magento has a very thorough caching and indexing system. Every time you add or edit a product, or amend an attribute, the caching system updates and also re-indexes the data. This improves front-end load times and accuracy of search data as well as many other things. This can get in the way when we’re doing an import though – Magento would have to do this every time a product is added/updated and we might be adding or updating thousands of records during an import. It’s safe to say that this is probably the case in any running store. You’ve probably already noticed that Magento has the ability to set these to “manual”.
We’re going to leverage this so that our script runs quicker. The theory is that we can suspend indexing, complete our process, and then re-enable & re-run indexing. We can therefore end up with two functions – unSetIndex() which we can call at the beginning of our process, and setIndex() that we can call at the end to change things back. Obviously, you only need to call these in your script as and when you want, i.e. if you don’t have indexes set on staging, don’t call them.
PHP Code:
private function unSetIndex()
{
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
return true;
}
private function setIndex()
{
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
return true;
}
As you can see in the setIndex() function, we make a call to “reindexAll”. You could omit this is you didn’t actually want to trigger the re-index and had it scheduled. It would probably be a bit mean to make the next person to save a product wait while it re-indexes everything.
Time your scripts
This tip doesn’t directly speed up your script, but it’s good to benchmark your script as you put it together and do test runs. This will also give you some insight into where you’re at when comparing your development, staging and live environments.
I’ve had this in my arsenal for quite some time, so dug out a reference to it – http://www.developerfusion.com/code/...n-time-in-php/
PHP Code:
<!-- put this at the top of the page -->
<!--?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?-->
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<!--?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
?-->
Hopefully, you’ve got a few tips to make your import much quicker now! Any problems – leave them in the comments below.
Source: branded3.com
View more threads in the same category:
Bookmarks