teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

表、クエリが見づらかったため修正

2020/05/19 12:14

投稿

Rio_1201
Rio_1201

スコア2

title CHANGED
File without changes
body CHANGED
@@ -16,6 +16,7 @@
16
16
  対象のSQL、実行計画、インデックス、テーブル構造を追記いたしました。
17
17
 
18
18
  -- 対象のSQL
19
+ ```mysql
19
20
  SELECT msd.groups as p_name,CHAR_LENGTH(msd.groups)+(LENGTH(msd.groups)-CHAR_LENGTH(msd.groups))/2 as namelen, COUNT(*) as cnt,SUM(af.gross) as hassei,msd.disp
20
21
  FROM `mail_settings_detail` msd , af_result af
21
22
  WHERE
@@ -25,32 +26,38 @@
25
26
  AND msd.status = 1
26
27
  AND af.status NOT IN ('XX','YY')
27
28
  GROUP BY msd.groups ORDER BY category , disp;
29
+ ```
28
30
 
31
+
29
32
  問題のインデックス:mail_settings_detailのstatus
30
33
 
31
34
  statusがvisibleの場合の実行計画
32
- id select_type table partitions type possible_keys key key_len ref rows filtered Extra
35
+ |id|select_type|table|partitions|type|possible_keys|key|key_len|ref|rows|filtered|Extra|
36
+ |:--|:--:|--:|
33
- 1 SIMPLE af range pgid,tag_time,status tag_time 4 35196 60 Using index condition; Using where; Using temporary; Using filesort
37
+ |1|SIMPLE|af||range|pgid,tag_time,status|tag_time|4||35196|60|Using index condition; Using where; Using temporary; Using filesort|
34
- 1 SIMPLE msd ref pgid,status pgid 4 DB.af.pgid 1 40 Using where
38
+ |1|SIMPLE|msd||ref|pgid,status|pgid|4|DB.af.pgid|1|40|Using where|
35
39
 
36
40
  statusがinvisibleの場合の実行計画
37
- id select_type table partitions type possible_keys key key_len ref rows filtered Extra
41
+ |id|select_type|table|partitions|type|possible_keys|key|key_len|ref|rows|filtered|Extra|
42
+ |:--|:--:|--:|
38
- 1 SIMPLE af range pgid,tag_time,status tag_time 4 35196 60 Using index condition; Using where; Using temporary; Using filesort
43
+ |1|SIMPLE|af||range|pgid,tag_time,status|tag_time|4||35196|60|Using index condition; Using where; Using temporary; Using filesort|
39
- 1 SIMPLE msd ref pgid pgid 4 DB.af.pgid 1 13.33 Using where
44
+ |1|SIMPLE|msd||ref|pgid|pgid|4|DB.af.pgid|1|13.33|Using where|
40
45
 
41
46
  statusのインデックスを削除した場合の実行計画
42
- id select_type table partitions type possible_keys key key_len ref rows filtered Extra
47
+ |id|select_type|table|partitions|type|possible_keys|key|key_len|ref|rows|filtered|Extra|
48
+ |:--|:--:|--:|
43
- 1 SIMPLE msd ALL pgid 651 4 Using where; Using temporary; Using filesort
49
+ |1|SIMPLE|msd||ALL|pgid||||651|4|Using where; Using temporary; Using filesort|
44
- 1 SIMPLE af ref pgid,tag_time,status pgid 4 DB.msd.pgid 1178 0.72 Using where
50
+ |1|SIMPLE|af||ref|pgid,tag_time,status|pgid|4|DB.msd.pgid|1178|0.72|Using where|
45
51
 
46
52
  対象テーブルのインデックス
47
- Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible
53
+ ```mysql
48
- mail_settings_detail 0 PRIMARY 1 rowid A 651 BTREE YES
54
+ CREATE INDEX pgid ON mail_settings_detail(pgid);
49
- mail_settings_detail 1 pgid 1 pgid A 526 BTREE YES
55
+ CREATE INDEX userid ON mail_settings_detail(userid);
50
- mail_settings_detail 1 userid 1 userid A 515 BTREE YES
51
- mail_settings_detail 1 mail_setid 1 mail_setid A 102 BTREE NO
56
+ CREATE INDEX mail_setid ON mail_settings_detail(mail_setid);
52
- mail_settings_detail 1 status 1 status A 3 BTREE YES
57
+ CREATE INDEX status ON mail_settings_detail(status);
58
+ ```
53
59
 
60
+
54
61
  statusのインデックスがvisibleの場合の実行時間
55
62
  0.302秒
56
63
 

1

対象のSQL、実行計画、インデックス、テーブル構造を追記いたしました。

2020/05/19 12:14

投稿

Rio_1201
Rio_1201

スコア2

title CHANGED
File without changes
body CHANGED
@@ -12,5 +12,72 @@
12
12
  invisibleにしているインデックスを削除することで
13
13
  参照系のクエリの実行時間に影響することはあり得るのでしょうか。
14
14
 
15
+ 追記
16
+ 対象のSQL、実行計画、インデックス、テーブル構造を追記いたしました。
17
+
18
+ -- 対象のSQL
19
+ SELECT msd.groups as p_name,CHAR_LENGTH(msd.groups)+(LENGTH(msd.groups)-CHAR_LENGTH(msd.groups))/2 as namelen, COUNT(*) as cnt,SUM(af.gross) as hassei,msd.disp
20
+ FROM `mail_settings_detail` msd , af_result af
21
+ WHERE
22
+ msd.category IN ('AAA','BBB','CCC','DDD')
23
+ AND af.tag_time BETWEEN '2020-05-01 00:00:00' AND '2020-05-15 23:59:59'
24
+ AND msd.pgid = af.pgid
25
+ AND msd.status = 1
26
+ AND af.status NOT IN ('XX','YY')
27
+ GROUP BY msd.groups ORDER BY category , disp;
28
+
29
+ 問題のインデックス:mail_settings_detailのstatus
30
+
31
+ statusがvisibleの場合の実行計画
32
+ id select_type table partitions type possible_keys key key_len ref rows filtered Extra
33
+ 1 SIMPLE af range pgid,tag_time,status tag_time 4 35196 60 Using index condition; Using where; Using temporary; Using filesort
34
+ 1 SIMPLE msd ref pgid,status pgid 4 DB.af.pgid 1 40 Using where
35
+
36
+ statusがinvisibleの場合の実行計画
37
+ id select_type table partitions type possible_keys key key_len ref rows filtered Extra
38
+ 1 SIMPLE af range pgid,tag_time,status tag_time 4 35196 60 Using index condition; Using where; Using temporary; Using filesort
39
+ 1 SIMPLE msd ref pgid pgid 4 DB.af.pgid 1 13.33 Using where
40
+
41
+ statusのインデックスを削除した場合の実行計画
42
+ id select_type table partitions type possible_keys key key_len ref rows filtered Extra
43
+ 1 SIMPLE msd ALL pgid 651 4 Using where; Using temporary; Using filesort
44
+ 1 SIMPLE af ref pgid,tag_time,status pgid 4 DB.msd.pgid 1178 0.72 Using where
45
+
46
+ 対象テーブルのインデックス
47
+ Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible
48
+ mail_settings_detail 0 PRIMARY 1 rowid A 651 BTREE YES
49
+ mail_settings_detail 1 pgid 1 pgid A 526 BTREE YES
50
+ mail_settings_detail 1 userid 1 userid A 515 BTREE YES
51
+ mail_settings_detail 1 mail_setid 1 mail_setid A 102 BTREE NO
52
+ mail_settings_detail 1 status 1 status A 3 BTREE YES
53
+
54
+ statusのインデックスがvisibleの場合の実行時間
55
+ 0.302秒
56
+
57
+ statusのインデックスがinvisibleの場合の実行時間
58
+ 0.215秒
59
+
60
+ statusのインデックス削除後の場合の実行時間
61
+ 10.115秒
62
+
63
+ mail_settings_detailテーブル
64
+ レコード数:651
65
+ rowid : int11 primarykey
66
+ pgid : int11
67
+ category : varchar30
68
+ groups : varchar30
69
+ disp : int11
70
+ status : int11
71
+ 他、本SQLに出てこないものが6カラム
72
+
73
+ af_resultテーブル
74
+ レコード数:290万
75
+ retstid : bigint20 primarykey
76
+ pgid : int11
77
+ gross : int11
78
+ tag_time : timestamp
79
+ status : enum
80
+ 他、本SQLに出てこないものが27カラム
81
+
15
82
  ### 補足情報(FW/ツールのバージョンなど)
16
83
  MySQL 8.0.17