我有一个表user_notification。有一个cron命令运行的执行如何优化我的sql查询
SELECT * FROM `user_notification` `t` WHERE `t`.`status`=2;
,其中状态1 - Send_fail,2-队列,3-成功(像这样)
在表中有大约10万〜记录被成倍增加一天白天。这个查询花费了太多时间。有没有一种方法来优化这个查询?
#Table structure for table `user_notification`
CREATE TABLE `user_notification` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`notification_id` int(11) NOT NULL,
`notification_title` varchar(256) NOT NULL,
`notification_message` text NOT NULL,
`status` int(1) NOT NULL,
`created` int(11) NOT NULL,
`updated` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `user_notification`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id` (`user_id`),
ADD KEY `notification_id` (`notification_id`);
ALTER TABLE `user_notification`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=94322;
ALTER TABLE `user_notification`
ADD CONSTRAINT `user_notification_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
ADD CONSTRAINT `user_notification_ibfk_2` FOREIGN KEY (`notification_id`) REFERENCES `notification` (`id`);
# Query_time: 0.010663 Lock_time: 0.000045 Rows_sent: 0 Rows_examined: 17294
SET timestamp=1491527264;
SELECT * FROM `user_notification` `t` WHERE `t`.`status`=2;
查询执行时间为0.01秒。这还不错,但您仍然可以在列状态下创建索引。 –
@Manish为什么你在查询中使用't' SELECT * FROM user_notification t WHERE t.status = 2;'我认为没有必要使用'aliases' – user3441151
@ user3441151。 。 。别名通常是一个好主意,不应该气馁。 –