简体   繁体   English

MySQL select 查询非常慢

[英]MySQL select query is terribly slow

I have a table with 2 196 998 records:我有一张有 2 196 998 条记录的表:

CREATE TABLE price (
    dt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    marketId INT,
    buy DOUBLE,
    sell DOUBLE,
    PRIMARY KEY (dt, marketId),
    FOREIGN KEY fk_price_market(marketId) REFERENCES market(id) ON UPDATE CASCADE ON DELETE CASCADE
)  ENGINE=INNODB;

The query查询

select max(buy) from price;

takes 1.92 sec that is a reasonable time and it takes 0.00 sec if I create an index on 'buy' column:需要 1.92 秒,这是一个合理的时间,如果我在“购买”列上创建索引,则需要 0.00 秒:

CREATE INDEX idx_price_buy ON price (buy);

And the query和查询

select count(*) from price where marketId=309;

takes 0.05 sec and returns 160 570.需要 0.05 秒并返回 160 570。

But the query但是查询

select max(buy) from price where marketId=309;

takes 15.49 sec (that is terribly huge) even if I create both idices:即使我创建了两个 idice,也需要 15.49 秒(这非常大):

CREATE INDEX idx_price_market ON price (marketId);
CREATE INDEX idx_price_buy ON price (buy);

(I am not sure, but probably index idx_price_market already exists because marketId column is needed in a foreign key constraint) (我不确定,但可能索引idx_price_market已经存在,因为外键约束中需要marketId列)

1) Is there a way to optimize it? 1)有没有办法优化它?

2) If no, what about other databases? 2)如果不是,那么其他数据库呢? Do they perform better?他们表现更好吗?

EDIT1:编辑1:

After creating the compound index创建复合索引后

CREATE INDEX idx_price_market_buy ON price (marketId, buy); CREATE INDEX idx_price_market_buy ON 价格(marketId,买入);

the query takes 0.00 sec.查询需要 0.00 秒。

desc select max(buy) from price where marketId=309;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                        |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
1 row in set, 1 warning (0.01 sec)
select max(buy) from price where marketId=309;

Creating individual indexes on each column probably does not allow MySQL to optimize the query.在每列上创建单独的索引可能不允许 MySQL 优化查询。

For this query, you want a compound index on (marketId, buy) .对于此查询,您需要(marketId, buy)上的复合索引

create index idx_price_market_buy ON price (marketId, buy);

The ordering of columns in the index matters: first the query filters on marketId (so you want this column in first position in the coumpound index), then it computes the maximum buy .索引中列的顺序很重要:首先查询marketId上的过滤器(因此您希望此列在复合索引中的第一个 position 中),然后计算最大buy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM