Reputation: 1253
I need an efficient way to search through my models to find a specific User, here's a list,
User - list of users, their names, etc.
Events - table of events for all users, on when they're not available
Skills - many-to-many relationship with the User, a User could have a lot of skills
Contracts - many-to-one with User, a User could work on multiple contracts, each with a rating (if completed)
... etc.
So I got a lot of tables linked to the User table. I need to search for a set of users fitting certain criteria; for example, he's available from next Thurs through Fri, has x/y/z skills, and has received an average 4 rating on all his completed contracts.
Is there some way to do this search efficiently while minimizing the # of times I hit the database? Sorry if this is a very newb question.
Thanks!
Upvotes: 0
Views: 885
Reputation: 27861
Not sure if this method will solve you issue for all 4 cases, but at least it should help you out in the first one - querying users data efficiently.
I usually find using values
or values_list
query function faster because it slims down the SELECT
part of the actual SQL, and therefore you will get results faster. Django docs regarding this.
Also worth mentioning that starting with new dev version within values
and values_list
you can query any type of relationship, including many_to_one.
And finally you might find in_bulk
also useful. If I do a complex query, you might try to query the ids first of some models using values
or values_list
and then use in_bulk
to get the model instances faster. Django docs about that.
Upvotes: 1