MySQL select query is terribly slow

2020-08-02 01:45发布

问题:

I have a table with 2 196 998 records:

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:

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.

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:

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)

1) Is there a way to optimize it?

2) If no, what about other databases? Do they perform better?

EDIT1:

After creating the compound index

CREATE INDEX idx_price_market_buy ON price (marketId, buy);

the query takes 0.00 sec.

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)

回答1:

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

Creating individual indexes on each column probably does not allow MySQL to optimize the query.

For this query, you want a compound index on (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.