테이블 user_notification이 있습니다.SQL 쿼리를 최적화하는 방법
SELECT * FROM `user_notification` `t` WHERE `t`.`status`=2;
을 실행하는 cron 명령 실행이 어디 상태 일 - Send_fail, 2 대기열 (.이 같은) 3- 성공 주위 ~ 100K 기록 거기 테이블에서
하루 기하 급수적으로하는 증가하고 있습니다 하루. 이 쿼리는 너무 많은 시간이 걸립니다. 이 쿼리를 최적화하는 방법이 있습니까?
#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;'별칭을 사용할 필요가 없다고 생각합니다. – user3441151
@ user3441151. . . 별칭은 일반적으로 좋은 아이디어이며 낙담해서는 안됩니다. –