2017-08-15 349 views
0

我在mysql中有一個表,它有11百萬行; 我的選擇請求使用索引,其中,臨時和文件排序帶索引的Mysql查詢需要很長時間

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 

請幫我優化我的表和查詢;

+2

請一)張貼創建索引(ES),你認爲它應該被使用和b)決定你實際上使用的是什麼數據庫語句 - 你真棒在mysql上,所以請刪除標籤[oracle]和[postgres]或解釋爲什麼他們在那裏 –

+0

yoyu使用MySQL,Oracle或Postgresql?不要標記不涉及的產品。 – jarlh

+0

向我們提供表格索引的結果。請同時提供您使用索引的colomn的基數。 – Noob

回答

0

該查詢使用一個等於操作員tag_id = 185和WHERE子句中的範圍內操作員date >=

WHERE 
    t.date >= "2017-08-14 00:00:00" AND 
    t.tag_id = 185 

因此,爲了加快這一conrete查詢,上tag_id, t.date創建多列索引(在這個特定的順序 - 柱=操作人員必須先在指數),即:

CREATE INDEX somename ON (tag_id, t.date); 
+0

並不總是最好的選擇。當您在查詢中選擇範圍運算符時更好地避免在該colomn上創建索引。所以我建議他需要提供額外的信息,然後才能改進查詢和表格。 – Noob