From b8731c3fff3f36ed96655e74ab1c86263eba0119 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 8 Aug 2017 23:07:03 +0500 Subject: [PATCH] Refs #28459 -- Improved performance of sql.compiler.cursor_iter(). --- django/db/models/sql/compiler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index d6caa34328..f92df8c996 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -51,6 +51,7 @@ class SQLCompiler: order_by = self.get_order_by() self.where, self.having = self.query.where.split_having() extra_select = self.get_extra_select(order_by, self.select) + self.has_extra_select = bool(extra_select) group_by = self.get_group_by(self.select + extra_select, order_by) return extra_select, order_by, group_by @@ -1025,7 +1026,7 @@ class SQLCompiler: result = cursor_iter( cursor, self.connection.features.empty_fetchmany_value, - self.col_count, + self.col_count if self.has_extra_select else None, chunk_size, ) if not chunked_fetch and not self.connection.features.can_use_chunked_reads: @@ -1394,6 +1395,6 @@ def cursor_iter(cursor, sentinel, col_count, itersize): """ try: for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel): - yield [r[0:col_count] for r in rows] + yield rows if col_count is None else [r[:col_count] for r in rows] finally: cursor.close()