我在mysql中有一个表,它有1100万行; 我的选择请求使用索引,其中,temporary和filesort
SELECT SQL_NO_CACHE
SUM(t.requests) AS requests,
SUM(t.impression) as impression,
SUM(t.double_imp) as double_imp,
SUM(t.bad_requests) AS bad_requests,
SUM(t.empty_body) AS empty_body,
SUM(t.bad_vast) AS bad_vast,
SUM(t.exceptions) AS exceptions,
SUM(t.midpoint) AS midpoint,
SUM(t.thirdquartile) AS thirdquartile,
SUM(t.complete) AS complete,
SUM(t.click) AS click,
SUM(t.start) AS start,
SUM(t.error) as error,
SUM(t.creativeview) AS creativeview,
SUM(IF(t.rate_type = "0", (t.impression * t.fixed_rate) / 1000, (t.impression * t.rate) / 1000 * (t.percent_rate / 100))) as media_cost,
SUM((t.impression * t.rate) / 1000) as revenue,
t.date
FROM stats t
WHERE
t.date >= "2017-08-14 00:00:00" AND
t.tag_id = 185
GROUP BY t.date
当我尝试解释这个查询时,我有下一个:
+----+-------------+-------+------------+------+--------------------+---------------+---------+-------+---------+----------+---------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+---------------+---------+-------+---------+----------+---------------------------------------------------------------------+
| 1 | SIMPLE | t | NULL | ref | date,supply_tag_id | supply_tag_id | 5 | const | 5372331 | 50.00 | Using index condition; Using where; Using temporary; Using filesort |
+----+-------------+-------+------------+------+--------------------+---------------+---------+-------+---------+----------+---------------------------------------------------------------------+
我必须通过tag_id,域,国家和使用日期过滤器发送报告。 这需要很长时间。
结果:
+----------+------------+------------+--------------+------------+----------+------------+----------+---------------+----------+-------+--------+--------+--------------+--------------------+--------------------+---------------------+
| requests | impression | double_imp | bad_requests | empty_body | bad_vast | exceptions | midpoint | thirdquartile | complete | click | start | error | creativeview | media_cost | revenue | date |
+----------+------------+------------+--------------+------------+----------+------------+----------+---------------+----------+-------+--------+--------+--------------+--------------------+--------------------+---------------------+
| 1271160 | 90531 | 46 | 304032 | 99560 | 0 | 0 | 65379 | 61777 | 68897 | 458 | 200359 | 440547 | 200197 | 167.78620000647717 | 335.57240001295435 | 2017-08-14 00:00:00 |
| 1253073 | 93511 | 7 | 257968 | 96868 | 0 | 0 | 62792 | 59275 | 69590 | 480 | 200685 | 414717 | 200520 | 174.01877502643288 | 348.03755005286575 | 2017-08-14 01:00:00 |
| 1174477 | 77541 | 10 | 220188 | 86333 | 0 | 0 | 52267 | 49380 | 57340 | 442 | 190527 | 362037 | 190356 | 145.73635006021442 | 291.47270012042884 | 2017-08-14 02:00:00 |
| 1122816 | 63555 | 5 | 259560 | 81427 | 0 | 0 | 44284 | 41839 | 47567 | 340 | 156377 | 359633 | 156245 | 119.94110007583329 | 239.88220015166658 | 2017-08-14 03:00:00 |
| 1193945 | 46442 | 5 | 255575 | 81628 | 0 | 0 | 30905 | 29189 | 34025 | 263 | 132637 | 388845 | 132527 | 86.10265007089804 | 172.20530014179607 | 2017-08-14 04:00:00 |
| 1301825 | 72943 | 12 | 269850 | 92438 | 0 | 0 | 46121 | 42915 | 49640 | 545 | 227208 | 405245 | 227060 | 136.62139983045088 | 273.24279966090177 | 2017-08-14 05:00:00 |
| 1375913 | 77615 | 14 | 278095 | 99777 | 0 | 0 | 56659 | 52836 | 54024 | 791 | 269526 | 395597 | 269383 | 150.330824404467 | 300.661648808934 | 2017-08-14 06:00:00 |
| 1592128 | 40945 | 6 | 318811 | 106792 | 0 | 0 | 28313 | 26365 | 28708 | 388 | 269652 | 478694 | 269525 | 77.05547480582607 | 154.11094961165213 | 2017-08-14 07:00:00 |
| 2085863 | 70902 | 16 | 684129 | 144990 | 0 | 0 | 46282 | 43673 | 52301 | 416 | 301956 | 818018 | 301753 | 126.72110009752994 | 253.44220019505988 | 2017-08-14 08:00:00 |
| 1530312 | 56250 | 10 | 451030 | 105760 | 0 | 0 | 33535 | 31578 | 41443 | 258 | 191312 | 638500 | 191187 | 99.43862510215726 | 198.87725020431452 | 2017-08-14 09:00:00 |
+----------+------------+------------+--------------+------------+----------+------------+----------+---------------+----------+-------+--------+--------+--------------+--------------------+--------------------+---------------------+
10 rows in set (46.07 sec)
这是表结构:
CREATE TABLE `stats` (
`date` datetime NOT NULL,
`tag_id` int(11) DEFAULT NULL,
`domain` int(11) DEFAULT NULL,
`country` int(11) DEFAULT NULL,
`rate` float DEFAULT '0',
`percent_rate` float DEFAULT '0',
`fixed_rate` float DEFAULT '0',
`rate_type` int(11) NOT NULL DEFAULT '0',
`requests` int(11) DEFAULT '0',
`bad_requests` int(11) DEFAULT '0',
`empty_body` int(11) DEFAULT '0',
`bad_vast` int(11) DEFAULT '0',
`exceptions` int(11) DEFAULT '0',
`start` int(11) DEFAULT '0',
`firstquartile` int(11) DEFAULT '0',
`midpoint` int(11) DEFAULT '0',
`thirdquartile` int(11) DEFAULT '0',
`complete` int(11) DEFAULT '0',
`close` int(11) DEFAULT '0',
`pause` int(11) DEFAULT '0',
`resume` int(11) DEFAULT '0',
`acceptinvitationlinear` int(11) DEFAULT '0',
`timespentviewin` int(11) DEFAULT '0',
`otheradinteraction` int(11) DEFAULT '0',
`progress` int(11) DEFAULT '0',
`creativeview` int(11) DEFAULT '0',
`mute` int(11) DEFAULT '0',
`unmute` int(11) DEFAULT '0',
`fullscreen` int(11) DEFAULT '0',
`impression` int(11) DEFAULT '0',
`player_imp` int(11) NOT NULL DEFAULT '0',
`double_imp` int(11) NOT NULL DEFAULT '0',
`error` int(11) DEFAULT '0',
`no_show` int(11) NOT NULL DEFAULT '0',
`budget_limit` int(11) NOT NULL DEFAULT '0',
`click` int(11) DEFAULT '0',
`nonlinearclickthrough` int(11) DEFAULT '0',
`companionclickthrough` int(11) DEFAULT '0',
KEY `date` (`date`),
KEY `tag_id` (`tag_id`),
KEY `domain` (`domain`),
KEY `country` (`country`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
请帮助我优化我的表格和查询;
答案 0 :(得分:0)
查询在WHERE子句中使用等于运算符tag_id = 185
和范围运算符date >=
:
WHERE
t.date >= "2017-08-14 00:00:00" AND
t.tag_id = 185
因此,要加快此同步查询,请在tag_id, t.date
上创建一个多列索引(按此特定顺序 - =
列的运算符必须位于索引中的第一个),即:
CREATE INDEX somename ON ( tag_id, t.date );