From the course: Advanced SQL for Application Development

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Avoiding N+1 queries

Avoiding N+1 queries

From the course: Advanced SQL for Application Development

Start my 1-month free trial

Avoiding N+1 queries

- Now starting again from where we left off, one of the things we often have to do, is look up data from more than one table. Now there are a couple of different ways of doing that. Now a common way, that people who are maybe new to SQL will do, is to fetch information from one table, save that information in a variable, loop over it, and then go get information from the second table. Let's see how that would work using our all ovens. So suppose we have our list of all ovens and now let's say we want to get information from product orders, to see what orders include ovens. One way to do that, is to essentially just loop over the, all the items in all ovens. So let's see how that would look. First of all, I'm going to create a cursor conn.cursor and I'm going to have a local variable here called fetch, all count, which I'm going to use just to print out, to show how many times we're going to the database. And…

Contents