非常慢MySQL的读取性能

问题描述:

我在MySQL如下表:非常慢MySQL的读取性能

CREATE TABLE tweetdb(
     tweetid BIGINT(18) UNSIGNED NOT NULL, 
     userid INT(10) UNSIGNED NOT NULL, 
     timestamp CHAR(14), 
     tweet TEXT, 
     score TINYINT, 
    PRIMARY KEY(tweetid, userid) 
) ENGINE=MYISAM PARTITION BY KEY(userid) PARTITIONS 101; 

+-----------+---------------------+------+-----+---------+-------+ 
| Field  | Type    | Null | Key | Default | Extra | 
+-----------+---------------------+------+-----+---------+-------+ 
| tweetid | bigint(18) unsigned | NO | PRI | NULL |  | 
| userid | int(10) unsigned | NO | PRI | NULL |  | 
| timestamp | char(14)   | YES |  | NULL |  | 
| tweet  | text    | YES |  | NULL |  | 
| score  | tinyint(4)   | YES |  | NULL |  | 
+-----------+---------------------+------+-----+---------+-------+ 
5 rows in set (0.29 sec) 

我在这个表210万行。 我的暗潮服务器(Java应用程序)发送GET与以下选择查询:

"SELECT test.tweetdb.tweetid, test.tweetdb.tweet, test.tweetdb.score FROM test.tweetdb WHERE test.tweetdb.userid = 287543000 AND test.tweetdb.timestamp = 20140420000829;" 

我使用用户标识和时间戳来获得满意的结果,因为它是唯一我可以用来验证数据库中的数据。该数据库仅用于只读目的,没有写入/更新。

我也在桌上使用了一个索引。

mysql> SHOW INDEX FROM tweetdb; 
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| tweetdb |   1 | id_index |   1 | userid  | A   |   1 |  NULL | NULL | YES | BTREE  |   |    | 
| tweetdb |   1 | id_index |   2 | timestamp | A   |   1 |  NULL | NULL | YES | BTREE  |   |    | 
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
2 rows in set (0.00 sec) 

现在,即使使用分区以及将主键后,它需要几乎1秒至与正确的响应,这是很长的响应回。我的应用程序必须具有每秒至少6000个请求的吞吐量。

硬件配置:

我运行的暗潮服务器(前端)查询在Amazon M1.large例如MySQL服务器(后端)。为了避免延迟,我在同一个实例上运行两台服务器。

任何人都可以帮我吗?我正在耗尽想法。 谢谢!从暗潮前端服务器

更新

mysql> EXPLAIN SELECT * FROM test.tweetdb LIMIT 1; 
+----+-------------+---------+------+---------------+------+---------+------+-----------+-------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows  | Extra | 
+----+-------------+---------+------+---------------+------+---------+------+-----------+-------+ 
| 1 | SIMPLE  | tweetdb | ALL | NULL   | NULL | NULL | NULL | 270119913 |  | 
+----+-------------+---------+------+---------------+------+---------+------+-----------+-------+ 
1 row in set (3.67 sec) 


mysql> EXPLAIN SELECT * FROM test.tweetdb WHERE test.tweetdb.userid=287543000 AND test.tweetdb.timestamp=20140420000829; 
+----+-------------+---------+------+---------------+------+---------+------+---------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+---------+------+---------------+------+---------+------+---------+-------------+ 
| 1 | SIMPLE  | tweetdb | ALL | NULL   | NULL | NULL | NULL | 2657601 | Using where | 
+----+-------------+---------+------+---------------+------+---------+------+---------+-------------+ 
1 row in set (0.00 sec) 

时间

The time it takes is 1.3 seconds

+0

什么'解释select ...'说? – 2015-03-31 10:25:02

+0

更新了问题。 – AngryPanda 2015-03-31 10:30:23

+0

这是清除它没有使用任何索引,你可能需要添加一个索引作为'alter table test.tweetdb add index user_timestamp_idx(userid,timestamp)' – 2015-03-31 10:32:13

你的主键是tweetid和用户ID的组合。而对于mysql,它将进行全面搜索,因为您的表具有combile列的主键。您可以创建另一个只有userid的密钥。 对于mysql,如果你有两列的密钥,那么他们应该出现在其他地方,否则它认为它整个表搜索

+0

在我的数据集中,用户标识和时间戳组合不是唯一的。 twitterbot可以同时创建多个推文。 我想在tweetid,userid和timestamp上创建一个主键,但随后将数据加载到表中需要很长时间。 你是否建议我将主键放在一起? – AngryPanda 2015-03-31 10:57:30