Django系列 5:ORM之prefetch_related 方法

Django之Model操作之prefetch_related的应用

参考博客: Django操作进阶与查询优化

对于多对多字段(ManyToManyField)和一对多字段,可以使用prefetch_related()来进行优化。

作用和方法:

prefetch_related()和select_related()的设计目的很相似,都是为了减少SQL查询的数量,但是实现的方式不一样。后者是通过JOIN语句,在SQL查询内解决问题。

但是对于多对多关系,使用SQL语句解决就显得有些不太明智,因为JOIN得到的表将会很长,会导致SQL语句运行时间的增加和内存占用的增加。若有n个对象,每个对象的多对多字段对应Mi条,就会生成Σ(n)Mi 行的结果表。


关于prefetch_related 使用方法的不同造成 SQL查询语句的不同:

1: 不带任何参数, model.objects.prefetch_related().all()

假设查询的结果有n个 Person 对象

a:多对多,  这种情况,会造成若干次查询,那么共查询1 + n 次SQL(本例查询有2个 Person 对象),除了第一次查询 Person对应的表,其他的SQL使用了 JOIN ON 来对关联表( orm_practice_person_visitation )查询

b:直接外键, 这种情况,会造成若1 + n次查询, 除了第一次查询,其他次都是正常的where id=xxx 的普通查询

c: 外键的外键,这种情况,会造成若1 + 2 * n次查询, 除了第一次查询,其他每个对象都会单独查询2次(外键与外键的外键关联表)的where id=xxx 的普通查询

查询1:多对多

person_objs = Person.objects.prefetch_related().filter(firstname__contains='兰').all()  

# person_objs = Person.objects.prefetch_related().all()

for p in person_objs:

            for i in p.visitation.all():    #  多对多

                        print(i.name)

[2021-11-06 13:09:02,797] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:09:02,799] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` INNER JOIN `orm_practice_person_visitation` ON (`orm_practice_city`.`id` = `orm_practice_person_visitation`.`city_id`) WHERE `orm_practice_person_visitation`.`person_id` = 13; args=(13,)

[2021-11-06 13:09:02,800] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` INNER JOIN `orm_practice_person_visitation` ON (`orm_practice_city`.`id` = `orm_practice_person_visitation`.`city_id`) WHERE `orm_practice_person_visitation`.`person_id` = 17; args=(17,)

查询2:外键

person_objs = Person.objects.prefetch_related().filter(firstname__contains='兰')

for p in person_objs:

            print(p.hometown.name)

            # print(p.needs.orderinfo)

[2021-11-06 13:42:37,332] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:42:37,333] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_order`.`id`, `orm_practice_order`.`customer_id`, `orm_practice_order`.`orderinfo`, `orm_practice_order`.`time` FROM `orm_practice_order` WHERE `orm_practice_order`.`id` = 23 LIMIT 21; args=(23,)

[2021-11-06 13:42:37,334] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_order`.`id`, `orm_practice_order`.`customer_id`, `orm_practice_order`.`orderinfo`, `orm_practice_order`.`time` FROM `orm_practice_order` WHERE `orm_practice_order`.`id` = 36 LIMIT 21; args=(36,)

查询3:外键的外键

person_objs = Person.objects.prefetch_related("hometown__province").filter(firstname__contains='兰')

for pin person_objs:

            print(p.hometown.province.name)

[2021-11-06 13:52:54,060] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:52:54,062] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` WHERE `orm_practice_city`.`id` = 33 LIMIT 21; args=(33,)

[2021-11-06 13:52:54,063] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_province`.`id`, `orm_practice_province`.`name` FROM `orm_practice_province` WHERE `orm_practice_province`.`id` = 33 LIMIT 21; args=(33,)

[2021-11-06 13:52:54,064] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` WHERE `orm_practice_city`.`id` = 47 LIMIT 21; args=(47,)

[2021-11-06 13:52:54,064] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_province`.`id`, `orm_practice_province`.`name` FROM `orm_practice_province` WHERE `orm_practice_province`.`id` = 47 LIMIT 21; args=(47,)


2:带参数,model.objects.prefetch_related(*lookups).all()

假设查询的结果有n个  Person 对象

a: 多对多,这种情况,会造成2次查询,并且第二次使用 JOIN ON  + IN 来对关联表( orm_practice_person_visitation )查询

b:直接外键,这种情况,会造成2次查询,并且第二次使用 IN 来对关联表( orm_practice_city ,  orm_practice_province)查询

c: 直接外键的外键, 这种情况, 会造成3次查询,并且第二次通过IN查询外键关联的表 (orm_practice_city)、第三次通过 IN 查询外键的外键关联的表(orm_practice_province )

查询1:多对多

person_objs = Person.objects.prefetch_related("visitation").filter(firstname__contains='兰').all()

# person_objs = Person.objects.prefetch_related("visitation").all()

for p in person_objs:

            for i in p.visitation.all():

                        print(i.name)

[2021-11-06 13:18:52,754] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:18:52,756] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT (`orm_practice_person_visitation`.`person_id`) AS `_prefetch_related_val_person_id`, `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` INNER JOIN `orm_practice_person_visitation` ON (`orm_practice_city`.`id` = `orm_practice_person_visitation`.`city_id`) WHERE `orm_practice_person_visitation`.`person_id` IN (13, 17); args=(13, 17)

查询2:外键

person_objs = Person.objects.prefetch_related().filter(firstname__contains='兰')

for p in person_objs:

            print(p.hometown.name)

            # print(p.needs.orderinfo)

[2021-11-06 13:45:01,121] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:45:01,122] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_order`.`id`, `orm_practice_order`.`customer_id`, `orm_practice_order`.`orderinfo`, `orm_practice_order`.`time` FROM `orm_practice_order` WHERE `orm_practice_order`.`id` IN (36, 23); args=(36, 23)

查询3:外键的外键

person_objs = Person.objects.prefetch_related("hometown__province").filter(firstname__contains='兰')

for pin person_objs:

            print(p.hometown.province.name)

[2021-11-06 13:46:48,447] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_person`.`id`, `orm_practice_person`.`firstname`, `orm_practice_person`.`lastname`, `orm_practice_person`.`needs_id`, `orm_practice_person`.`hometown_id`, `orm_practice_person`.`living_id` FROM `orm_practice_person` WHERE `orm_practice_person`.`firstname` LIKE BINARY '%兰%'; args=('%兰%',)

[2021-11-06 13:46:48,449] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_city`.`id`, `orm_practice_city`.`name`, `orm_practice_city`.`province_id` FROM `orm_practice_city` WHERE `orm_practice_city`.`id` IN (33, 47); args=(33, 47)

[2021-11-06 13:46:48,450] [utils.py:123] [utils:debug_sql] DEBUG (0.000) SELECT `orm_practice_province`.`id`, `orm_practice_province`.`name` FROM `orm_practice_province` WHERE `orm_practice_province`.`id` IN (33, 47); args=(33, 47)

注意 QuerySet是lazy的,要用的时候才会去访问数据库。运行到第二行Python代码时,for循环将plist看做iterator,这会触发数据库查询。最初的两次SQL查询就是prefetch_related导致的。

虽然已经查询结果中包含所有所需的city的信息,但因为在循环体中对Person.visitation进行了filter操作,这显然改变了数据库请求。因此这些操作会忽略掉之前缓存到的数据,重新进行SQL查询。

常用使用方法:

1:model.objects.prefetch_related()

2:model.objects.prefetch_related('外键')

3:model.objects.prefetch_related('外键__外键')

你可能感兴趣的:(Django系列 5:ORM之prefetch_related 方法)