Select multi-line SQL statement slow query optimization

Source: Internet
Author: User
Environment: MySQL5.5 InnoDB Table post, the primary key is id

The requirements are very simple, returned by ID post.name , need to return the specified number at one time, so used where id in :

select `name` from `post` where `id` in(2142324,2106574,2106564,2075699,2065402,2050790,2038346,2038345,2035588,2031765,2022035,2022034,2020745,2020737,2020718,1987558,1970241,1962232,1911342,1891481,1889641,1877438,1877434,1867217,1866057,1866013,1847315);

Explain results:

+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+|  1 | SIMPLE      | post  | range | PRIMARY       | PRIMARY | 4       | NULL |   27 | Using where |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+

Now this SQL hour produces thousands of slow query records, I directly in the console execution each time also need 7, 8 seconds

How can we optimize it?

Reply content:

Environment: MySQL5.5 InnoDB table post , primary key isid

The requirements are very simple, returned by ID post.name , need to return the specified number at one time, so used where id in :

select `name` from `post` where `id` in(2142324,2106574,2106564,2075699,2065402,2050790,2038346,2038345,2035588,2031765,2022035,2022034,2020745,2020737,2020718,1987558,1970241,1962232,1911342,1891481,1889641,1877438,1877434,1867217,1866057,1866013,1847315);

Explain results:

+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+|  1 | SIMPLE      | post  | range | PRIMARY       | PRIMARY | 4       | NULL |   27 | Using where |+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+

Now this SQL hour produces thousands of slow query records, I directly in the console execution each time also need 7, 8 seconds

How can we optimize it?

From the pure SQL point of view is no problem, go index, rows value is not small, if the speed is very slow, you can see from 3 aspects of 1:INNODB parameters have been optimized, such as InnoDBbufferpoor_size such as 2:qcache hit rate. According to your appeal question, an hour this statement produces thousands of slow queries, the hit rate is obviously not high, whether the table write frequency too high results in qcache failure, whether to consider read and write separation 3: Large data size, consider partitioning, or horizontal table

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.