如何使用类似的子查询优化Mysql查询?

时间:2017-08-29 21:05:31

标签: mysql

请求按我的需要工作,但我知道这不正确,请帮助我了解如何正确地进行类似的查询。

  

注意:SELECT read_date和SELECT auto重复!

THX!

SELECT let.letter_from, let.`letter_to`, COUNT(let.`letter_from`) AS sendMail,
    (SELECT COUNT(`status`) 
     FROM letters 
     WHERE `status` = 1 AND letter_from = 310 AND letter_to = let.`letter_to` 
     ) AS readMail,
     (SELECT `read_date`
      FROM letters 
      WHERE letter_from = 310 AND `status` = 1  AND `letter_to` = let.`letter_to`
      ORDER BY `read_date` DESC LIMIT 1
     ) AS dateLastRead,
     (SELECT `auto`
      FROM letters 
      WHERE letter_from = 310 AND `status` = 1  AND `letter_to` = let.`letter_to`
      ORDER BY `read_date` DESC LIMIT 1
     ) AS auto
FROM `letters` let
WHERE letter_from = 310
GROUP BY letter_to;
CREATE TABLE `letters` (
  `letter_id` int(11) NOT NULL AUTO_INCREMENT,
  `letter_to` int(11) NOT NULL,
  `letter_from` int(11) NOT NULL,
  `letter_send_date` datetime NOT NULL,
  `status` tinyint(1) NOT NULL,
  `first_letter` tinyint(1) NOT NULL DEFAULT '0',
  `auto` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `answer` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `read_date` datetime DEFAULT NULL,
  `answer_date` datetime DEFAULT NULL,
  `forgetting` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`letter_id`),
  KEY `letter_to` (`letter_to`),
  KEY `letter_from` (`letter_from`)  
) ENGINE=InnoDB AUTO_INCREMENT=604 DEFAULT CHARSET=utf8;
insert  into `letters`(`letter_id`,`letter_to`,`letter_from`,`letter_send_date`,`status`,`first_letter`,`auto`,`answer`,`read_date`,`answer_date`,`forgetting`) values 
(482,310,313,'2017-02-10 13:32:56',1,0,0,1,'2017-02-11 12:33:15','2017-02-13 12:28:43',0),
(483,310,313,'2017-02-10 13:51:12',1,0,0,1,'2017-02-12 12:33:15','2017-02-10 14:03:46',0),
(484,310,313,'2017-02-10 13:52:02',1,0,0,1,'2017-02-11 12:33:15','2017-02-10 14:01:47',0),
(485,313,310,'2017-02-10 13:58:03',1,0,0,1,'2017-02-12 12:33:15','2017-02-13 12:39:26',0),
(486,313,310,'2017-02-10 14:01:22',1,0,0,1,'2017-02-11 11:33:15','2017-02-13 12:38:47',0),
(487,313,310,'2017-02-10 14:03:46',1,0,0,1,'2017-02-12 12:33:15','2017-02-13 12:37:38',0),
(488,313,310,'2017-02-10 14:27:53',1,0,0,1,'2017-02-13 11:33:15','2017-02-13 12:34:19',0),
(489,313,310,'2017-02-13 12:28:43',1,0,0,1,'2017-02-13 10:33:15','2017-02-13 12:33:15',0),
(490,310,313,'2017-02-13 12:33:15',0,0,0,0,NULL,NULL,0),
(491,310,313,'2017-02-13 12:34:02',0,0,0,0,NULL,NULL,0),
(492,310,313,'2017-02-13 12:36:30',1,0,0,1,'2017-02-13 13:00:21','2017-02-13 13:09:35',0),
(493,310,313,'2017-02-13 12:37:38',1,0,0,1,'2017-02-13 13:00:21','2017-02-13 13:06:21',0),
(494,310,313,'2017-02-13 12:38:47',1,0,0,1,'2017-02-13 13:00:59','2017-02-13 13:04:59',0),
(495,310,313,'2017-02-13 12:39:26',1,0,0,1,'2017-02-13 12:40:41','2017-02-13 12:43:41',0),
(496,313,310,'2017-02-13 12:43:41',1,0,0,1,'2017-02-17 12:43:41','2017-03-22 12:26:48',0),
(497,313,310,'2017-02-13 12:55:44',1,0,0,1,'2017-02-15 12:55:44','2017-03-22 12:25:36',0),
(498,313,310,'2017-02-13 13:02:26',1,0,0,1,'2017-02-15 13:02:26','2017-03-22 12:24:37',0),
(499,313,310,'2017-02-13 13:03:55',1,0,0,1,'2017-03-05 15:14:53','2017-03-06 15:14:53',0),
(500,313,310,'2017-02-13 13:04:59',1,0,0,1,'2017-02-13 14:20:43','2017-02-13 14:26:43',0),
(501,313,310,'2017-02-13 13:06:21',1,0,0,1,'2017-03-05 15:12:35','2017-03-06 15:12:35',0),
(502,313,310,'2017-02-13 13:09:34',1,0,0,1,'2017-02-13 13:09:46','2017-02-13 13:10:46',0),
(503,310,313,'2017-02-13 13:10:46',0,0,0,0,NULL,NULL,0),
(504,310,313,'2017-02-13 14:25:04',0,0,0,0,NULL,NULL,0),
(505,310,313,'2017-02-13 14:26:43',0,0,0,0,NULL,NULL,0),
(506,310,313,'2017-03-06 15:12:35',0,0,0,0,NULL,NULL,0),
(507,310,313,'2017-03-06 15:14:00',0,0,0,0,NULL,NULL,0),
(508,310,313,'2017-03-06 15:14:11',0,0,0,0,NULL,NULL,0),
(509,310,313,'2017-03-06 15:14:22',1,0,0,1,'2017-03-29 12:53:45','2017-03-29 12:54:21',0),
(510,310,313,'2017-03-06 15:14:36',1,0,0,0,'2017-04-21 13:32:15',NULL,0),
(511,310,313,'2017-03-06 15:14:42',1,0,0,1,'2017-04-21 13:04:17','2017-04-21 13:04:46',0),
(512,310,313,'2017-03-06 15:14:53',1,0,0,1,'2017-03-29 12:53:35','2017-04-21 13:07:17',0),
(513,310,313,'2017-03-06 15:16:05',0,0,0,0,NULL,NULL,0),
(514,310,313,'2017-03-06 15:16:27',0,0,0,0,NULL,NULL,0),
(515,310,313,'2017-03-06 15:16:42',0,0,0,0,NULL,NULL,0),
(516,310,313,'2017-03-06 15:17:20',1,0,0,0,'2017-04-21 13:11:01',NULL,0),
(517,325,313,'2017-03-06 15:21:59',1,0,0,1,'2017-03-20 12:22:28','2017-03-22 12:22:28',0),
(518,312,313,'2017-03-06 15:41:34',1,0,0,1,'2017-03-22 14:07:43','2017-03-22 14:08:11',0),
(519,327,313,'2017-03-06 15:52:26',1,0,0,1,'2017-03-20 13:34:38','2017-03-22 11:51:58',0),
(520,313,327,'2017-03-22 11:51:58',1,0,0,1,'2017-03-20 12:22:28','2017-03-22 12:32:09',0),
(521,313,325,'2017-03-22 12:22:07',1,0,0,1,'2017-03-20 12:22:28','2017-03-22 12:24:01',0),
(522,325,313,'2017-03-22 12:24:01',1,0,0,1,'2017-04-21 13:31:22','2017-04-21 13:32:21',0),
(523,310,313,'2017-03-22 12:24:37',1,0,0,0,'2017-04-21 13:23:42',NULL,0),
(524,310,313,'2017-03-22 12:25:17',1,0,0,1,'2017-04-21 13:03:29','2017-04-21 13:03:45',0),
(525,310,313,'2017-03-22 12:26:48',1,0,0,1,'2017-04-21 13:10:23','2017-04-21 13:10:55',0),
(526,327,313,'2017-03-22 12:32:09',1,0,0,1,'2017-03-22 12:32:09','2017-03-22 12:33:10',0),
(527,313,327,'2017-03-22 12:33:10',1,0,0,1,'2017-03-22 12:33:09','2017-03-22 12:35:59',0),
(528,327,313,'2017-03-22 12:35:59',1,0,0,1,'2017-03-22 12:36:09','2017-03-22 12:38:05',0),
(529,313,327,'2017-03-22 12:38:05',1,0,0,1,'2017-03-22 12:38:09','2017-03-22 12:38:58',0),
(530,327,313,'2017-03-22 12:38:58',0,0,0,0,NULL,NULL,0),
(531,326,313,'2017-03-22 13:23:36',0,0,0,0,NULL,NULL,0),
(532,310,313,'2017-03-22 13:29:22',1,0,0,1,'2017-03-29 12:53:18','2017-04-21 13:13:46',0),
(533,310,311,'2017-03-22 14:03:06',1,0,0,1,'2017-08-03 13:44:16','2017-08-03 13:44:20',0),
(534,310,311,'2017-03-22 14:03:24',1,0,0,1,'2017-08-03 13:44:23','2017-08-03 13:44:28',0),
(535,329,311,'2017-03-22 14:06:53',0,0,0,0,NULL,NULL,0),
(536,313,312,'2017-03-22 14:08:11',1,0,0,1,'2017-03-23 12:22:08','2017-03-27 12:22:08',0),
(537,335,313,'2017-03-27 12:20:53',0,0,0,0,NULL,NULL,0),
(538,312,313,'2017-03-27 12:22:08',1,0,0,1,'2017-06-28 12:13:32','2017-06-28 12:13:42',0),
(539,341,311,'2017-03-27 12:50:39',1,0,0,1,'2017-04-10 13:59:38','2017-06-28 12:12:28',0),
(540,325,346,'2017-03-27 13:16:10',1,0,0,1,'2017-04-10 13:02:51','2017-04-10 13:03:04',0),
(541,328,346,'2017-03-27 13:19:39',1,0,0,1,'2017-06-28 12:12:39','2017-06-28 12:12:50',0),
(542,313,310,'2017-03-29 12:54:21',1,0,0,1,'2017-04-10 13:18:09','2017-04-21 13:18:09',0),
(543,346,325,'2017-04-10 13:03:04',1,0,0,0,'2017-04-14 13:03:04',NULL,0),
(544,354,352,'2017-04-21 12:34:45',1,0,0,1,'2017-05-11 13:20:32','2017-05-12 13:17:54',0),
(545,335,352,'2017-04-21 12:35:24',1,0,0,1,'2017-05-11 13:20:32','2017-05-12 13:20:32',0),
(546,313,310,'2017-04-21 13:03:45',1,0,1,0,'2017-04-26 13:13:46',NULL,0),
(547,313,310,'2017-04-21 13:04:46',1,0,0,0,'2017-04-25 13:13:46',NULL,0),
(548,313,310,'2017-04-21 13:07:17',1,0,0,0,'2017-04-22 13:13:46',NULL,0),
(549,313,310,'2017-04-21 13:10:55',1,0,0,0,'2017-04-22 13:13:46',NULL,0),
(551,313,310,'2017-04-21 13:13:46',1,0,1,0,'2017-04-23 13:13:46',NULL,0),
(552,310,313,'2017-04-21 13:18:09',1,0,0,1,'2017-04-21 13:19:21','2017-04-21 13:19:57',0),
(553,313,310,'2017-04-21 13:19:57',0,0,0,0,NULL,NULL,0),
(554,313,325,'2017-04-21 13:32:21',0,0,0,0,NULL,NULL,0),
(555,313,310,'2017-04-21 15:01:43',0,0,0,0,NULL,NULL,0),
(556,352,354,'2017-05-12 13:14:47',3,1,0,0,NULL,NULL,0),
(557,341,376,'2017-05-12 13:16:46',1,0,0,1,'2017-05-26 13:57:04','2017-05-26 13:57:22',0),
(558,312,376,'2017-05-12 13:16:55',0,0,0,0,NULL,NULL,0),
(559,352,354,'2017-05-12 13:17:54',0,0,0,0,NULL,NULL,0),
(560,327,376,'2017-05-12 13:19:18',0,0,0,0,NULL,NULL,0),
(561,352,335,'2017-05-12 13:19:56',3,1,0,0,NULL,NULL,0),
(562,352,335,'2017-05-12 13:20:04',0,0,0,0,NULL,NULL,0),
(563,352,335,'2017-05-12 13:20:13',0,0,0,0,NULL,NULL,0),
(564,352,335,'2017-05-12 13:20:32',0,0,0,0,NULL,NULL,0),
(566,377,335,'2017-05-12 13:29:30',1,1,0,1,'2017-05-12 12:29:52','2017-05-12 13:29:52',0),
(567,335,377,'2017-05-12 13:29:52',0,0,0,0,NULL,NULL,0),
(568,376,335,'2017-05-12 13:30:41',3,1,0,0,NULL,NULL,0),
(569,376,335,'2017-05-12 13:30:47',0,0,0,0,NULL,NULL,0),
(570,376,341,'2017-05-26 13:57:22',0,0,0,0,NULL,NULL,0),
(571,376,341,'2017-05-30 16:02:58',0,0,0,0,NULL,NULL,0),
(572,328,352,'2017-06-17 13:24:49',1,0,0,1,'2017-06-28 12:13:01','2017-06-28 12:13:07',0),
(573,328,352,'2017-06-17 13:25:21',1,0,0,1,'2017-06-28 12:13:02','2017-06-28 12:13:18',0),
(576,341,381,'2017-06-23 14:19:48',1,0,0,1,'2017-06-28 12:10:58','2017-06-28 12:11:13',0),
(577,341,381,'2017-06-23 14:20:01',1,0,0,1,'2017-06-28 12:11:25','2017-06-28 12:11:40',0),
(578,313,327,'2017-06-24 14:42:53',0,0,0,0,NULL,NULL,0),
(579,313,327,'2017-06-24 14:43:38',0,0,0,0,NULL,NULL,0),
(580,327,376,'2017-06-28 12:03:59',0,0,0,0,NULL,NULL,0),
(581,341,376,'2017-06-28 12:04:24',0,0,0,0,NULL,NULL,0),
(582,341,376,'2017-06-28 12:04:48',0,0,0,0,NULL,NULL,0),
(583,341,376,'2017-06-28 12:05:03',0,0,0,0,NULL,NULL,0),
(584,381,341,'2017-06-28 12:11:13',0,0,0,0,NULL,NULL,0),
(585,381,341,'2017-06-28 12:11:40',0,0,0,0,NULL,NULL,0),
(586,311,341,'2017-06-28 12:12:27',0,0,0,0,NULL,NULL,0),
(587,346,328,'2017-06-28 12:12:50',0,0,0,0,NULL,NULL,0),
(588,352,328,'2017-06-28 12:13:07',0,0,0,0,NULL,NULL,0),
(589,352,328,'2017-06-28 12:13:18',0,0,0,0,NULL,NULL,0),
(590,313,312,'2017-06-28 12:13:42',0,0,0,0,NULL,NULL,0),
(591,327,387,'2017-06-28 12:27:41',0,0,0,0,NULL,NULL,0),
(592,327,381,'2017-07-03 14:25:24',0,0,0,0,NULL,NULL,0),
(593,341,389,'2017-07-08 12:58:46',1,0,0,0,'2017-07-09 12:58:46',NULL,0),
(594,311,310,'2017-08-03 13:44:20',0,0,0,0,NULL,NULL,0),
(595,311,310,'2017-08-03 13:44:28',0,0,0,0,NULL,NULL,0),
(596,391,310,'2017-08-03 14:07:19',1,1,0,0,'2017-08-04 14:07:19',NULL,0),
(597,391,310,'2017-08-10 15:07:26',1,1,0,1,'2017-08-20 13:44:30','2017-08-28 13:44:30',0),
(598,310,389,'2017-08-04 11:40:08',0,0,0,0,NULL,NULL,0),
(599,310,369,'2017-08-17 17:24:26',1,0,0,1,'2017-08-17 17:25:16','2017-08-17 17:30:31',0),
(600,369,310,'2017-08-17 17:30:31',1,0,0,0,'2017-08-18 17:30:31',NULL,0),
(601,310,390,'2017-08-28 13:44:30',0,0,0,0,NULL,NULL,0),
(602,310,391,'2017-08-28 13:54:47',0,0,0,0,NULL,NULL,0),
(603,327,391,'2017-08-28 13:55:19',0,0,0,0,NULL,NULL,0);

1 个答案:

答案 0 :(得分:0)

它很难看,但添加这些(按给定的顺序):

INDEX(letter_from, `status`, `letter_to`, `read_date`, `auto`)
INDEX(letter_from, letter_to, status)

第一个用于子查询,并且是'覆盖&#39 ;;第二个是外部查询,应该避免使用tmp表和/或filesort。 (见EXPLAIN SELECT ...

你真的想要COUNT(status)吗?这会计算非空状态行的数量。也许你想要SUM(status),它会给你一个较小的数字。

这没有意义:

SELECT COUNT(let.`letter_from`) AS sendMail
   ... WHERE letter_from = 310

您对COUNT的期待是什么?

我暂时放弃COUNTsstatus。看看其余部分,我推断出你正试图找到最新的"从310到每个可能的收件人的信(letter_to)。然后显示read_date和auto以及其他一些东西?

让我们首先看一下"最新的":

SELECT letter_from, letter_to, MAX(read_date)
    FROM letters
    WHERE letter_from = 310
    GROUP BY letter_to;

不要让它变得更复杂,你会遇到麻烦。为了提高性能,请添加INDEX(letter_from, letter_to, read_date)

现在,让我们使用它作为衍生的'表:

SELECT l.letter_from,
       l.letter_to,
       s.read_date,
       s.auto
    FROM ( that subquery ... ) AS s
    JOIN letters AS l  USING(letter_from, letter_to, read_date)

然后,在更好地理解status之后,也许我可以毫不费力地将其折叠起来。