我该如何优化mySQL查询? (SUM,200k行)

时间:2013-09-27 09:39:38

标签: mysql sql

如何更优化此查询?

SELECT COUNT(*) AS releases, SUM(bt.size) AS sum_size, SUM(a.download_count) download_count FROM bb_bt_torrents bt LEFT JOIN bb_attachments_desc a ON (a.attach_id = bt.attach_id) WHERE bt.poster_id = 35979;

此查询可以在大约25秒的时间内运行。 我有什么选择?

以下是给我的解释:

+----+-------------+-------+--------+---------------+-----------+---------+------------------------+--------+-------+
| id | select_type | table | type   | possible_keys | key       | key_len | ref                    | rows   | Extra |
+----+-------------+-------+--------+---------------+-----------+---------+------------------------+--------+-------+
|  1 | SIMPLE      | bt    | ref    | poster_id     | poster_id | 3       | const                  | 178770 |       |
|  1 | SIMPLE      | a     | eq_ref | PRIMARY       | PRIMARY   | 3       | unionpeer.bt.attach_id |      1 |       |
+----+-------------+-------+--------+---------------+-----------+---------+------------------------+--------+-------+

表格定义:

CREATE TABLE IF NOT EXISTS `bb_attachments_desc` (
`attach_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`physical_filename` varchar(255) NOT NULL DEFAULT '',
`real_filename` varchar(255) NOT NULL DEFAULT '',
`download_count` mediumint(8) unsigned NOT NULL DEFAULT '0',
`comment` varchar(255) NOT NULL DEFAULT '',
`extension` varchar(100) NOT NULL DEFAULT '',
`mimetype` varchar(100) NOT NULL DEFAULT '',
`filesize` int(20) NOT NULL DEFAULT '0',
`filetime` int(11) NOT NULL DEFAULT '0',
`thumbnail` tinyint(1) NOT NULL DEFAULT '0',
`tracker_status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`attach_id`),
KEY `filetime` (`filetime`),
KEY `filesize` (`filesize`),
KEY `physical_filename` (`physical_filename`(10))
) ENGINE=InnoDB  DEFAULT CHARSET=cp1251;

CREATE TABLE IF NOT EXISTS `bb_bt_torrents` (
`torrent_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`info_hash` varbinary(20) NOT NULL,
`post_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`poster_id` mediumint(9) NOT NULL DEFAULT '0',
`topic_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`forum_id` smallint(5) unsigned NOT NULL DEFAULT '0',
`attach_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`size` bigint(20) unsigned NOT NULL DEFAULT '0',
`reg_time` int(11) NOT NULL DEFAULT '0',
`call_seed_time` int(11) NOT NULL DEFAULT '0',
`complete_count` mediumint(8) unsigned NOT NULL DEFAULT '0',
`seeder_last_seen` int(11) NOT NULL DEFAULT '0',
`tor_status` tinyint(4) NOT NULL DEFAULT '0',
`tor_status_uid` mediumint(8) NOT NULL DEFAULT '0',
`tor_status_time` int(11) NOT NULL DEFAULT '0',
`tor_type` tinyint(1) NOT NULL DEFAULT '0',
`speed_up` mediumint(8) NOT NULL DEFAULT '0',
`speed_down` mediumint(8) NOT NULL DEFAULT '0',
`last_seeder_uid` mediumint(9) NOT NULL DEFAULT '0',
`donor` tinyint(2) NOT NULL DEFAULT '-1',
`donor_get_time` int(11) NOT NULL DEFAULT '0',
`rutracker_ids` tinyint(1) NOT NULL DEFAULT '0',
`dht` tinyint(1) NOT NULL DEFAULT '2',
PRIMARY KEY (`torrent_id`),
UNIQUE KEY `post_id` (`post_id`),
UNIQUE KEY `topic_id` (`topic_id`),
UNIQUE KEY `attach_id` (`attach_id`),
UNIQUE KEY `info_hash` (`info_hash`),
KEY `reg_time` (`reg_time`),
KEY `forum_id` (`forum_id`),
KEY `poster_id` (`poster_id`),
KEY `size` (`size`),
KEY `tor_status` (`tor_status`),
KEY `tor_status_time` (`tor_status_time`),
KEY `tor_status_2` (`tor_status`,`tor_status_time`)
) ENGINE=InnoDB  DEFAULT CHARSET=cp1251;

我考虑重写此查询,但不知道如何。

0 个答案:

没有答案