SELECT DATE_FORMAT( o.date_added, '%m/%d/%Y' ) AS 'Date', o.order_id as 'Order Number', CONCAT_WS( "", `firstname` , `lastname` ) AS `Name`, o.email as 'Email', o.total as 'Amount', ot.value as 'Shipping'FROM oc_order oINNER JOIN oc_order_total ot ON ot.order_id = o.order_idJOIN (SELECT COUNT( email ) AS 'Orders Count', emailFROM oc_orderGROUP BY email)xxx ON xxx.email = o.emailWHERE xxx.`Orders Count` >3AND ot.code = 'shipping'AND o.order_status_id != '0'ORDER BY `o`.`email` ASC
In this query, I am trying to get list of orders placed by customers more than 3 times. Email is the field by which I can count orders. Couple of problems with this query though:
- Query takes 150 seconds to pull up 24K orders. How can I improve it to make it quicker?
- I would Like to display number of orders by a customer in select but when I do
count(xxxx.email)it is displaying just 1 row and counting all orders not just 1 customer's.
EXPLAIN SELECT and Create table
Create Table for oc_orderCREATE TABLEoc_order(order_idint(11) NOT NULL AUTO_INCREMENT,invoice_noint(11) NOT NULL,invoice_prefixvarchar(26) NOT NULL,store_idint(11) NOT NULL,store_namevarchar(64) NOT NULL,store_urlvarchar(255) NOT NULL,customer_idint(11) NOT NULL,customer_group_idint(11) NOT NULL,firstnamevarchar(32) NOT NULL,lastnamevarchar(32) NOT NULL,emailvarchar(96) NOT NULL,telephonevarchar(32) NOT NULL,faxvarchar(32) NOT NULL,custom_fieldtext NOT NULL,payment_firstnamevarchar(32) NOT NULL,payment_lastnamevarchar(32) NOT NULL,payment_companyvarchar(40) NOT NULL,payment_address_1varchar(128) NOT NULL,payment_address_2varchar(128) NOT NULL,payment_cityvarchar(128) NOT NULL,payment_postcodevarchar(10) NOT NULL,payment_countryvarchar(128) NOT NULL,payment_country_idint(11) NOT NULL,payment_zonevarchar(128) NOT NULL,payment_zone_idint(11) NOT NULL,payment_address_formattext NOT NULL,payment_custom_fieldtext NOT NULL,payment_methodvarchar(128) NOT NULL,payment_costdecimal(15,4) NOT NULL DEFAULT '0.0000',payment_codevarchar(128) NOT NULL,shipping_firstnamevarchar(32) NOT NULL,shipping_lastnamevarchar(32) NOT NULL,shipping_companyvarchar(40) NOT NULL,shipping_address_1varchar(128) NOT NULL,shipping_address_2varchar(128) NOT NULL,shipping_cityvarchar(128) NOT NULL,shipping_postcodevarchar(10) NOT NULL,shipping_countryvarchar(128) NOT NULL,shipping_country_idint(11) NOT NULL,shipping_zonevarchar(128) NOT NULL,shipping_zone_idint(11) NOT NULL,shipping_address_formattext NOT NULL,shipping_custom_fieldtext NOT NULL,shipping_methodvarchar(128) NOT NULL,shipping_costdecimal(15,4) NOT NULL DEFAULT '0.0000',shipping_codevarchar(128) NOT NULL,commenttext NOT NULL,totaldecimal(15,4) NOT NULL DEFAULT '0.0000',extra_costdecimal(15,4) NOT NULL DEFAULT '0.0000',order_status_idint(11) NOT NULL,affiliate_idint(11) NOT NULL,commissiondecimal(15,4) NOT NULL,marketing_idint(11) NOT NULL,trackingvarchar(64) NOT NULL,language_idint(11) NOT NULL,currency_idint(11) NOT NULL,currency_codevarchar(3) NOT NULL,currency_valuedecimal(15,8) NOT NULL DEFAULT '1.00000000',ipvarchar(40) NOT NULL,forwarded_ipvarchar(40) NOT NULL,user_agentvarchar(255) NOT NULL,accept_languagevarchar(255) NOT NULL,date_addeddatetime NOT NULL,date_modifieddatetime NOT NULL,payment_company_idvarchar(32) NOT NULL,payment_tax_idvarchar(32) NOT NULL,PRIMARY KEY (order_id),KEYstore_id(store_id),KEYcustomer_id(customer_id),KEYcustomer_group_id(customer_group_id),KEYpayment_country_id(payment_country_id),KEYpayment_zone_id(payment_zone_id),KEYshipping_country_id(shipping_country_id),KEYshipping_zone_id(shipping_zone_id),KEYorder_status_id(order_status_id),KEYaffiliate_id(affiliate_id),KEYmarketing_id(marketing_id),KEYlanguage_id(language_id),KEYcurrency_id(currency_id),KEYpayment_company_id(payment_company_id),KEYpayment_tax_id(payment_tax_id),KEYcustomer_id_2(customer_id),KEYcustomer_id_3(customer_id),KEYsuperdruid_order(date_added,order_status_id,order_id)) ENGINE=InnoDB AUTO_INCREMENT=123456 DEFAULT CHARSET=utf8
Create Table for oc_order_totalCREATE TABLE
oc_order_total(order_total_idint(10) NOT NULL AUTO_INCREMENT,order_idint(11) NOT NULL,codevarchar(32) NOT NULL,titlevarchar(255) NOT NULL,valuedecimal(15,4) NOT NULL DEFAULT '0.0000',sort_orderint(3) NOT NULL,textvarchar(255) NOT NULL,PRIMARY KEY (order_total_id),KEYorder_id(order_id)) ENGINE=InnoDB AUTO_INCREMENT=123456 DEFAULT CHARSET=utf8![EXPLAIN SELECT]()

