简体   繁体   English

MySQL索引几乎没有加快简单查询

[英]MySQL index barely speeding up simple query

I have this table, that contains around 80,000,000 rows. 我有这个表,包含大约80,000,000行。

CREATE TABLE `mytable` (
`date` date NOT NULL,
`parameters` mediumint(8) unsigned NOT NULL,
`num` tinyint(3) unsigned NOT NULL,
`val1` int(11) NOT NULL,
`val2` int(10) NOT NULL,
`active` tinyint(3) unsigned NOT NULL,
`ref` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ref`) USING BTREE,
KEY `parameters` (`parameters`)
) ENGINE=MyISAM AUTO_INCREMENT=79092001 DEFAULT CHARSET=latin1

it's articulated around 2 main columns: "parameters" and "date". 它围绕着两个主要栏目:“参数”和“日期”。 there are around 67,000 possible values for "parameters" for each "parameters" there are around 1200 rows, each with a different date. 每个“参数”的“参数”有大约67,000个可能的值,大约有1200行,每行有不同的日期。 so for each date, there are 67,000 rows. 所以对于每个日期,有67,000行。 1200 * 67,000 = 80,400,000. 1200 * 67,000 = 80,400,000。

table size appears as 1.5GB, index size 1.4GB. 表大小显示为1.5GB,索引大小为1.4GB。

now, I want to query the table to retrieve all rows of one "parameters" (actually I want to do it for each parameter, but this is a good start) 现在,我想查询表以检索一个“参数”的所有行(实际上我想为每个参数执行此操作,但这是一个好的开始)

SELECT val1 FROM mytable WHERE parameters=1;

the first run gives me results in 8 seconds subsequent runs for different but close values of parameters (2, 3, 4...) are instantaneous 第一次运行给出了8秒后续运行的结果,不同但接近的参数值(2,3,4 ......)是瞬时的

a run for a "far away" value (parameters=1000) gives me results in 8 seconds again. 一个“远离”值(参数= 1000)的运行再次给我8秒的结果。

I did tests running the same query without the index, and got results in 20 seconds, so I guess the index is kicking in as shown by EXPLAIN, but not giving a drastic jump in performances: 我在没有索引的情况下运行相同查询的测试,并在20秒内得到结果,所以我猜索引如EXPLAIN所示,但没有给出大幅度的跳跃:

+----+-------------+----------+------+---------------+------------+---------+-------+------+-------+
| id | select_type | table    | type | possible_keys | key        | key_len | ref   | rows | Extra |
+----+-------------+----------+------+---------------+------------+---------+-------+------+-------+
|  1 | SIMPLE      | mytable  | ref  | parameters    | parameters | 3       | const | 1097 |       |
+----+-------------+----------+------+---------------+------------+---------+-------+------+-------+

but I'm still baffled by the time for such and easy request (no join, directly on the index). 但是我仍然对这种简单的请求(没有加入,直接在索引上)的时间感到困惑。

the server is 2 years-old 2 cpu quad core 2.6GHz running Ubuntu, with 4G of RAM. 该服务器是2年前运行Ubuntu的2 cpu四核2.6GHz,带有4G的RAM。 I've raised the key_buffer parameter to 1G, and have restarted mysql, but noticed no change whatsoever. 我已经将key_buffer参数提升到了1G,并重新启动了mysql,但是没有发现任何变化。

should I consider this normal ? 我应该认为这是正常的吗? or is there something I'm doing wrong ? 还是有什么我做错了? I get the feeling with the right config the request should be almost immediate. 我得到了正确配置的感觉,请求应该几乎立即。

Try using a covering index, ie create an index that includes both of the columns you need. 尝试使用覆盖索引,即创建包含所需列的两个列的索引。 It won't need the second disk I/O to fetch the values from the main table since the data's right there in the index. 它不需要第二个磁盘I / O来从主表中获取值,因为数据就在索引中。

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

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