Magento: Increasing import speed

Increasing SpeedI’ve written a couple of Magento posts on this blog in which I’ve walked through adding content from a script, such as CMS pages in my first post – Magento: First Class.

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:

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.

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.

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/2058/determine-execution-time-in-php/

Hopefully, you’ve got a few tips to make your import much quicker now! Any problems – leave them in the comments below.

By Douglas Radburn. at 3:22PM on Thursday, 08 Aug 2013

Doug is our Senior Open Source Web Developer since bringing his knowledge and skills to Branded3 in 2009. A founding developer of our Twitition and Competwition platforms, Doug has also been lead in our Open Source Projects on Magento ecommerce solutions and Wordpress CMS platforms. Follow Douglas Radburn on Twitter.

comments

Comments are closed.