回答編集履歴

2

update

2020/02/11 23:47

投稿

amura
amura

スコア333

test CHANGED
@@ -146,7 +146,7 @@
146
146
 
147
147
  ```
148
148
 
149
- メモリ消費軽減SQLを作成してみました
149
+ メモリ消費軽減SQLを作成してみました
150
150
 
151
151
  ```sql
152
152
 

1

update

2020/02/11 23:47

投稿

amura
amura

スコア333

test CHANGED
@@ -145,3 +145,169 @@
145
145
 
146
146
 
147
147
  ```
148
+
149
+ メモリを消費軽減SQLを作成してみました
150
+
151
+ ```sql
152
+
153
+ select
154
+
155
+ case
156
+
157
+ when actions.target_type='user' and actions.target_country_code='jp'
158
+
159
+ then users.user_id
160
+
161
+ else contents.content_id
162
+
163
+ end as content_id
164
+
165
+ , case
166
+
167
+ when actions.target_type='user' and actions.target_country_code='jp'
168
+
169
+ then users.nick_name
170
+
171
+ else null
172
+
173
+ end as nick_name
174
+
175
+ , case
176
+
177
+ when actions.target_type='thread' and actions.target_country_code='jp'
178
+
179
+ then threads.thread_text
180
+
181
+ else null
182
+
183
+ end as thread_text
184
+
185
+ , case
186
+
187
+ when (actions.target_type='user' and actions.target_country_code='jp') or actions.target_type='thread'
188
+
189
+ then null
190
+
191
+ else contents.content_title
192
+
193
+ end as content_title
194
+
195
+ , case
196
+
197
+ when (actions.target_type='user' and actions.target_country_code='jp') or actions.target_type='thread'
198
+
199
+ then actions.target_type
200
+
201
+ else content_type
202
+
203
+ end as content_type
204
+
205
+ , case
206
+
207
+ when (actions.target_type='user' and actions.target_country_code='jp') or actions.target_type='thread'
208
+
209
+ then actions.target_country_code
210
+
211
+ else contents.country_code
212
+
213
+ end as country_code
214
+
215
+ , max( case when
216
+
217
+ actions.action_name='follow' then actions.action_date end
218
+
219
+ ) as follow_date
220
+
221
+ , max( case when
222
+
223
+ actions.action_name='favorite' then actions.action_date end
224
+
225
+ ) as favorite_date
226
+
227
+ , jp_voices.voice_id
228
+
229
+ , jp_voices.thread_id
230
+
231
+ , jp_voices.parent_id
232
+
233
+ , jp_voices.voice_type
234
+
235
+ , jp_voices.voice_text
236
+
237
+ , jp_voices.up
238
+
239
+ , jp_voices.down
240
+
241
+ from jp_actions actions
242
+
243
+ # join
244
+
245
+ left join jp_threads threads
246
+
247
+ on actions.target_id=threads.thread_id
248
+
249
+ left join jp_users users
250
+
251
+ on actions.target_id=users.user_id
252
+
253
+ left join country_contents contents
254
+
255
+ on actions.target_id=contents.content_id
256
+
257
+ join jp_voices
258
+
259
+ on jp_voices.attach_id=actions.target_id
260
+
261
+ and jp_voices.attach_type=case
262
+
263
+ when (actions.target_type='user' and actions.target_country_code='jp') or actions.target_type='thread'
264
+
265
+ then actions.target_type else content_type end
266
+
267
+ and jp_voices.attach_country=case
268
+
269
+ when (actions.target_type='user' and actions.target_country_code='jp') or actions.target_type='thread'
270
+
271
+ then actions.target_country_code
272
+
273
+ else contents.country_code
274
+
275
+ end
276
+
277
+ and jp_voices.thread_id=1 and jp_voices.voice_type='comment'
278
+
279
+
280
+
281
+ # 彼がactor_idであるfavoriteを指定
282
+
283
+ where actions.actor_id=1
284
+
285
+ # favorite_dateとfollow_dateが欲しいので以下を指定
286
+
287
+ and actions.action_name in ('favorite', 'follow')
288
+
289
+ and exists(
290
+
291
+ select 0 from jp_actions
292
+
293
+ where target_id=actions.target_id and action_name='favorite'
294
+
295
+ )
296
+
297
+ # 彼とblock関係にあるjpのuserを除外
298
+
299
+ and not exists(
300
+
301
+ select 0 from jp_actions
302
+
303
+ where target_type='user' and target_country_code='jp' and action_name='block'
304
+
305
+ and target_id=actions.target_id and actions.target_type='user'
306
+
307
+ and actor_id=1
308
+
309
+ )
310
+
311
+ group by 1,2,3,4,5,6,9,10,11,12,13,14,15;
312
+
313
+ ```