1

I have 3 tables (MySQL):

  • Students
  • Groups
  • StudentsToGroups

All tables presented like models and have their own classes.

So, I want to show in view list of students with their group's names. Very simple example in Volt:

{% for student in students %}
  {{ student.name }}

  {% for student.getGroups() as group %}
    {{ group.name }}
  {% endfor %}
{% endfor %}

I guess that every time in nested loop will be JOIN and when students count is huge there will be performance issue.

How can I do this in more right way?

Denys Kurbatov
  • 143
  • 2
  • 10
  • Assuming that you're using a relational DB Engine, have you considered a query that join all the tables and then a single loop to present the data? – Claudio Jun 12 '19 at 09:41
  • 1
    @claudio yes, this one of possible variant, but I curious how other developers do that. Is big JOIN of 3 tables is the most performance right way? – Denys Kurbatov Jun 12 '19 at 09:46
  • I think a big JOIN will make your app faster rather the N queries with joins. I suggest you implement both solutions and measure the runtime of both to verify which one is best. Also for reference https://stackoverflow.com/questions/1067016/join-queries-vs-multiple-queries – Claudio Jun 12 '19 at 10:13
  • @claudio thank you for reference and suggestions. I will check out this. – Denys Kurbatov Jun 12 '19 at 10:59
  • One large query is much more efficient than multiple small queries. https://stackoverflow.com/a/37382695/8869589 – shn Jun 13 '19 at 16:26

0 Answers0