# Optimize get childrens from token only

**URL:** <https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611>\
**Category:** Server Side Swift with Vapor\
**Created:** [March 15, 2019, 2:48pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611 "2019-03-15T14:48:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![choioi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/choioi/32/102075_2.png) [@choioi](https://forums.kodeco.com/u/choioi)\
**Post date:** [March 15, 2019, 2:48pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/1 "2019-03-15T14:48:25Z")

</div>

My tables have : User , Token and Pets…  
a User have alot Pets.  
a User have only 1 token, Token and Pet is FK with User via ID

My logic is want if user sent token string to server and i’ll get all pet of user.  
so My code is  
 ![image](https://files.chunter.kodeco.com/original/3X/9/f/9f3df076a682cb00d72647ba6500e8ea02d009cc.png)

and result is working for me

![image](https://files.chunter.kodeco.com/original/3X/b/0/b0785ae0910da89a44e25a7c6bc825049f96c1a0.png)

But im newbie at backend, I want to know if there is another way to handle this problem

1.Token table is :  
final class Token: Codable {  
var id: Int?  
var token: String  
var userID: User.ID // FK  
…  
}  
extension Token {  
var user: Parent\<Token, User\> {  
return parent(\Token.userID)  
}  
}  
1.User table is :  
final class User: Codable {  
var id: Int?  
…  
}  
extension User {  
var pets: Children\<User, Pet\> {  
return children(.userID)  
}  
}

3.Pet table is :

final class Pet: Codable {  
var id: Int?  
var name: String  
var userID: User.ID  
init(name: String, userID: User.ID) {  
self.name = name  
self.userID = userID  
}  
}  
extension Pet {  
var user: Parent\<Pet, User\> {  
return parent(\Pet.userID)  
}  
}

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 15, 2019, 2:53pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/2 "2019-03-15T14:53:39Z")

</div>

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [March 15, 2019, 3:45pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/3 "2019-03-15T15:45:50Z")

</div>

@choioi what you’re hitting there is the N+1 problem, which is a really difficult problem to solve. One way to do it is with [this gist](https://gist.github.com/vzsg/0a05c681db27c63e4841b638c30108db). Hopefully native Fluent support will come in the future

---

<div class="post-metadata">

**Author:** ![choioi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/choioi/32/102075_2.png) [@choioi](https://forums.kodeco.com/u/choioi)\
**Post date:** [March 17, 2019, 9:57am UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/4 "2019-03-17T09:57:59Z")

</div>

I want to confirm some @0xtim :

1. In my case, are you sure is N+1 , because I only get the pet list from a specified user, not all users.?, and in my model user. my petsis Computed Properties only. not var pets = [Pet]  
 ![image](https://files.chunter.kodeco.com/original/3X/e/0/e0d2f83bb83690927de16b7868c0c42065beffd1.png)

2. what differrent from method 1 and method 2, same both of them same performance, i tested with 10k row pets for 2 user, and want get list of user 1 , my log is showing about 60ms for the 1st, and about 18ms for the second.

I thought that when using a foreign key, there would be advantages when accessing it rather than filter-based access

![image](https://files.chunter.kodeco.com/original/3X/2/f/2f3d7e11649aa1c7d3018800951cd9f27159e8f1.png)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 17, 2019, 2:59pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/5 "2019-03-17T14:59:58Z")

</div>

@0xtim Do you have any feedback about this? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![michaeltansg](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/michaeltansg/32/81494_2.png) [@michaeltansg](https://forums.kodeco.com/u/michaeltansg)\
**Post date:** [March 18, 2019, 12:45pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/6 "2019-03-18T12:45:58Z")

</div>

Hi @choioi

If your requirement is to use the token as a parameter in the request instead of the user as a URL parameter, then I would probably use an in-memory cache to store the token/user-id key-value pair when the user logs in.

---

<div class="post-metadata">

**Author:** ![choioi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/choioi/32/102075_2.png) [@choioi](https://forums.kodeco.com/u/choioi)\
**Post date:** [March 18, 2019, 2:59pm UTC](https://forums.kodeco.com/t/optimize-get-childrens-from-token-only/64611/7 "2019-03-18T14:59:29Z")

</div>

Thanks for your suggestion, I will try the cache to see the results @michaeltansg
