CartMetrix - Do you know yours?

10/23/2006

MySQL Database Performance - ZenCart Sales Report

One of my client's ZenCart installs was loading the server down so much to generate sales reports with the Sales Report v1 contribution that the public side was noticeably less responsive for the couple of minutes it took to build the report. The site has been running for over three years and contains 9k orders, so there is a lot of data to weed through even for a weekly report. A quick look at the slow query log listed the following query as the culprit:

SELECT count( op.products_id ) AS attr_cnt, o.orders_id, opa.orders_products_id, opa.products_options, opa.products_options_values, opa.options_values_price, opa.price_prefix
FROM orders_products_attributes opa, orders o, orders_products op
WHERE o.orders_id = opa.orders_id
AND op.orders_products_id = opa.orders_products_id
AND o.date_purchased >= '2006-10-18T00:00:00'
AND o.date_purchased < '2006-10-19T00:00:00'
AND op.products_id =179
GROUP BY products_options_values
ORDER BY orders_products_id;

Running this query through EXPLAIN shows 20978 rows examined. In this case, thats 20978 rows per day included in the sales report.

The main problem with this query is the ORDER BY clause which cannot utilize an exisiting index. Adding the following index:

ALTER TABLE 'orders_products_attributes' ADD INDEX ( 'orders_products_id' )

limits the rows examined for each query to 68 (results).

This simple change allows the sales report to load in seconds as opposed to minutes with no noticeable lag on the public side.

The create table SQL for the current version of ZenCart (v1.3.5) contains the following index in orders_products_attributes:

...
KEY idx_orders_id_prod_id_zen (orders_id,orders_products_id)
...

This index is used in the query in question. In my tests it was only slightly slower than the single column index above. EXPLAIN showed about 270 rows examined with the two column index on my dataset while the single column index only examined 68 rows.

Popularity: 21%

6/15/2006

Directory Shortcuts Using Bash Functions

Modify this snippet to match the directory shortcuts you need on your system and add into your .bashrc file:

www () {
if [ $1 == "domain1" ]; then
        cd /www/vhosts/domain1.com/htdocs
elif [ $1 == "domain2" ]; then
        cd /www/vhosts/domain2.com/htdocs              
else
        cd /www/vhosts/$1 ;
fi
echo `pwd`;
}

To use:

www domain1

Popularity: 16%

1/5/2006

Convert Mac Line Endings

I wasted an hour last week messing with a CSV file I was massaging data in for import into a large SQL database only to find out the reason the import kept dying was because of the Mac line endings. There are many shell script, Perl scripts and shell one liners to convert line endings but I found a program called flip available for OSX, WinXP and Linux that can convert between all three.

Usage: flip [-t|-u|-d|-m] filename[s]
Converts ASCII files between Unix, MS-DOS/Windows, or Macintosh newline formats

Options:
-u = convert file(s) to Unix newline format (newline)
-d = convert file(s) to MS-DOS/Windows newline format (linefeed + newline)
-m = convert file(s) to Macintosh newline format (linefeed)
-t = display current file type, no file modifications

They are linked here locally as well:

Popularity: 40%


damonparker.org is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

copyright © 2002-2008 damonparker.org. all rights reserved.

Close
E-mail It