我有以下查询:
SELECT DISTINCT f1.match_static_id,
f2.comments_no,
f2.maxtimestamp,
users.username,
users.id,
matches_of_comments.localteam_name,
matches_of_comments.visitorteam_name,
matches_of_comments.localteam_goals,
matches_of_comments.visitorteam_goals,
matches_of_comments.match_status,
new_iddaa.iddaa_code
FROM comments AS f1
INNER JOIN (
SELECT match_static_id,
MAX( TIMESTAMP ) maxtimestamp,
COUNT( match_static_id ) AS comments_no
FROM comments
GROUP BY match_static_id
) AS f2 ON f1.match_static_id = f2.match_static_id
AND f1.timestamp = f2.maxtimestamp
INNER JOIN users ON users.id = f1.user_id
INNER JOIN matches_of_comments ON matches_of_comments.match_id = f2.match_static_id
LEFT JOIN new_iddaa ON new_iddaa.match_id = matches_of_comments.match_id
WHERE matches_of_comments.flag =1
ORDER BY f2.maxtimestamp DESC
这是该查询的 EXPLAIN 计划:
+----+-------------+---------------------+--------+-----------------------------------+-----------+---------+------------------------------------------+-------+------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+---------------------+--------+-----------------------------------+-----------+---------+------------------------------------------+-------+------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 542 | Using temporary; Using filesort |
| 1 | PRIMARY | f1 | ref | timestamp,match_static_id,user_id | timestamp | 4 | f2.maxtimestamp | 1 | Using where |
| 1 | PRIMARY | users | eq_ref | PRIMARY | PRIMARY | 4 | skormix_db1.f1.user_id | 1 | |
| 1 | PRIMARY | matches_of_comments | ALL | match_id | NULL | NULL | NULL | 20873 | Range checked for each record (index map: 0x8) |
| 1 | PRIMARY | new_iddaa | ref | match_id | match_id | 4 | skormix_db1.matches_of_comments.match_id | 1 | |
| 2 | DERIVED | comments | ALL | NULL | NULL | NULL | NULL | 933 | Using temporary; Using filesort |
+----+-------------+---------------------+--------+-----------------------------------+-----------+---------+------------------------------------------+-------+------------------------------------------------+
如果此匹配项至少包含一条评论,我将使用此查询来获取匹配信息。
我得到了团队的名称、代码(iddaa 代码)、评论数量、最后一个评论的时间戳、最后一个评论的作者。
我有一个大数据库,预计在接下来的几个月里它会更大,我对 MySQL 查询非常陌生,我想确保我从一开始就使用优化查询,所以我想知道如何阅读这个解释信息使查询更好更快。
我看到表中有很多地方没有使用索引,即使我构建了它们。
我还在表列中看到了派生,我不知道如何使这个查询更快以及如何摆脱文件排序,因为我无法为派生查询创建索引??
我用索引(键)写下查询中使用表的结构,希望能对我的问题得到一些提示或简单的答案,在此先感谢。
评论(f1)表结构是:
CREATE TABLE `comments` (
`id` int(25) NOT NULL AUTO_INCREMENT,
`comments` text COLLATE utf8_unicode_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date` date NOT NULL,
`time` time NOT NULL,
`match_static_id` int(25) NOT NULL,
`ip` varchar(255) CHARACTER SET latin1 NOT NULL,
`comments_yes_or_no` int(25) NOT NULL,
`user_id` int(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `timestamp` (`timestamp`),
KEY `match_static_id` (`match_static_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=935 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
用户表结构为:
CREATE TABLE `users` (
`id` int(25) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`gender` int(25) NOT NULL,
`first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`avatar` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`alert` int(25) NOT NULL,
`daily_tahmin` int(25) NOT NULL,
`monthly_tahmin` int(25) NOT NULL,
`admin` int(25) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=995 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
match_of_comments_ 结构是:
CREATE TABLE `matches_of_comments` (
`id` int(25) NOT NULL AUTO_INCREMENT,
`en_tournament_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`tournament_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`country_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`match_status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`match_time` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`match_date` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`static_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`fix_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`match_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`localteam_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`localteam_goals` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`localteam_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`visitorteam_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`visitorteam_goals` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`visitorteam_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ht_score` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`flag` int(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `match_status` (`match_status`),
KEY `match_date` (`match_date`),
KEY `match_id` (`match_id`),
KEY `localteam_id` (`localteam_id`),
KEY `visitorteam_id` (`visitorteam_id`),
KEY `flag` (`flag`)
) ENGINE=MyISAM AUTO_INCREMENT=237790 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
new_iddaa 表结构为:
CREATE TABLE `new_iddaa` (
`id` int(25) NOT NULL AUTO_INCREMENT,
`match_id` int(25) NOT NULL,
`iddaa_code` int(25) NOT NULL,
`tv_channel` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`skormix_tahmin` varchar(255) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`),
KEY `match_id` (`match_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8191 DEFAULT CHARSET=latin1