Performance tips

This commit is contained in:
Gwendal Roué committed 2016-08-01 18:38:23 +02:00
1 parent c49506e588
commit 4cd0334ed1
1 file changed
+2 -2
+2 -2
View File
@@ -3557,7 +3557,7 @@ The following code is inefficient. It is an example of the [N+1 problem](http://
```swift
// SELECT * FROM authors
let authors = Author.findAll(db)
let authors = Author.fetchAll(db)
for author in authors {
// SELECT COUNT(*) FROM books WHERE authorId = ...
author.bookCount = Book.filter(authorIdColumn == author.id).fetchCount(db)
@@ -3572,7 +3572,7 @@ let sql = "SELECT authors.*, COUNT(books.id) AS bookCount " +
"LEFT JOIN books ON books.authorId = authors.id " +
"GROUP BY authors.id " +
"ORDER BY authors.name"
let authors = Author.findAll(db, sql)
let authors = Author.fetchAll(db, sql)
```
In the example above, consider extending your Author with an extra bookCount property, or define and use a different type.