Menu

📰
0

Reddit - Please wait for verification

News and Notes on the Structured Query Language·/u/badboyzpwns·3 days ago
#ukMMs5on
Reading 0:00
15s threshold

For example here is the n+1 problem // 1 query to get all surveys const surveys = await db.query("SELECT * FROM surveys"); // then N queries — one per survey const results = await Promise.all( surveys.map(async (survey) => { const employee = await db.query( // hits DB once per survey ❌ "SELECT * FROM employees WHERE id = $1", [survey.employee_id] ); return { ...survey, employee }; }) ); One way to solve it is with JOIN const results = await db.query(` SELECT s.*, e.name, e.department FROM surveys s JOIN employees e ON e.id = s.employee_id `); or WHERE IN const employees = await db.query( `SELECT * FROM employees WHERE id IN (${employeeIds.map((_, i) => `$${i + 1}`).join(", ")})`, employeeIds ); Am I missing anything else? submitted by /u/badboyzpwns [link] [comments]

Read More