[英]mysql query, slow performance on select or update
我有一個被頻繁更新和選擇的隊列表(8個並發線程)。 每個線程的操作是:
- 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 |
+----+-------------+-------+-------+----------------------------------+----------------+---------+------+------+-------------+
更新語句最多需要13秒(!)才能完成。 我不知道如何優化查詢或索引。 也許有人可以在這里幫助我。
謝謝。
這是您的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.