MySQL Sub Query需要很长时间才能响应

时间:2016-04-08 10:45:25

标签: mysql subquery

我创建了3张表imei,发布和查看。我在所有3个表中都有大约1000条记录。现在,当我执行b / m查询时,需要很长时间才能响应。

表设计&样本数据如下:

  $scope.selected = null;
  $scope.changeOption = function(text) {
    $scope.selected = text;
  }
CREATE TABLE IF NOT EXISTS `imei` (
  `imei_id` int(5) NOT NULL AUTO_INCREMENT,
  `imei_no` varchar(30) NOT NULL,
  `imei_net` varchar(30) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`imei_id`),
  UNIQUE KEY `imei_no` (`imei_no`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1346 ;
CREATE TABLE IF NOT EXISTS `post` (
  `post_id` int(5) NOT NULL AUTO_INCREMENT,
  `post_title` varchar(60) NOT NULL,
  `post_desc` varchar(500) NOT NULL,
  `post_author` varchar(30) NOT NULL DEFAULT 'Admin',
  `user_id` int(10) NOT NULL DEFAULT '1',
  `date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`post_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=876 ;

表内的值:

IMEI:

CREATE TABLE IF NOT EXISTS `view` (
  `view_id` int(5) NOT NULL AUTO_INCREMENT,
  `post_id` int(11) NOT NULL,
  `imei_id` varchar(30) NOT NULL,
  `status` varchar(10) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`view_id`),
  UNIQUE KEY `imei_id` (`imei_id`,`post_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13706 ;

POST:

INSERT INTO `imei` (`imei_id`, `imei_no`, `imei_net`, `date`) VALUES
(1, '123456789012345', 'Airtel', '2015-08-06 07:39:47'),
(2, '234567890123456', 'Aircel', '2015-08-06 06:08:33')

查看:

INSERT INTO `post` (`post_id`, `post_title`, `post_desc`, `post_author`, `user_id`, `date`) VALUES
    (1, 'NSC Rate Down', 'NSC rates are getting down from today', 'Admin', 1, '2015-07-08 05:29:54'),
    (2, 'NCDEX offers cashback', 'NCDEX offers cashback for the previous users', 'Admin', 1, '2015-07-08 05:30:01')

执行查询:

INSERT INTO `view` (`view_id`, `post_id`, `imei_id`, `status`, `date`) VALUES
(1, 1, '1', '1', '2015-08-08 05:04:38'),
(7, 2, '1', '1', '2015-08-08 07:55:25')

1 个答案:

答案 0 :(得分:0)

尝试此查询SELECT post_idpost_titlepost_descdate,if({post_id in(SELECT post_id FROM {{ 1}} WHERE view =(SELECT imei_id FROM imei_id WHERE imei = 865645026396909))),1,0)状态为来自imei_no