Zen Cart Logo
Forums / General Questions / Delete customer carts older than date X (SQL help?)

Delete customer carts older than date X (SQL help?)

Locked

Views: 4,375

Results 1 to 11 of 11
This thread is locked. New replies are disabled.
26 Jan 2009, 11:50 PM
#1
bodyjewelrystores avatar

bodyjewelrystores

Zen Follower

Join Date:
Jun 2006
Posts:
123
Plugin Contributions:
0

Delete customer carts older than date X (SQL help?)

Hi all,

Our web store has been open for almost a year, and over time the database has accumulated a lot of customer carts that were never checked out. I seems like this is a major factor slowing down our website (especially when it comes to adding items to cart, and checking out).

I'm looking for a way to clean up old, unused carts automatically, perhaps with an SQL query.

I'm not very well-versed in SQL... but it seems that these old cart items are stored in tables "customers_basket" and "customers_basket_attributes". But only the former one has a date field, and I'm not seeing a straightforward way of correlating to the correct rows in the second table.

So... does anyone have an idea? How can I automatically purge every non-checked-out cart that's older than date X?

(we're already using the "Recover Cart Sales" add-on, which allows me to review and delete old carts, but only one-by-one - and when it comes to our web server and the state of our database, this is impossibly slow. So I'm looking for a solution that just deletes everything older than a certain date).

Thanks in advance.

27 Jan 2009, 12:08 AM
#2
data_digger avatar

data_digger

Zen Follower

Join Date:
Jan 2009
Posts:
222
Plugin Contributions:
2

Re: Delete customer carts older than date X (SQL help?)

Backup... I already drunk beer today... Beer + writing delete statements can give funny results ;)

DELETE FROM customers_basket
WHERE customers_basket_date_added < 20081231

Where 20081231 stands for 31th December of 2008 ( it will remove all carts created before that date).

After executing above statement, execute

DELETE FROM customers_basket_attributes cba
WHERE cba.customers_id NOT IN (SELECT cb.customers_id FROM customers_basket)
27 Jan 2009, 12:29 AM
#3
bodyjewelrystores avatar

bodyjewelrystores

Zen Follower

Join Date:
Jun 2006
Posts:
123
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

THANK YOU!!! Seems to work perfectly... see, no need to sully the good name of beer ;)

(Well... the second query did give me a syntax error... but I changed it as below and it worked):

DELETE FROM customers_basket_attributes 
WHERE customers_basket_attributes.customers_id NOT IN (SELECT customers_basket.customers_id FROM customers_basket);

Thanks again.... you've just saved me hours upon hours of work, so I'm gonna go get a cold one myself.
:cheers:

28 Jul 2009, 2:58 AM
#4
g33k_chik avatar

g33k_chik

New Zenner

Join Date:
Jun 2007
Location:
TN
Posts:
83
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

:( It didn't work for me.

I got the following error:

SQL query:
DELETE FROM customers_basket WHERE customers_basket_date_added <20081231
**MySQL said: **[IMG]https://p3nlmysqladm001.secureserver.net/nld41/7/themes/original/img/b_help.png[/IMG]
#1146 - Table 'swansoninc_com.customers_basket' doesn't exist

Can someone help me? I have a ton of carts to get rid of and my site is so slow that I am losing sales because people are going to competitors instead.

I am using 1.3.7. www. swansoninc. com

28 Jul 2009, 4:52 AM
#5
g33k_chik avatar

g33k_chik

New Zenner

Join Date:
Jun 2007
Location:
TN
Posts:
83
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

Well, tried it again and it worked. Not the second code, but the first one....so yay! No more old carts. :clap:

28 Jul 2009, 1:01 PM
#6
bodyjewelrystores avatar

bodyjewelrystores

Zen Follower

Join Date:
Jun 2006
Posts:
123
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

For best results (and proper db maintenance), you should probably try the 2nd query too - otherwise, the old carts' item attributes will still remain as "orphaned" data, taking up database space for nothing and very possibly slowing it down...

Of course if your store does not have item attributes, feel free to ignore this :)

But otherwise, this should work:

DELETE FROM customers_basket_attributes WHERE customers_basket_attributes.customers_id NOT IN (SELECT customers_basket.customers_id FROM customers_basket);

28 Jul 2009, 4:27 PM
#7
g33k_chik avatar

g33k_chik

New Zenner

Join Date:
Jun 2007
Location:
TN
Posts:
83
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

Hmmm....

I got this when I ran it:

1146 Table 'swansoninc_com.customers_basket' doesn't exist
in:
[DELETE FROM zen_customers_basket_attributes WHERE customers_basket_attributes.customers_id NOT IN (SELECT customers_basket.customers_id FROM customers_basket);]

Oh, well. I got rid of all those pesky old carts and my site didn't crash. I'm happy.:bigups:

28 Jul 2009, 4:55 PM
#8
bodyjewelrystores avatar

bodyjewelrystores

Zen Follower

Join Date:
Jun 2006
Posts:
123
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

Yeah I'm not sure why that weird prefix keeps adding itself to your table names when the query is processed... especially if your installation doesn't use table prefixes (which seems to be the case, otherwise that table name would've existed)

Wonder if you're running this code from Zen's SQL patcher, or from phpMyAdmin... if one doesn't work you could try the other... No big deal though, as long as the site didn't crash, I guess. :)

28 Jul 2009, 5:04 PM
#9
g33k_chik avatar

g33k_chik

New Zenner

Join Date:
Jun 2007
Location:
TN
Posts:
83
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

I tried it from both. Got the same error message in both places. Oh well....I'll just blame my host for changing the prefix. haha :laugh:

3 Feb 2010, 11:09 AM
#10
dogtags avatar

dogtags

Totally Zenned

Join Date:
Nov 2003
Posts:
876
Plugin Contributions:
1

Re: Delete customer carts older than date X (SQL help?)

After you cleared lots of old carts, did you notice a performance boost?

Also, is there a way to combine the two SQLs above so they cover both concerns mentioned?

Thanks :smile:

1 Mar 2010, 2:36 PM
#11
nagelkruid avatar

nagelkruid

Zen Follower

Join Date:
Nov 2006
Posts:
284
Plugin Contributions:
0

Re: Delete customer carts older than date X (SQL help?)

not sure if it boosted performance, but it may have gotten a tad quicker yes.

for the second query i don't think it will go in the patch tool as it uses a nested function, which is something that tool may not support.

In SQLadmin you need to add the prefix consistent if you build the query for the second statement:

DELETE FROM <prefix>customers_basket_attributes 
WHERE <prefix>customers_basket_attributes.customers_id NOT IN (SELECT <prefix>customers_basket.customers_id FROM <prefix>customers_basket);

that one worked for me