优化MySql查询:只查询说明额外的“使用where”

时间:2014-02-05 19:31:17

标签: mysql sql

我有以下架构的表格:

+----------------------+--------------+--------------+-----+---------+-----------+
|        Field         |     Type     |         Null | Key | Default |     Extra |
+----------------------+--------------+--------------+-----+---------+-----------+
| request_id           | bigint(20)   | NO           | PRI |         |           |
| marketplace_id       | int(11)      | NO           | PRI |         |           |
| feed_attribute_name  | varchar(256) | NO           | PRI |         |           |
| full_update_count    | int(11)      | NO           |     |         |           |
| partial_update_count | int(11)      | NO           |     |         |           |
| ptd                  | varchar(256) | NO           | PRI |         |           |
| processed_date       | datetime     | NO           | PRI |         |           |
+----------------------+--------------+--------------+-----+---------+-----------+

我正在这样查询:

EXPLAIN SELECT SUM(full_update_count) as total FROM 
x.attribute_usage_information WHERE marketplace_id=6 
AND ptd='Y' AND processed_date>2013-12-31 AND 
feed_attribute_name='abc'

查询计划是:

id  select_type table   type    possible_keys   key key_len    ref     rows         Extra
1   SIMPLE      X       ALL                                            1913668816   Using where

我是查询优化的新手,所以我的推论可能是错误的。 我很惊讶它没有使用索引,这可能是其缓慢执行的原因(大约一个小时)。表格大小为10^10。是否可以重写此查询以便它使用索引,因为where子句是表的主键集的一部分?

编辑:显示索引结果

+---------------------------+------------+------------+--------------+----------------+------
|Table                      | Non_unique |  Key_name  | Seq_in_index | Column_name    | Collation   Cardinality Sub_part    Packed  Null    Index_type  Comment
|attribute_usage_information |   0        |   PRIMARY  | 1            | request_id     | A  2901956             BTREE   
|attribute_usage_information |   0        |   PRIMARY  | 2            | marketplace_id | A  2901956             BTREE   
|attribute_usage_information |   0        |   PRIMARY  | 3            |                | feed_attribute_name    A   273613033               BTREE   
|attribute_usage_information |   0        |   PRIMARY  | 4            | ptd            | A  1915291236              BTREE   
|attribute_usage_information |   0        |   PRIMARY  | 5            | processed_date | A  1915291236              BTREE   

编辑2:显示结果

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'data_usage_rw'@'%' IDENTIFIED BY PASSWORD *** WITH GRANT OPTION

2 个答案:

答案 0 :(得分:0)

使用复合WHERE

覆盖index(marketplace_id,ptd,processed_date,feed_attribute_name)条件
ALTER TABLE `tablename` ADD INDEX (marketplace_id,ptd,processed_date,feed_attribute_name)

要耐心,需要一段时间。

答案 1 :(得分:0)

您的查询:

SELECT SUM(full_update_count) as total
FROM x.attribute_usage_information
WHERE marketplace_id=6  AND ptd='Y' AND processed_date>2013-12-31 AND 
      feed_attribute_name='abc';

“使用where”表示MySQL正在进行全表扫描。这是一个简单的查询,因此唯一的优化方法是创建一个减少正在处理的行数的索引。此查询的最佳索引是x.attribute_usage_information(marketplace_id, ptd, feed_attribute_name, processed_date, full_update_count)

您可以将其创建为:

create index attribute_usage_information_idx on x.attribute_usage_information(marketplace_id, ptd, feed_attribute_name, processed_date, full_update_count);

通过加入full_update_count,这是一个覆盖索引。这进一步加快了查询速度,因为查询中使用的所有列都在索引中。执行引擎不需要在原始数据页上查找值。