Refs #28459 -- Improved performance of SQLCompiler.results_iter().

This commit is contained in:
Sergey Fedoseev 2017-08-08 02:06:15 +05:00 committed by Tim Graham
parent 50a97edc1a
commit ca46f4688c
3 changed files with 18 additions and 16 deletions

View File

@ -1227,9 +1227,9 @@ class RawQuerySet:
converters = compiler.get_converters([
f.get_col(f.model._meta.db_table) if f else None for f in fields
])
if converters:
query = compiler.apply_converters(query, converters)
for values in query:
if converters:
values = compiler.apply_converters(values, converters)
# Associate fields to values
model_init_values = [values[pos] for pos in model_init_pos]
instance = model_cls.from_db(db, model_init_names, model_init_values)

View File

@ -938,14 +938,17 @@ class SQLCompiler:
converters[i] = (convs, expression)
return converters
def apply_converters(self, row, converters):
row = list(row)
for pos, (convs, expression) in converters.items():
value = row[pos]
for converter in convs:
value = converter(value, expression, self.connection)
row[pos] = value
return tuple(row)
def apply_converters(self, rows, converters):
connection = self.connection
converters = list(converters.items())
for row in rows:
row = list(row)
for pos, (convs, expression) in converters:
value = row[pos]
for converter in convs:
value = converter(value, expression, connection)
row[pos] = value
yield tuple(row)
def results_iter(self, results=None):
"""Return an iterator over the results from executing this query."""
@ -953,11 +956,10 @@ class SQLCompiler:
results = self.execute_sql(MULTI)
fields = [s[0] for s in self.select[0:self.col_count]]
converters = self.get_converters(fields)
for rows in results:
for row in rows:
if converters:
row = self.apply_converters(row, converters)
yield row
rows = chain.from_iterable(results)
if converters:
rows = self.apply_converters(rows, converters)
return rows
def has_results(self):
"""

View File

@ -464,7 +464,7 @@ class Query:
result = [None for q in outer_query.annotation_select.items()]
converters = compiler.get_converters(outer_query.annotation_select.values())
result = compiler.apply_converters(result, converters)
result = next(compiler.apply_converters((result,), converters))
return {
alias: val