S
S
Sergey2015-11-19 16:47:49
MySQL
Sergey, 2015-11-19 16:47:49

How to concatenate the result of a MySQL one-to-many selection?

There is a table with tasks and there is a table with the fields of these tasks (Redmine database).
Now the sample is obtained in the following way:

select
  i.subject
  ,cv.value
  ,cv1.value
  ,cv2.value
from
  issues i
  left join custom_values cv on cv.custom_field_id=23 and cv.customized_id = i.id
  left join custom_values cv1 on cv1.custom_field_id=21 and cv1.customized_id = i.id
  left join custom_values cv2 on cv2.custom_field_id=24 and cv2.customized_id = i.id
where 1=1
  and t.id in (2,3,4,5)
  and cv.value = 'Да'

The request itself is slightly simplified so as not to litter.
The question is: how can joins be simplified so that fetching is faster?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-11-19
@disc

I will try to advise pseudo-code:
leave one join
and move part of the condition to WHERE
cv.custom_field_id IN (21,23,24)

I
Immortal_pony, 2015-11-19
@Immortal_pony

The query does not look like it could be simplified.
But to increase the speed of sampling, you can add an index on the custom_field + customized_id fields, if it does not already exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question