I am need to retrieve records from two tables which are linked by a third. Session content doesn’t contain any information I need besides the keys to link the other two tables.
I am currently using these joins:
SELECT sessions.name, rooms.name, slots.type, slots.dayString, slots.startHour, slots.startMinute, slots.endHour, slots.endMinute FROM slots INNER JOIN sessions INNER JOIN rooms ;
It only takes 50 ms in SQLite DB browser, but takes multiple seconds of buffering when I call the related API endpoint.
I can’t use it if it takes this long to load Is there anyway to improve query time besides limiting the number of rows it returns (currently returns 77000)?
Advertisement
Answer
Session content doesn’t contain any information I need besides the keys to link the other two tables.
But you don’t use these keys to set the conditions to link the tables, so what you are actually doing is a CROSS
join of the 3 tables and the result is the Cartesian product of all the rows of all the tables.
Use an ON
clause for each join to set the conditions:
SELECT sn.name, r.name, s.type, s.dayString, s.startHour, s.startMinute, s.endHour, s.endMinute FROM slots s INNER JOIN sessions sn ON sn.slot_id = s.slot_id INNER JOIN rooms r ON r.room_id = sn.room_id;
Change the column names I used in the ON
clauses to the actual ones.