質問編集履歴

4

修正

2019/01/07 07:37

投稿

riomakopa
riomakopa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -86,13 +86,13 @@
86
86
 
87
87
  SQL
88
88
 
89
- ```
89
+
90
90
 
91
91
  explain analyze
92
92
 
93
-
93
+ ```
94
-
94
+
95
- WITH tenant_post_detail_list AS
95
+ WITH tenant_post_detail_list AS
96
96
 
97
97
  (SELECT
98
98
 
@@ -334,8 +334,6 @@
334
334
 
335
335
  ON reply_check.max_reply_message_id = mm.message_id
336
336
 
337
-
338
-
339
337
  WHERE
340
338
 
341
339
  mrc.del_flg = 0
@@ -354,4 +352,4 @@
354
352
 
355
353
  ,mm.message_id ASC
356
354
 
357
- ```
355
+ ```

3

修正

2019/01/07 07:37

投稿

riomakopa
riomakopa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  考え方を教えて頂きたいです。
4
4
 
5
+ ゴールは「適切にINDEXが張られているか」と
6
+
7
+ どこでコストが掛かっているのかを解明したいです。
8
+
5
9
 
6
10
 
7
11
  クエリで一番コストがかかっているところは
@@ -42,13 +46,11 @@
42
46
 
43
47
  ↑の中に
44
48
 
45
- ```
46
-
47
49
  「値のシーケンシャルI/Oで1ページを読みこむコストを1とした際の相対値で示される」とあります
48
50
 
49
51
 
50
52
 
51
- 全文
53
+
52
54
 
53
55
  ```
54
56
 
@@ -72,120 +74,284 @@
72
74
 
73
75
  -> Nested Loop (cost=268.15..1182.94 rows=1 width=52) (actual time=11.120..35.718 rows=664 loops=1) __**
74
76
 
75
- Join Filter: (ct.revision = c.revision)
76
-
77
- Rows Removed by Join Filter: 97
78
-
79
- -> Hash Join (cost=267.88..348.99 rows=179 width=82) (actual time=11.067..14.269 rows=761 loops=1)
80
-
81
- Hash Cond: (((t.tenant_code)::text = (u_1.tenant_code)::text) AND (t.revision = ct.revision))
82
-
83
- -> Seq Scan on m_tenant t (cost=0.00..73.61 rows=761 width=19) (actual time=0.005..0.925 rows=761 loops=1)
84
-
85
- -> Hash (cost=255.59..255.59 rows=819 width=63) (actual time=11.048..11.048 rows=761 loops=1)
86
-
87
- Buckets: 1024 Batches: 1 Memory Usage: 75kB
88
-
89
- -> Hash Join (cost=42.12..255.59 rows=819 width=63) (actual time=2.167..9.805 rows=761 loops=1)
90
-
91
- Hash Cond: ((u_1.tenant_code)::text = (ct.tenant_code)::text)
92
-
93
- -> Seq Scan on m_user u_1 (cost=0.00..179.95 rows=2895 width=14) (actual time=0.019..2.987 rows=2895 loops=1)
94
-
95
- -> Hash (cost=32.61..32.61 rows=761 width=49) (actual time=2.134..2.134 rows=761 loops=1)
96
-
97
- Buckets: 1024 Batches: 1 Memory Usage: 63kB
98
-
99
- -> Seq Scan on m_contract ct (cost=0.00..32.61 rows=761 width=49) (actual time=0.014..1.136 rows=761 loops=1)
100
-
101
- -> Index Scan using m_contract_idx_user_id on m_contract c (cost=0.28..4.65 rows=1 width=18) (actual time=0.021..0.024 rows=1 loops=761)
102
-
103
- Index Cond: ((user_id)::text = (u_1.user_id)::text)
104
-
105
- Filter: (((u_1.tenant_code)::text = (tenant_code)::text) AND (revision = (SubPlan 2)))
106
-
107
- Rows Removed by Filter: 0
108
-
109
- SubPlan 2
110
-
111
- -> Result (cost=4.29..4.30 rows=1 width=0) (actual time=0.010..0.010 rows=1 loops=1025)
112
-
113
- InitPlan 1 (returns $1)
114
-
115
- -> Limit (cost=0.28..4.29 rows=1 width=4) (actual time=0.006..0.007 rows=1 loops=1025)
116
-
117
- -> Index Only Scan Backward using m_contract_pkey on m_contract (cost=0.28..4.29 rows=1 width=4) (actual time=0.004..0.004 rows=1 loops=1025)
118
-
119
- Index Cond: ((tenant_code = (c.tenant_code)::text) AND (revision IS NOT NULL))
120
-
121
- Heap Fetches: 0
122
-
123
- -> Hash Join (cost=8.44..14.22 rows=1 width=10) (actual time=0.174..0.299 rows=1 loops=664)
124
-
125
- Hash Cond: (((m_compartment.compartment_id)::text = (cp.compartment_id)::text) AND ((max(m_compartment.use_to)) = cp.use_to))
126
-
127
- -> HashAggregate (cost=8.23..10.33 rows=210 width=11) (actual time=0.002..0.140 rows=210 loops=664)
128
-
129
- Group Key: m_compartment.compartment_id
130
-
131
- -> Seq Scan on m_compartment (cost=0.00..7.18 rows=210 width=11) (actual time=0.004..0.322 rows=210 loops=1)
132
-
133
- Filter: (('2019-01-07'::date >= use_from) AND ('2019-01-07'::date <= use_to))
134
-
135
- Rows Removed by Filter: 2
136
-
137
- -> Hash (cost=0.19..0.19 rows=1 width=14) (actual time=0.009..0.009 rows=1 loops=664)
138
-
139
- Buckets: 1024 Batches: 1 Memory Usage: 1kB
140
-
141
- -> Index Scan using m_compartment_pkey on m_compartment cp (cost=0.14..0.19 rows=1 width=14) (actual time=0.004..0.005 rows=1 loops=664)
142
-
143
- Index Cond: ((ct.compartment_id)::text = (compartment_id)::text)
144
-
145
- -> Index Scan using m_user_pkey on m_user u (cost=0.28..1.19 rows=1 width=12) (actual time=0.005..0.006 rows=1 loops=664)
146
-
147
- Index Cond: ((user_id)::text = (ct.user_id)::text)
148
-
149
- -> Index Scan using m_zone_pkey on m_zone z (cost=0.14..0.21 rows=1 width=25) (actual time=0.003..0.004 rows=1 loops=664)
150
-
151
- Index Cond: ((cp.zone_id)::text = (zone_id)::text)
152
-
153
- -> Index Scan using m_category_pkey on m_category cg (cost=0.14..0.18 rows=1 width=24) (actual time=0.003..0.005 rows=1 loops=664)
154
-
155
- Index Cond: ((t.category_id1)::text = (category_id)::text)
156
-
157
- Filter: (category_kbn = 1)
158
-
159
- -> Seq Scan on m_post p (cost=0.00..1.91 rows=13 width=20) (actual time=0.010..0.077 rows=14 loops=1)
160
-
161
- Filter: ((del_flg = 0) AND (((sc_id)::text = 'marinoa'::text) OR ((sc_id)::text = 'ALL'::text)))
162
-
163
- Rows Removed by Filter: 38
164
-
165
- -> Hash Left Join (cost=4635.00..9786.84 rows=17 width=1394) (actual time=35.896..3585.607 rows=3790 loops=1)
166
-
167
- Hash Cond: (mm.message_id = reply_check.max_reply_message_id)
168
-
169
- -> Nested Loop Left Join (cost=133.38..5284.81 rows=17 width=1390) (actual time=27.238..3567.075 rows=3790 loops=1)
170
-
171
- -> Nested Loop (cost=133.11..5279.44 rows=17 width=1386) (actual time=27.226..3547.307 rows=3790 loops=1)
172
-
173
- -> Nested Loop (cost=132.68..4502.88 rows=115 width=1370) (actual time=27.183..3164.768 rows=24443 loops=1)
174
-
175
- -> CTE Scan on tenant_post_detail_list tpl (cost=0.00..0.42 rows=1 width=102) (actual time=12.285..256.152 rows=677 loops=1)
176
-
177
- Filter: ((((post_id)::text <> 'tenant'::text) AND ((user_id)::text = ' '::text)) OR (((post_id)::text = 'tenant'::text) AND ((user_id)::text <> ' '::text)))
178
-
179
- Rows Removed by Filter: 1
180
-
181
- -> Bitmap Heap Scan on t_message_mng mm (cost=132.68..4502.30 rows=16 width=1338) (actual time=3.181..4.243 rows=36 loops=677)
182
-
183
-
184
-
185
-
77
+
186
78
 
187
79
  Planning time: 13.235 ms
188
80
 
189
81
  Execution time: 3611.990 ms
190
82
 
191
83
  ```
84
+
85
+
86
+
87
+ SQL
88
+
89
+ ```
90
+
91
+ explain analyze
92
+
93
+
94
+
95
+ WITH tenant_post_detail_list AS
96
+
97
+ (SELECT
98
+
99
+ u.post_id AS post_id
100
+
101
+ ,u.user_id AS user_id
102
+
103
+ ,ct.tenant_code AS tenant_code
104
+
105
+ ,ct.tenant_name AS tenant_post_name
106
+
107
+ ,z.zone_id AS zone_id
108
+
109
+ ,z.zone_name AS zone_name
110
+
111
+ ,cg.category_id AS category_id
112
+
113
+ ,cg.category_name AS category_name
114
+
115
+ FROM
116
+
117
+ .v_max_revision_all_tenant AS mrt
118
+
119
+ INNER JOIN .m_contract AS ct
120
+
121
+ ON mrt.tenant_code = ct.tenant_code
122
+
123
+ AND mrt.revision = ct.revision
124
+
125
+ INNER JOIN .m_tenant AS t
126
+
127
+ ON mrt.tenant_code = t.tenant_code
128
+
129
+ AND mrt.revision = t.revision
130
+
131
+ INNER JOIN m_user AS u
132
+
133
+ ON ct.user_id = u.user_id
134
+
135
+ LEFT OUTER JOIN
136
+
137
+ ( SELECT
138
+
139
+ cp.compartment_id
140
+
141
+ ,cp.zone_id
142
+
143
+ ,cp.floor_id
144
+
145
+ FROM
146
+
147
+ .m_compartment AS cp
148
+
149
+ INNER JOIN (
150
+
151
+ SELECT
152
+
153
+ compartment_id
154
+
155
+ ,MAX(use_to) AS max_use_to
156
+
157
+ FROM
158
+
159
+ .m_compartment
160
+
161
+ WHERE 'now' BETWEEN use_from AND use_to
162
+
163
+ GROUP BY compartment_id
164
+
165
+ ) AS cp2
166
+
167
+ ON cp2.compartment_id=cp.compartment_id
168
+
169
+ AND cp2.max_use_to=cp.use_to
170
+
171
+ ) AS cp
172
+
173
+ ON ct.compartment_id = cp.compartment_id
174
+
175
+ LEFT OUTER JOIN .m_zone AS z
176
+
177
+ ON cp.zone_id = z.zone_id
178
+
179
+ LEFT OUTER JOIN .m_category AS cg
180
+
181
+ ON t.category_id1 = cg.category_id
182
+
183
+ AND cg.category_kbn = 1
184
+
185
+
186
+
187
+ UNION ALL
188
+
189
+
190
+
191
+ SELECT
192
+
193
+ p.post_id AS post_id
194
+
195
+ ,' ' AS user_id
196
+
197
+ ,' ' AS tenant_code
198
+
199
+ ,p.post_name AS tenant_post_name
200
+
201
+ ,' ' AS zone_id
202
+
203
+ ,' ' AS zone_name
204
+
205
+ ,' ' AS category_id
206
+
207
+ ,' ' AS category_name
208
+
209
+ FROM m_post AS p
210
+
211
+ WHERE
212
+
213
+ (p.sc_id = 'marinoa' OR p.sc_id = 'ALL')
214
+
215
+ AND p.del_flg = 0
216
+
217
+ )
218
+
219
+
220
+
221
+ SELECT
222
+
223
+ mm.*
224
+
225
+ ,mrc.message_receive_id
226
+
227
+ ,mrc.reg_time AS receive_date
228
+
229
+ ,tpl.tenant_post_name
230
+
231
+ ,mrp.reply_no
232
+
233
+ ,(CASE
234
+
235
+ WHEN mm.confirm_req_flg = 1
236
+
237
+ AND mrc.confirm_flg = 0
238
+
239
+ THEN 0
240
+
241
+ WHEN mm.confirm_req_flg = 0
242
+
243
+ AND mrc.read_flg = 0
244
+
245
+ THEN 1
246
+
247
+ WHEN
248
+
249
+ (mm.confirm_req_flg = 1
250
+
251
+ AND mrc.confirm_flg = 1)
252
+
253
+ OR
254
+
255
+ (mm.confirm_req_flg = 0
256
+
257
+ AND mrc.read_flg = 1)
258
+
259
+ THEN 2
260
+
261
+ END
262
+
263
+ ) AS state
264
+
265
+ ,reply_check.max_reply_message_id IS NOT NULL AS replied_flg
266
+
267
+
268
+
269
+ FROM
270
+
271
+ .t_message_receive AS mrc
272
+
273
+ INNER JOIN .t_message_mng AS mm
274
+
275
+ ON mm.message_id = mrc.message_id
276
+
277
+ AND
278
+
279
+ ((mm.span_flg =1
280
+
281
+ AND mm.span_from <= '01/07/2019 00:00:00.000'
282
+
283
+ AND mm.span_to >= '01/07/2019 00:00:00.000')
284
+
285
+ OR
286
+
287
+ mm.span_Flg = 0)
288
+
289
+ INNER JOIN tenant_post_detail_list tpl
290
+
291
+ ON
292
+
293
+ (tpl.post_id = mm.post_id
294
+
295
+ AND tpl.post_id <> 'tenant'
296
+
297
+ AND tpl.user_id = ' ')
298
+
299
+ OR
300
+
301
+ (tpl.user_id = mm.user_id
302
+
303
+ AND tpl.post_id = 'tenant'
304
+
305
+ AND tpl.user_id <> ' ')
306
+
307
+ LEFT OUTER JOIN .t_message_reply AS mrp
308
+
309
+ ON mrp.message_receive_id = mrc.message_receive_id
310
+
311
+ AND mrp.reply_no = 1
312
+
313
+ LEFT OUTER JOIN
314
+
315
+ (SELECT max(reply_message_id) AS max_reply_message_id
316
+
317
+ FROM .t_message_mng
318
+
319
+ WHERE
320
+
321
+ reply_message_id IS NOT NULL
322
+
323
+
324
+
325
+
326
+
327
+ AND post_id = '20100'
328
+
329
+
330
+
331
+ GROUP BY reply_message_id
332
+
333
+ ) AS reply_check
334
+
335
+ ON reply_check.max_reply_message_id = mm.message_id
336
+
337
+
338
+
339
+ WHERE
340
+
341
+ mrc.del_flg = 0
342
+
343
+ AND mrc.post_id = '20100'
344
+
345
+ AND mm.message_name LIKE '%%' ESCAPE '~'
346
+
347
+ AND mm.note LIKE '%%' ESCAPE '~'
348
+
349
+ ORDER BY
350
+
351
+
352
+
353
+ mrc.reg_time DESC
354
+
355
+ ,mm.message_id ASC
356
+
357
+ ```

2

修正

2019/01/07 07:35

投稿

riomakopa
riomakopa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,10 @@
9
9
  以下の所だと思っているのですが
10
10
 
11
11
  あってますでしょうか?
12
+
13
+
14
+
15
+
12
16
 
13
17
  ```
14
18
 
@@ -31,6 +35,16 @@
31
35
  (コストは1以上あってはならないという認識があります)
32
36
 
33
37
  結果的にcost=10988.02..10988.07 になっているように思います。
38
+
39
+ こちらは、先輩方に教えていただいたことですが
40
+
41
+ https://lets.postgresql.jp/sites/default/files/2016-11/Explaining_Explain_ja.pdf
42
+
43
+ ↑の中に
44
+
45
+ ```
46
+
47
+ 「値のシーケンシャルI/Oで1ページを読みこむコストを1とした際の相対値で示される」とあります
34
48
 
35
49
 
36
50
 

1

追記

2019/01/07 07:30

投稿

riomakopa
riomakopa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,36 @@
1
1
  やりたいことはクエリコストの改善なのですが
2
2
 
3
3
  考え方を教えて頂きたいです。
4
+
5
+
6
+
7
+ クエリで一番コストがかかっているところは
8
+
9
+ 以下の所だと思っているのですが
10
+
11
+ あってますでしょうか?
12
+
13
+ ```
14
+
15
+ __Append (cost=277.15..1200.83 rows=14 width=204) (actual time=12.277..253.460 rows=678 loops=1)
16
+
17
+ -> Nested Loop Left Join (cost=277.15..1198.78 rows=1 width=93) (actual time=12.275..252.529 rows=664 loops=1)
18
+
19
+ -> Nested Loop Left Join (cost=277.01..1198.60 rows=1 width=76) (actual time=12.266..247.347 rows=664 loops=1)
20
+
21
+ -> Nested Loop (cost=276.87..1198.38 rows=1 width=54) (actual time=12.257..242.928 rows=664 loops=1)
22
+
23
+ -> Nested Loop Left Join (cost=276.59..1197.17 rows=1 width=48) (actual time=12.240..236.827 rows=664 loops=1)
24
+
25
+ -> Nested Loop (cost=268.15..1182.94 rows=1 width=52) (actual time=11.120..35.718 rows=664 loops=1) __**
26
+
27
+ ```
28
+
29
+ 理由は、costが200以上掛かっているためです。
30
+
31
+ (コストは1以上あってはならないという認識があります)
32
+
33
+ 結果的にcost=10988.02..10988.07 になっているように思います。
4
34
 
5
35
 
6
36
 
@@ -136,25 +166,7 @@
136
166
 
137
167
  -> Bitmap Heap Scan on t_message_mng mm (cost=132.68..4502.30 rows=16 width=1338) (actual time=3.181..4.243 rows=36 loops=677)
138
168
 
139
- Recheck Cond: (((tpl.post_id)::text = (post_id)::text) OR ((tpl.user_id)::text = (user_id)::text))
140
-
141
- Filter: (((message_name)::text ~~ '%%'::text) AND ((note)::text ~~ '%%'::text) AND (((span_flg = 1) AND (span_from <= '2019-01-07'::date) AND (span_to >= '2019-01-07'::date)) OR (span_flg = 0)) AND ((((tpl.post_id)::text = (post_id)::text) AND ((tpl.post_id)::text <> 'tenant'::text) AND ((tpl.user_id)::text = ' '::text)) OR (((tpl.user_id)::text = (user_id)::text) AND ((tpl.post_id)::text = 'tenant'::text) AND ((tpl.user_id)::text <> ' '::text))))
142
-
143
- Rows Removed by Filter: 2690
144
-
145
- Heap Blocks: exact=949070
146
-
147
- -> BitmapOr (cost=132.68..132.68 rows=3214 width=0) (actual time=0.406..0.406 rows=0 loops=677)
148
-
149
- -> Bitmap Index Scan on t_message_mng_idx_post_id (cost=0.00..127.57 rows=3104 width=0) (actual time=0.353..0.353 rows=2327 loops=677)
150
-
151
- Index Cond: ((tpl.post_id)::text = (post_id)::text)
152
-
153
- -> Bitmap Index Scan on t_message_mng_idx_user_id (cost=0.00..5.10 rows=109 width=0) (actual time=0.049..0.049 rows=435 loops=677)
154
-
155
- Index Cond: ((tpl.user_id)::text = (user_id)::text)
156
-
157
-
169
+
158
170
 
159
171
 
160
172