我有一张表,里面的数据很快就传开了。这是它的结构:
CREATE TABLE `tasks_pending` (
`pending_id` int(11) NOT NULL AUTO_INCREMENT,
`task_id` int(9) NOT NULL,
`user_id` int(9) NOT NULL,
`added_time` int(11) NOT NULL DEFAULT '0',
`additional` varchar(1000) DEFAULT NULL,
`taken` tinyint(1) NOT NULL DEFAULT '0',
`taken_by` int(11) NOT NULL,
`taken_time` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pending_id`),
UNIQUE KEY `task_id` (`task_id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `added_time` (`added_time`),
KEY `task_id_2` (`task_id`),
KEY `taken` (`taken`)
) ENGINE=MEMORY
我还有一个大查询可以提取所需的数据:
SELECT DISTINCT `task_id` AS tid, `pending_id` as pid, `taken_time`, `taken_by`, `additional`,
(
SELECT COUNT( pending_id )
FROM tasks_pending tp
WHERE task_id = tid
) AS count,
(
SELECT remain
FROM tasks
WHERE task_id = tid
) AS rem,
(
SELECT type
FROM tasks tas
WHERE task_id = tid
) AS type
FROM `tasks_pending`
WHERE `taken` = '0'
HAVING
(
count > 9
OR count = rem
OR type = 'pack'
)
limit 30
子查询的 where 子句中的所有列都有 PRIMARY 索引,因此它们必须快速执行。我找不到问题,也许你能找到我?先感谢您。
PS我是俄罗斯人,很抱歉英语不好。
UPD:tasks
表结构:
CREATE TABLE `tasks` (
`task_id` int(50) NOT NULL AUTO_INCREMENT,
`user_id` int(50) DEFAULT NULL,
`type` enum('video','friend','group','like','pack','other') NOT NULL,
`url` varchar(500) NOT NULL,
`additional` varchar(1000) DEFAULT NULL,
`remain` int(30) DEFAULT NULL,
`created_at` int(11) NOT NULL DEFAULT '0',
`updated_at` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`task_id`),
KEY `user_id` (`user_id`),
KEY `remain` (`remain`),
KEY `created_at` (`created_at`),
KEY `updated_at` (`updated_at`)
) ENGINE=InnoDB