{"id":445768,"date":"2025-01-22T21:01:47","date_gmt":"2025-01-22T21:01:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=445768"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=445768","title":{"rendered":"<span>Django \u043f\u0440\u043e\u0442\u0438\u0432 N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432: \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e select_related \u0438 prefetch_related<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <strong>\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong> \u0432 Django. N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0434\u0435\u043b\u0430\u0435\u0442 <strong>\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043b\u043a\u0438\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0440\u0443\u043f\u043d\u044b\u0445<\/strong>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">class Author(models.Model):     name = models.CharField(max_length=100)  class Book(models.Model):     title = models.CharField(max_length=100)     author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')<\/code><\/pre>\n<p>\u0418 \u0432\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">authors = Author.objects.all() for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 SQL:<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u043e\u043c <strong>\u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/strong> (\u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0430):<\/p>\n<pre><code class=\"sql\">SELECT * FROM book WHERE author_id = 1; SELECT * FROM book WHERE author_id = 2; SELECT * FROM book WHERE author_id = 3; ...<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 100 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u0431\u0443\u0434\u0435\u0442 <strong>101 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>. \u0410 \u0435\u0441\u043b\u0438 10 000 \u0430\u0432\u0442\u043e\u0440\u043e\u0432? \u041c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430?<\/p>\n<ul>\n<li>\n<p><strong>\u0412\u0440\u0435\u043c\u044f \u043e\u0442\u0432\u0435\u0442\u0430:<\/strong> \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u0442\u0451\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u042d\u0442\u043e \u043a\u0430\u0442\u0430\u0441\u0442\u0440\u043e\u0444\u0430 \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440:<\/strong> \u0441\u043e\u0442\u043d\u0438 \u043c\u0435\u043b\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u044e\u0442 \u0431\u0430\u0437\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u043d\u044c\u0433\u0438:<\/strong> \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043b\u0430\u0442\u0438\u0442\u0435 \u0437\u0430 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0439 \u0443\u0434\u0430\u0440 \u043f\u043e \u0431\u044e\u0434\u0436\u0435\u0442\u0443.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0448\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u043e\u0441\u043d\u043e\u0432\u044b Django ORM<\/h3>\n<h4>select_related<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ForeignKey<\/code> \u0438 <code>OneToOneField<\/code>. \u041e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 SQL JOIN, \u0432\u044b\u0442\u044f\u0433\u0438\u0432\u0430\u044f \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043f\u0440\u044f\u043c\u044b\u043c \u0441\u0432\u044f\u0437\u044f\u043c <code>ForeignKey<\/code> \u0438\u043b\u0438 <code>OneToOneField<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u0430\u0436\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440: \u0441\u0432\u044f\u0437\u044c <\/strong><code>ForeignKey<\/code><\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0435\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">class Author(models.Model):     name = models.CharField(max_length=100)  class Book(models.Model):     title = models.CharField(max_length=100)     author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">books = Book.objects.all() for book in books:     print(book.title, book.author.name)<\/code><\/pre>\n<p>ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 <strong>2 + N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong>:<\/p>\n<ol>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">SELECT * FROM book;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"python\">SELECT * FROM author WHERE id = &lt;author_id&gt;;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>select_related<\/code>:<\/p>\n<pre><code class=\"python\">books = Book.objects.select_related('author') for book in books:     print(book.title, book.author.name)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0432\u0441\u0435\u0433\u043e <strong>1 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>:<\/p>\n<pre><code class=\"python\">SELECT \"book\".\"id\", \"book\".\"title\", \"book\".\"author_id\",         \"author\".\"id\", \"author\".\"name\" FROM \"book\" INNER JOIN \"author\" ON (\"book\".\"author_id\" = \"author\".\"id\");<\/code><\/pre>\n<h4>prefetch_related<\/h4>\n<p>\u042d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ManyToManyField<\/code> \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 <code>ForeignKey<\/code>. \u041e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 Python, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043b\u0430\u0432\u0438\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u0438\u0435-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c <code>ManyToManyField<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ForeignKey<\/code>, \u0433\u0434\u0435 \u0441\u0432\u044f\u0437\u044c \u0438\u0434\u0451\u0442 \u043e\u0442 \u0434\u0435\u0442\u0435\u0439 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044e.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440: \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c <\/strong><code>ForeignKey<\/code><\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0451\u043c \u0442\u0443 \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u044c, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code>class AuthorQuerySet(models.QuerySet):     def with_books(self):         return self.prefetch_related('books')<\/code><\/pre>\n<p>Django \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442:<\/p>\n<ol>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u044c <code>author.books.all()<\/code>:<\/p>\n<pre><code class=\"python\">SELECT * FROM book WHERE author_id = 1; SELECT * FROM book WHERE author_id = 2; ...<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>prefetch_related<\/code>:<\/p>\n<pre><code>class Author(models.Model):     name = models.CharField(max_length=100)      objects = AuthorQuerySet.as_manager()<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM book WHERE author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>Django \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043d\u0438\u0433\u0438 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u0430\u0432\u0442\u043e\u0440\u0430\u043c\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Python. <\/p>\n<p>\u042d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<h3>\u041a\u0430\u043a \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 QuerySets \u0441\u043f\u0430\u0441\u0430\u044e\u0442 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435?<\/h3>\n<p>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438, \u0432\u0440\u043e\u0434\u0435 \u043c\u043e\u0449\u043d\u044b\u0435, \u043d\u043e \u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u044b\u0442\u0430\u0435\u0442\u0435\u0441\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043f\u0440\u043e\u0449\u0435 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 QuerySet. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_from_year(self, year):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.filter(published_year=year)             )         )<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0435\u0433\u043e \u043a \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_from_year(2023) for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books() for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043a\u043d\u0438\u0433 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM book WHERE published_year = 2023 AND author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u043e\u0433: <strong>2 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e N+1<\/strong>.<\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043d\u0438\u0433\u0438, \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 2023 \u0433\u043e\u0434\u0443:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_from_year(self, year):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.filter(published_year=year)             )         )<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_from_year(2023) for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_book_published_year ON book (published_year);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043d\u0438\u0433 2023 \u0433\u043e\u0434\u0430:<\/p>\n<pre><code class=\"sql\">authors = Author.objects.prefetch_related('books')[:100]<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0441\u0435 \u0443\u0441\u0438\u043b\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0439\u0442\u0438 \u043f\u0440\u0430\u0445\u043e\u043c.<\/p>\n<ol>\n<li>\n<p><strong>\u0418\u043d\u0434\u0435\u043a\u0441\u044b:<\/strong> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_book_published_year ON book (published_year);<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>LIMIT \u0438 OFFSET:<\/strong> \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e.<\/p>\n<pre><code class=\"python\">authors = Author.objects.prefetch_related('books')[:100]<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>EXPLAIN:<\/strong> \u043d\u0435 \u043b\u0435\u043d\u0438\u0442\u0435\u0441\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<pre><code class=\"sql\">EXPLAIN SELECT * FROM book WHERE published_year = 2023;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u0438\u0445 \u043a\u043d\u0438\u0433\u0438 \u0438 \u0438\u0437\u0434\u0430\u0442\u0435\u043b\u0435\u0439 \u044d\u0442\u0438\u0445 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_and_publishers(self):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.select_related('publisher')             )         )<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_and_publishers() for author in authors:     print(author.name)     for book in author.books.all():         print(f\"  {book.title} ({book.publisher.name})\")<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043d\u0438\u0433 \u0441 \u0438\u0445 \u0438\u0437\u0434\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"sql\">SELECT * FROM book  LEFT JOIN publisher ON book.publisher_id = publisher.id WHERE author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr\/>\n<p>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 QuerySets \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442:<\/p>\n<ul>\n<li>\n<p><strong>\u0418\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u043a\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u044d\u0442\u043e \u043d\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0435\u0442\u043a\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0431\u0430\u0437\u0443.<\/p>\n<blockquote>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e \u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 23 \u044f\u043d\u0432\u0430\u0440\u044f, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. <\/p>\n<p>\u041d\u0430 \u0437\u0430\u043d\u044f\u0442\u0438\u0438 \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u0435\u0441\u044c \u0432 \u0442\u0435\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f. <a href=\"https:\/\/otus.pw\/7Mu2\/\">\u0417\u0430\u043f\u0438\u0441\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/875384\/\"> https:\/\/habr.com\/ru\/articles\/875384\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <strong>\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong> \u0432 Django. N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0434\u0435\u043b\u0430\u0435\u0442 <strong>\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043b\u043a\u0438\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0440\u0443\u043f\u043d\u044b\u0445<\/strong>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">class Author(models.Model):     name = models.CharField(max_length=100)  class Book(models.Model):     title = models.CharField(max_length=100)     author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')<\/code><\/pre>\n<p>\u0418 \u0432\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">authors = Author.objects.all() for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 SQL:<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u043e\u043c <strong>\u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u044c<\/strong> (\u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0430):<\/p>\n<pre><code class=\"sql\">SELECT * FROM book WHERE author_id = 1; SELECT * FROM book WHERE author_id = 2; SELECT * FROM book WHERE author_id = 3; ...<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 100 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u0431\u0443\u0434\u0435\u0442 <strong>101 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>. \u0410 \u0435\u0441\u043b\u0438 10 000 \u0430\u0432\u0442\u043e\u0440\u043e\u0432? \u041c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430?<\/p>\n<ul>\n<li>\n<p><strong>\u0412\u0440\u0435\u043c\u044f \u043e\u0442\u0432\u0435\u0442\u0430:<\/strong> \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u0442\u0451\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u042d\u0442\u043e \u043a\u0430\u0442\u0430\u0441\u0442\u0440\u043e\u0444\u0430 \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440:<\/strong> \u0441\u043e\u0442\u043d\u0438 \u043c\u0435\u043b\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u044e\u0442 \u0431\u0430\u0437\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u043d\u044c\u0433\u0438:<\/strong> \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043b\u0430\u0442\u0438\u0442\u0435 \u0437\u0430 \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0439 \u0443\u0434\u0430\u0440 \u043f\u043e \u0431\u044e\u0434\u0436\u0435\u0442\u0443.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0448\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u043e\u0441\u043d\u043e\u0432\u044b Django ORM<\/h3>\n<h4>select_related<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ForeignKey<\/code> \u0438 <code>OneToOneField<\/code>. \u041e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 SQL JOIN, \u0432\u044b\u0442\u044f\u0433\u0438\u0432\u0430\u044f \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043f\u0440\u044f\u043c\u044b\u043c \u0441\u0432\u044f\u0437\u044f\u043c <code>ForeignKey<\/code> \u0438\u043b\u0438 <code>OneToOneField<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u0430\u0436\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440: \u0441\u0432\u044f\u0437\u044c <\/strong><code>ForeignKey<\/code><\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0435\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">class Author(models.Model):     name = models.CharField(max_length=100)  class Book(models.Model):     title = models.CharField(max_length=100)     author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">books = Book.objects.all() for book in books:     print(book.title, book.author.name)<\/code><\/pre>\n<p>ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 <strong>2 + N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong>:<\/p>\n<ol>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">SELECT * FROM book;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"python\">SELECT * FROM author WHERE id = &lt;author_id&gt;;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>select_related<\/code>:<\/p>\n<pre><code class=\"python\">books = Book.objects.select_related('author') for book in books:     print(book.title, book.author.name)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0432\u0441\u0435\u0433\u043e <strong>1 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>:<\/p>\n<pre><code class=\"python\">SELECT \"book\".\"id\", \"book\".\"title\", \"book\".\"author_id\",         \"author\".\"id\", \"author\".\"name\" FROM \"book\" INNER JOIN \"author\" ON (\"book\".\"author_id\" = \"author\".\"id\");<\/code><\/pre>\n<h4>prefetch_related<\/h4>\n<p>\u042d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ManyToManyField<\/code> \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 <code>ForeignKey<\/code>. \u041e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 Python, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043b\u0430\u0432\u0438\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u0438\u0435-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c <code>ManyToManyField<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439 <code>ForeignKey<\/code>, \u0433\u0434\u0435 \u0441\u0432\u044f\u0437\u044c \u0438\u0434\u0451\u0442 \u043e\u0442 \u0434\u0435\u0442\u0435\u0439 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044e.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440: \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c <\/strong><code>ForeignKey<\/code><\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0451\u043c \u0442\u0443 \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u044c, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code>class AuthorQuerySet(models.QuerySet):     def with_books(self):         return self.prefetch_related('books')<\/code><\/pre>\n<p>Django \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442:<\/p>\n<ol>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0432\u044f\u0437\u044c <code>author.books.all()<\/code>:<\/p>\n<pre><code class=\"python\">SELECT * FROM book WHERE author_id = 1; SELECT * FROM book WHERE author_id = 2; ...<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>prefetch_related<\/code>:<\/p>\n<pre><code>class Author(models.Model):     name = models.CharField(max_length=100)      objects = AuthorQuerySet.as_manager()<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c ORM \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043d\u0438\u0433 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">SELECT * FROM book WHERE author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>Django \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043d\u0438\u0433\u0438 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u0430\u0432\u0442\u043e\u0440\u0430\u043c\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Python. <\/p>\n<p>\u042d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<h3>\u041a\u0430\u043a \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 QuerySets \u0441\u043f\u0430\u0441\u0430\u044e\u0442 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435?<\/h3>\n<p>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438, \u0432\u0440\u043e\u0434\u0435 \u043c\u043e\u0449\u043d\u044b\u0435, \u043d\u043e \u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u044b\u0442\u0430\u0435\u0442\u0435\u0441\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043f\u0440\u043e\u0449\u0435 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 QuerySet. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0441 \u0438\u0445 \u043a\u043d\u0438\u0433\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_from_year(self, year):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.filter(published_year=year)             )         )<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0435\u0433\u043e \u043a \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_from_year(2023) for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books() for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043a\u043d\u0438\u0433 \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM book WHERE published_year = 2023 AND author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u043e\u0433: <strong>2 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e N+1<\/strong>.<\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043d\u0438\u0433\u0438, \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 2023 \u0433\u043e\u0434\u0443:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_from_year(self, year):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.filter(published_year=year)             )         )<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_from_year(2023) for author in authors:     print(author.name, [book.title for book in author.books.all()])<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_book_published_year ON book (published_year);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043d\u0438\u0433 2023 \u0433\u043e\u0434\u0430:<\/p>\n<pre><code class=\"sql\">authors = Author.objects.prefetch_related('books')[:100]<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0441\u0435 \u0443\u0441\u0438\u043b\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0439\u0442\u0438 \u043f\u0440\u0430\u0445\u043e\u043c.<\/p>\n<ol>\n<li>\n<p><strong>\u0418\u043d\u0434\u0435\u043a\u0441\u044b:<\/strong> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_book_published_year ON book (published_year);<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>LIMIT \u0438 OFFSET:<\/strong> \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e.<\/p>\n<pre><code class=\"python\">authors = Author.objects.prefetch_related('books')[:100]<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>EXPLAIN:<\/strong> \u043d\u0435 \u043b\u0435\u043d\u0438\u0442\u0435\u0441\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<pre><code class=\"sql\">EXPLAIN SELECT * FROM book WHERE published_year = 2023;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c QuerySet, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u0438\u0445 \u043a\u043d\u0438\u0433\u0438 \u0438 \u0438\u0437\u0434\u0430\u0442\u0435\u043b\u0435\u0439 \u044d\u0442\u0438\u0445 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"python\">class AuthorQuerySet(models.QuerySet):     def with_books_and_publishers(self):         return self.prefetch_related(             models.Prefetch(                 'books',                 queryset=Book.objects.select_related('publisher')             )         )<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"python\">authors = Author.objects.with_books_and_publishers() for author in authors:     print(author.name)     for book in author.books.all():         print(f\"  {book.title} ({book.publisher.name})\")<\/code><\/pre>\n<p>SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"sql\">SELECT * FROM author;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043d\u0438\u0433 \u0441 \u0438\u0445 \u0438\u0437\u0434\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"sql\">SELECT * FROM book  LEFT JOIN publisher ON book.publisher_id = publisher.id WHERE author_id IN (1, 2, 3, ...);<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr\/>\n<p>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 QuerySets \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442:<\/p>\n<ul>\n<li>\n<p><strong>\u0418\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u043a\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u044d\u0442\u043e \u043d\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0435\u0442\u043a\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0431\u0430\u0437\u0443.<\/p>\n<blockquote>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e \u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 23 \u044f\u043d\u0432\u0430\u0440\u044f, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. <\/p>\n<p>\u041d\u0430 \u0437\u0430\u043d\u044f\u0442\u0438\u0438 \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u0435\u0441\u044c \u0432 \u0442\u0435\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f. <a href=\"https:\/\/otus.pw\/7Mu2\/\">\u0417\u0430\u043f\u0438\u0441\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/875384\/\"> https:\/\/habr.com\/ru\/articles\/875384\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-445768","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/445768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445768"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/445768\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}