2015-09-13 59 views
0

我有一個包含大約500K個作業的表,每個作業都有一個唯一的ID用作主鍵,並且用於指示作業是掛起,完成還是失敗。狀態是一個不是關鍵的整數。極慢的MySQL性能

我的問題是,一個簡單的查詢,我嘗試根據狀態選擇作業需要很多時間,超過10分鐘。 DB中有大約46個線程連接,我也重新啓動了它,但它並沒有幫助性能。

我粘貼表模式,我在這裏嘗試運行查詢: http://pastie.org/10416054

有沒有辦法找到有什麼瓶頸,優化表,所以它並沒有那麼長的時間?

回答

11

小時後我將步槍折以下命令:

CREATE INDEX idx_qry_status ON queries(status); 

當你的查詢執行表,從業人員沒有任何指標。

請參閱Create Index上的手冊頁。

視覺的後,表明智的(不是性能明智):

create table queries 
( id bigint auto_increment primary key, 
    status int null 
    -- partial definition 
); 
insert queries (status) values (7),(2),(1),(4),(1),(5),(9),(11); 

CREATE INDEX idx_qry_status ON queries(status); 
show indexes from queries; 
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| queries |   0 | PRIMARY  |   1 | id   | A   |   8 |  NULL | NULL |  | BTREE  |   |    | 
| queries |   1 | idx_qry_status |   1 | status  | A   |   8 |  NULL | NULL | YES | BTREE  |   |    | 
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
+0

謝謝,我想我在做一個備份,現在我想它。 – Vasilis

+1

工程很好,查詢時間從11分鐘下降到不到1秒。我正在閱讀一篇關於MySQL索引的有趣文章,它解釋了很多內容https://www.dreamhost.com/blog/2013/08/27/mysql-indexing-basics/ – Vasilis