Hi, I'm developing a site and I have some doubts about the best approach to query the database. In general, when I load a page, several data has to be fetched from the database. I'm not sure if I'm doing it the best way. So right now and as an example I call the following functions when the page is rendered:
(...)
(a) -> getAuthorName() // 1 db connection - 1 db query
(b) -> getAuthorLocation() // 1 db connection - 1 db query
(consider that here it could come more info the author....)
(...)
(c) count<- countNumberBooks() // 1 db connection - 1 db query
(d) if(count == 0)
(e)//do something
else
(f) books<- getBooksByAuthor() // 1 db connection - 1 db query
(g) renderBooks...
Given this example, my questions are:
- Is it better to do (a) and (b) in one single function in order to have only 1 query to the database?
- Is it better to do (f), count the result and then either do (e) or (g), and in this way getting rid of (c)?
- Or, is it better to load everything we need from the database in one step at the beggining, fetch the data into some kind of structure, and then read it as we render the page?
Thanks!