5

this may be somewhat of a noob question as I just started administering an ExpressionEngine site that uses CartThrob. My client complained of the site being slow during the shopping / checkout process so I enabled SQL profiling in EE to look into it.

I noticed something strange, which is that CartThrob is making many queries during each page load, far more than I would expect. Basically the page template uses

exp:cartthrob:add_to_cart_form

six times to show six different products. Then uses

exp:cartthrob:item_options

within each one to pull some various information about the product. The strange thing is that a single SQL SELECT and another SQL UPDATE query seem to be running over and over again on the same page (each of them runs over 50 times on one page load).

SELECT `cart`
FROM (`exp_cartthrob_cart`)
WHERE `id` = '12345'
LIMIT 1;

UPDATE `exp_cartthrob_cart`
SET `cart` = 'a-bunch-of-encoded-gibberish',
    `timestamp` = 1386624719,
    `url` = 'http://example.com/product-details'
WHERE `id` = '12345';

Why would cartthrob be running these same queries over and over again, and how can I optimize this code to prevent it?

  • I've noticed that same pair of queries running many times on one of our sites too. Did anyone figure out a way to stop CartThrob from doing those queries so many times? – Benjamin Kohl Dec 1 '16 at 21:58
3

Sounds like a poorly coded template... First, you should aim to refactor it using the multi_add_to_cart_form instead of the simple add_to_cart_form:

http://cartthrob.com/docs/tags_detail/multi_add_to_cart_form/index.html

This will give you a single form for all 6 products so should reduce CT's load by 6X. You should then look into other optimisations and caching strategies for further gains (in particular, try to minimise or eliminate usage of EE's embed tag). It's likely that if the previous developer missed the multi_add_to_cart_form they probably missed a lot of other things...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.