简体   繁体   中英

Simple Query Slow In Mysql

Is there anyway to get better performance out of this.

select * from p_all where sec='0P00009S33' order by date desc

Query took 0.1578 sec.

Table structure is shown below. There are more than 100 Millions records in this table.

+------------------+---------------+------+-----+---------+-------+
| Field            | Type          | Null | Key | Default | Extra |
+------------------+---------------+------+-----+---------+-------+
| sec              | varchar(10)   | NO   | PRI | NULL    |       |
| date             | date          | NO   | PRI | NULL    |       |
| open             | decimal(13,3) | NO   |     | NULL    |       |
| high             | decimal(13,3) | NO   |     | NULL    |       |
| low              | decimal(13,3) | NO   |     | NULL    |       |
| close            | decimal(13,3) | NO   |     | NULL    |       |
| volume           | decimal(13,3) | NO   |     | NULL    |       |
| unadjusted_close | decimal(13,3) | NO   |     | NULL    |       |
+------------------+---------------+------+-----+---------+-------+

EXPLAIN result

+----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table     | type | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | price_all | ref  | PRIMARY       | PRIMARY | 12      | const | 1731 | Using where |
+----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+

How can i speed up this query?

In your example, you do a SELECT * , but you only have an INDEX that contains the columns sec and date .

In result, MySQLs execution plan roughly looks like the following:

  1. Find all rows that have sec = 0P00009S33 in the INDEX. This is fast.
  2. Sort all returned rows by date . This is also possibly fast, depending on the size of your MySQL buffer. Here is possibly room for improvement by optimizing the sort_buffer_size .
  3. Fetch all columns (= full row) for each returned row from the previous INDEX query. This is slow! see (1)

You can optimize this drastically by reducing the SELECTed fields to the minimum. Example: If you only need the open price, do only a SELECT sec, date, open instead of SELECT * .

When you identified the minimum columns you need to query, add a combined INDEX that contains exactly these colums (all columns involved - in the WHERE , SELECT or ORDER BY clause)

This way you can completely skip the slow part of this query, (3) in my example above. When the INDEX already contains all necessary columns, MySQLs optimizer can avoid looking up the full columns and serve your query directly from the INDEX.

Disclaimer: I'm unsure in which order MySQL executes the steps, possibly i ordered (2) and (3) the wrong way round. But this is not important to answer this question, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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