Java MySQL读取性能非常慢
我在MySQL中有下表:Java MySQL读取性能非常慢,java,mysql,http,amazon-ec2,undertow,Java,Mysql,Http,Amazon Ec2,Undertow,我在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
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)
我在这个表中有2.1亿行。
我的Undertow服务器(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;"
我使用userid和timestamp来获得结果,因为这是我唯一可以用来测试数据库的数据。数据库是只读的,没有写入/更新
我还使用了表上的索引
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个请求
硬件配置:
我正在运行一个Undertow服务器(前端)来查询AmazonM1.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)
来自牵引前端服务器的时间
您的主键是tweetid和userid的组合。对于mysql,它将进行完全搜索,因为您的表具有combile列的主键。您可以创建另一个只有userid的密钥。
对于mysql,如果您在键中有两列,那么它们应该出现在它认为用于整个表搜索的位置中什么是
explain select
says?更新了问题。很明显,它没有使用任何索引。您可能需要添加一个索引作为alter table test.tweetdb添加索引用户\u timestamp\u idx(userid,timestamp)
我已经有一个具有相同参数的索引。嗯,我在问题中遗漏了这一点。在我的数据集中,userid和timestamp组合不是唯一的。推特机器人可以同时创建多条推特。我想在tweetid、userid和timestamp上创建一个主键,但是将数据加载到表中需要很长时间。你建议我一起删除主键吗?