from itertools import islice
batch_size =100
objs =(Entry(headline='Test %s'% i)for i inrange(1000))whileTrue:
batch =list(islice(objs, batch_size))if not batch:break
Entry.objects.bulk_create(batch, batch_size)
通过 Prefetch 控制预取的查询
N + 1 问题是非常常见的查询效率杀手。在 Django 中我们通常会使用 selected_related 或prefetch_related 来预取关联对象,来减少和 DB 之间的交互,但是在使用上也需要有一些注意的地方。
首先,预取需要精确控制到字段。
Django 默认的查询方式都是粗放的,例如普通查询不使用 values 或者 only 时都是 select * ,而预取也不例外,看看下面这个例子。
classFoo(models.Model):...classBar(models.Model):
foo = models.ForeignKey(Foo)...classBaz(models.Model):"""A very large table"""
foo = models.ForeignKey(Foo)
import re
import time
value ="aaaaaaaaaaaaaaaaaaaaaaaaaaaaabs"
strange_regex = re.compile("(a+)+s")
start = time.time()
re.match(strange_regex, value)
end = time.time()print(end - start)
不知道大家执行了多久,在我开发机上使用 Python 3.6+(包括 3.10.x)需要耗费20秒以上,即使 CPU ——Apple M1 Pro 的性能已经相当强悍了。
While writing the text editor sam [6] in the early 1980s, Rob Pike wrote a new regular expression implementation, which Dave Presotto extracted into a library that appeared in the Eighth Edition. Pike's implementation incorporated submatch tracking into an efficient NFA simulation but, like the rest of the Eighth Edition source, was not widely distributed. Pike himself did not realize that his technique was anything new. Henry Spencer reimplemented the Eighth Edition library interface from scratch, but using backtracking, and released his implementation into the public domain.It became very widely used, eventually serving as the basis for the slow regular expression implementations mentioned earlier: Perl, PCRE, Python, and so on.