2014-09-30 51 views
0

我有一個頻繁更新和選擇的隊列表(8個併發線程)。每個線程的操作是:MySQL查詢,選擇或更新時性能降低

- count for queue_process_id 
- count == 0, update 10 new items with queue_process_id = N 
- select and search for queue_process_id 

隊列表:

CREATE TABLE `queue` (
    `queue_id` int(11) NOT NULL AUTO_INCREMENT, 
    `queue_module_id` tinyint(4) NOT NULL, 
    `queue_action` int(11) NOT NULL, 
    `queue_value` text NOT NULL, 
    `queue_value2` varchar(32) NOT NULL, 
    `queue_priority` smallint(6) NOT NULL, 
    `queue_order` smallint(6) NOT NULL, 
    `queue_process_id` tinyint(4) NOT NULL DEFAULT '99', 
    PRIMARY KEY (`queue_id`), 
    KEY `queue_priority` (`queue_priority`), 
    KEY `queue_module_id` (`queue_module_id`), 
    KEY `queue_action` (`queue_action`), 
    KEY `queue_order` (`queue_order`), 
    KEY `queue_process_id` (`queue_process_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2502029 DEFAULT CHARSET=utf8 | 

這是用於更新新項目的查詢:

update queue set queue_process_id = N where queue_module_id = N and queue_process_id = 99 order by queue_priority desc limit 10 

queue_module_id爲不同的模塊。每個模塊都有N個線程,所有這些線程都使用同一個表。

這個結果對EXPLAIN語句(更新切換選擇):

mysql> explain select SQL_NO_CACHE * from queue where queue_module_id = 1 and queue_process_id = 99 order by queue_priority desc limit 10; 

    +----+-------------+-------+-------+----------------------------------+----------------+---------+------+------+-------------+ 
    | id | select_type | table | type | possible_keys     | key   | key_len | ref | rows | Extra  | 
    +----+-------------+-------+-------+----------------------------------+----------------+---------+------+------+-------------+ 
    | 1 | SIMPLE  | queue | index | queue_module_id,queue_process_id | queue_priority | 2  | NULL | 20 | Using where | 
    +----+-------------+-------+-------+----------------------------------+----------------+---------+------+------+-------------+ 

update語句需要最多13秒完成(!)。我不知道如何優化查詢或索引。也許有人可以幫助我在這裏。

謝謝。

回答

1

這是你update查詢:

update queue 
    set queue_process_id = N 
    where queue_module_id = N and queue_process_id = 99 
    order by queue_priority desc 
    limit 10; 

(我假設N是一個佔位符,因爲它缺乏需要恆定的報價。)

您可以通過索引速度這一點。最好的索引是queue(queue_module_id, queue_process_id, queue_priority)。你可以創建這個使用:

create index idx_queue_3 on queue(queue_module_id, queue_process_id, queue_priority) 
+0

很好的答案。 CPU從100%到0-2%。我真的必須考慮索引。謝謝。 – hberg539 2014-09-30 13:08:50