簡體   English   中英

使用索引的MySQL查詢需要很長時間

[英]Mysql query with indexes take a long time

我在mysql中有一個表,它有1100萬行; 我的選擇請求使用索引,位置,臨時和文件排序

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

請幫助我優化表和查詢;

該查詢在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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM