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