我正在努力尝试优化一些查询并得到一些令人费解的结果(可能源于我对 MySQL 内部工作原理的有限理解)。
令人费解的事情(至少在这一点上对我来说)是,当我试图剖析完整的查询以优化它时,我发现内部选择查询(子查询)本身运行的速度要慢得多。我认为更简单的查询会运行得更快。下面是查询和我的结果:
完整的查询
SELECT r.id, r.serve_url, r.title, r.category_id, GROUP_CONCAT(hr.server_id) AS server_id
FROM hosted_resources hr
LEFT JOIN resources AS r ON (hr.resource_id = r.id)
WHERE hr.resource_id = (
    select id from resources
    where resource_type_id = 1
    and category_id = 1
    and id < 311
    order by date_added desc
    limit 1
)
GROUP BY r.id, r.serve_url, r.title, r.category_id;
EXPLAIN 查询的结果:
+----+-------------+-----------+-------+-------------------------------------------------------------------------------+----------------------------------------------+---------+-------+------+-----------------------------------------------------------+
| id | select_type | table     | type  | possible_keys                                                                 | key                                          | key_len | ref   | rows | Extra                                                     |
+----+-------------+-----------+-------+-------------------------------------------------------------------------------+----------------------------------------------+---------+-------+------+-----------------------------------------------------------+
|  1 | PRIMARY     | hr        | ref   | hosted_resources_resource_id_resource_id_idx,hosted_resources_resource_id_idx | hosted_resources_resource_id_resource_id_idx | 4       | const |    2 | Using where; Using index; Using temporary; Using filesort |
|  1 | PRIMARY     | r         | const | PRIMARY                                                                       | PRIMARY                                      | 4       | const |    1 |                                                           |
|  2 | SUBQUERY    | resources | ref   | PRIMARY,type_idx,category_idx,type_category_idx,type_category_date_idx        | type_category_date_idx                       | 8       |       |   87 | Using where; Using index                                  |
+----+-------------+-----------+-------+-------------------------------------------------------------------------------+----------------------------------------------+---------+-------+------+-----------------------------------------------------------+
基准测试结果:
Concurrency Level:      5000
Time taken for tests:   9.396 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      31900000 bytes
HTML transferred:       16900000 bytes
Requests per second:    10642.78 [#/sec] (mean)
Time per request:       469.802 [ms] (mean)
Time per request:       0.094 [ms] (mean, across all concurrent requests)
Transfer rate:          3315.47 [Kbytes/sec] received
子查询(我曾期望它运行得更快)
select id, serve_url, title, category_id from resources
where resource_type_id = 1
and category_id = 1
and id < 311
order by date_added desc
limit 1
EXPLAIN 查询的结果:
+----+-------------+-----------+------+------------------------------------------------------------------------+------------------------+---------+-------------+------+-------------+
| id | select_type | table     | type | possible_keys                                                          | key                    | key_len | ref         | rows | Extra       |
+----+-------------+-----------+------+------------------------------------------------------------------------+------------------------+---------+-------------+------+-------------+
|  1 | SIMPLE      | resources | ref  | PRIMARY,type_idx,category_idx,type_category_idx,type_category_date_idx | type_category_date_idx | 8       | const,const |   87 | Using where |
+----+-------------+-----------+------+------------------------------------------------------------------------+------------------------+---------+-------------+------+-------------+
基准测试结果:
Concurrency Level:      5000
Time taken for tests:   42.181 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      41800000 bytes
HTML transferred:       27100000 bytes
Requests per second:    2370.75 [#/sec] (mean)
Time per request:       2109.040 [ms] (mean)
Time per request:       0.422 [ms] (mean, across all concurrent requests)
Transfer rate:          967.75 [Kbytes/sec] received
资源表:
CREATE TABLE `resources` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 
  `resource_type_id` int(11) NOT NULL COMMENT,
  `resource_status_id` int(11) NOT NULL COMMENT 
  `is_hosted` bit(1) NOT NULL COMMENT 
  `category_id` int(11) NOT NULL COMMENT 
  `serve_url` varchar(255) DEFAULT NULL COMMENT 
  `title` varchar(255) DEFAULT NULL COMMENT 
  `parent_resource_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `serve_url_UNIQUE` (`serve_url`),
  KEY `type_idx` (`resource_type_id`),
  KEY `status_idx` (`resource_status_id`),
  KEY `category_idx` (`category_id`),
  KEY `resources_parent_resource_id_idx` (`parent_resource_id`),
  KEY `type_category_idx` (`resource_type_id`,`category_id`),
  KEY `date_added_idx` (`date_added`),
  KEY `type_category_date_idx` (`resource_type_id`,`category_id`,`date_added`),
  CONSTRAINT `resources_category_id` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `resources_parent_resource_id` FOREIGN KEY (`parent_resource_id`) REFERENCES `resources` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `resources_resource_status_id` FOREIGN KEY (`resource_status_id`) REFERENCES `resource_statuses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `resources_resource_type_id` FOREIGN KEY (`resource_type_id`) REFERENCES `resource_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=598 DEFAULT CHARSET=latin1;
任何困难都会非常非常感激。凯特