What is the most complex query that you are comfortable to live with before refactoring?
(Everyday Code – instead of keeping our knowledge in a README.md let’s share it with the internet)
Today I did some refactoring and I again worked with a query that I kind of never got to refactor and it is the most “complex” query that we seem to be comfortable with in our platforms because other queries are improved, but this one stays the same. This does not mean that it is not understandable or not maintainable, it is just the most “complex” we seem to be comfortable with, because any more complex than this and we naturally think about refactoring it.
This got me thinking.
What are other real life examples for the most complex relation database queries others are comfortable enough to live with before thinking about changing the model or the scheme.
Is my threshold too low or too high?
I would be happy to learn more from you.
Here is the query (Active Record) in question:
Get all the references for 'Tasks' that are in CourseSections that are in Courses and filter all of them through the translations they have These are 8 tables (ContentRef, Task, CourseSection, Course, and 4 translation tables)
# Get all the references for 'Tasks' that are in CourseSections that are in Courses
# and filter all of them through the translations they have
#
# These are 8 tables (ContentRef, Task, CourseSection, Course, and 4 translation tables)
ContentRef.
joins(:course_section).
includes(:translations).
includes(:task)
includes(task:[:translations]).
includes(course_section: [course: [:translations]]).
includes(course_section:[:translations]).
where(courses:{id: course_ids }, content_type: "Task").
select("content_refs.*, course_translations.*, course_section_translations.*, task_translations.*")
# The models is
Content Ref
belongs_to CourseSection
belongs_to Task
has_many :translations
CourseSection
belongs_to Course
has_many :translations
Course
has_many :translation
Task
has_many :translation
Reply
You must be logged in to post a comment.