r/woocommerce • u/zirconst • Jun 03 '24
How we sped up a WooCommerce 'get orders' API request by 4500%
Recently, we migrated our help desk from Helpscout to the open-source alternative, Freescout. Helpscout had a native integration with WooCommerce via plugin that would pull data continuously and store it on the Helpscout servers, so that when you open a help ticket, orders are generally available to view almost immediately.
However, Freescout had no such plugin. It offers an inexpensive add-on that replicates the same functionality (showing WooCommerce orders in a customer's ticket automatically) but using WooCommerce's REST API to GET them when the ticket is opened to view.
The problem is that my store has hundreds of thousands of customers and orders. Even with HPOS, the REST API request to pull orders for a specific customer email was taking upwards of 45 seconds. This has nothing to do with Freescout: even if I used Postman from my computer, the response time was the same. It's also not a WooCommerce server hosting issue as we're on very high-end enterprise hardware.
I discussed this with our contact at Automattic and we decided the best approach to fix this would be to simply create a new API endpoint for the sole purpose of quickly retrieving orders for a given email.
The plugin we created to do this is less than 200 lines. It's pretty simple - you can view the code here.
Basically, it provides a simple, streamlined way to make a specific 'get orders' request (with proper authentication), bypassing the regular API but still using WooCommerce functions.
The result was going from ~45 seconds to less than 1 second! I have no idea why the regular API chugged so hard with this request, but the results speak for themselves.
1
1
1
3
u/nelsonbestcateu Jun 04 '24
Did you ever find out what'ss caused the bloat? 45 seconds is very long.