2015-04-21 2 views
-1

Мой запрос работает очень долго. Я попытался улучшить производительность запросов, добавив индекс на website_id в таблицу hh_hits.Как улучшить этот сложный запрос?

Запрос:

SELECT 

    w.state, xx.pretty_name, xx.member_id, 
    SUM(ce.total_charge) as total_charge, 
    SUM(ce.shipping_cost) as shipping_cost, 
    SUM(ce.product_price * product_count) as product_price, 
    SUM(ce.tax) as tax, 
    SUM(ce.service_charge) as service_charge, 
    COUNT(distinct ce.order_id) as order_count, 
    SUM(ce.product_count) as product_count, 
    SUM((select sum(addon_price*addon_count) 
      from wow.cart_entry_addons_archive cean 
       where cean.cart_entry_id=ce.cart_entry_id 
       and cean.addon_type='xx' 
       group by ce.cart_entry_id)) as giftwrap_total, 
    sum((select sum(addon_price*addon_count) 
       from wow.cart_entry_addons_archive cean2 
       where cean2.cart_entry_id=ce.cart_entry_id 
       and cean2.addon_type='xx' 
       group by ce.cart_entry_id)) as addon_total, 
    (select sum(number) as hits 
       from wow.hh_hits thts 
       where thts.website_id=xx.website_id 
       and thts.start_date >= 'xxx' 
       and thts.start_date <= 'xx') as visits 

FROM 

    wow.carts_archive c, 
    wow.cart_entries_archive ce, 
    eoe.websites xx 

WHERE 

    ce.order_date >= 'xx' and 
    ce.order_date <= 'xx' and 
    ce.website_id=xx.website_id and 
    lower(ce.status) != 'deleted' and 
    ce.order_status != 'cancelled' and 
    ce.cart_id = c.cart_id and 
    (c.cc_number <> '343334' or c.cc_number is null) 
GROUP BY ce.website_id 
ORDER BY ce.website_id; 

Объяснить план:

+----+--------------------+-------+--------+------------------+----------+---------+---------------------------+-------+----------------------------------------------+ 
| id | select_type  | table | type | possible_keys | key  | key_len | ref      | rows | Extra          | 
+----+--------------------+-------+--------+------------------+----------+---------+---------------------------+-------+----------------------------------------------+ 
| 1 | PRIMARY   | ce | range | id1_nn,idx_726 | idx_1049 | 9  | NULL      | 33 | Using where; Using filesort     | 
| 1 | PRIMARY   | w  | ref | idx_1055   | idx_1055 | 5  | wow.ce.website_id   |  1 | Using where         | 
| 1 | PRIMARY   | c  | eq_ref | PRIMARY   | PRIMARY | 4  | eoe.ce.cart_id   |  1 | Using where         | 
| 4 | DEPENDENT SUBQUERY | thts | ALL | hh_n1   | NULL  | NULL | NULL      | 24493 | Using where         | 
| 3 | DEPENDENT SUBQUERY | cean2 | ref | idx_1383   | idx_1383 | 4  | wow.ce.cart_entry_id  |  1 | Using where; Using temporary; Using filesort | 
| 2 | DEPENDENT SUBQUERY | cean | ref | idx_1383   | idx_1383 | 4  | wow.ce.cart_entry_id  |  1 | Using where; Using temporary; Using filesort | 

Query объяснить план кажется hh_hits таблица не использует индексы.

+0

Какой у вас двигатель db? Mysql или Sql? –

+0

Двигатель Mysql и db является innodb. – Chungyar

+1

@ Lucky: обычно вопрос такого типа будет помечен как оба (поскольку MySQL SQL также является SQL). Однако тег 'sql' запрашивает, что он используется только для запросов ANSI SQL, поэтому ваше редактирование было правильным. – halfer

ответ

0

На hh_hits, добавьте соединение индекс:

INDEX(website_id, start_date) 

Для дальнейшего обсуждения, просьба представить SHOW CREATE TABLE для каждой из таблиц.

Если мое предложение не помогает, тогда мы должны поговорить о создании сводной таблицы, которая содержит промежуточные итоги для каждой пары дат сайта. Каждый вечер вы увеличиваете таблицу, а затем запускаете зависимый подзапрос.

Смежные вопросы