[英]Very Slow MySQL Read Performance
我在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)
該表中有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和時間戳獲取結果,因為這只是我可用於測試數據庫的數據。 該數據庫僅供只讀,無寫/更新。
我還在表上使用了索引。
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服務器(前端)來查詢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)
來自Undertow前端服務器的時間
您的主鍵是tweetid和userid的組合。 對於mysql,它將進行完全搜索,因為您的表具有combile列的主鍵。 您可以創建另一個只有用戶ID的密鑰。 對於mysql,如果您在鍵中有兩列,則它們應該出現在其他地方,否則它將在整個表搜索中考慮
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.