简体   繁体   English

Django:高效的数据库搜索

[英]Django: efficient database search

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. 因此,我有很多链接到User表的表。 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. 例如,他从下个星期四到星期五都可以使用,具有x / y / z技能,并且他已完成的所有合同平均获得4分。

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! 谢谢!

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. 不知道这种方法是否能解决所有4种情况的问题,但至少在第一种情况下应该可以帮助您-有效地查询用户数据。

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. 通常,我发现使用valuesvalues_list查询函数的速度更快,因为它会缩小实际SQL的SELECT部分的范围,因此您可以更快地获得结果。 Django docs regarding this. 有关此的Django文档

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. 还值得一提的是,从valuesvalues_list新开发版本开始,您可以查询任何类型的关系,包括many_to_one。

And finally you might find in_bulk also useful. 最后,您可能会发现in_bulk也很有用。 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. 如果执行复杂的查询,您可能会尝试使用valuesvalues_list首先查询某些模型的id,然后使用in_bulk更快地获取模型实例。 Django docs about that. Django文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM