|
11 | 11 | from django.db.models.functions.math import Power
|
12 | 12 | from django.db.models.lookups import IsNull
|
13 | 13 | from django.db.models.sql import compiler
|
14 |
| -from django.db.models.sql.constants import MULTI, SINGLE |
| 14 | +from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, MULTI, SINGLE |
15 | 15 | from django.db.models.sql.datastructures import BaseTable
|
16 | 16 | from django.utils.functional import cached_property
|
17 | 17 | from pymongo import ASCENDING, DESCENDING
|
@@ -235,7 +235,9 @@ def pre_sql_setup(self, with_col_aliases=False):
|
235 | 235 | self.order_by_objs = [expr.replace_expressions(all_replacements) for expr, _ in order_by]
|
236 | 236 | return extra_select, order_by, group_by
|
237 | 237 |
|
238 |
| - def execute_sql(self, result_type=MULTI, chunked_fetch=False, chunk_size=None): |
| 238 | + def execute_sql( |
| 239 | + self, result_type=MULTI, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE |
| 240 | + ): |
239 | 241 | self.pre_sql_setup()
|
240 | 242 | try:
|
241 | 243 | query = self.build_query(
|
@@ -315,16 +317,15 @@ def _make_result(self, entity, columns):
|
315 | 317 | result.append(obj.get(name))
|
316 | 318 | return result
|
317 | 319 |
|
318 |
| - def cursor_iter(self, cursor, _, columns): |
319 |
| - """ |
320 |
| - Yield chunks of results from cursor. |
321 |
| - Ignore any chunk_size/batch_size overrides and abide by MongoDB's |
322 |
| - default chunking behavior. This maximizes performance on document |
323 |
| - retrieval. |
324 |
| - Read more here: https://www.mongodb.com/docs/manual/core/cursors/#cursor-batches |
325 |
| - """ |
| 320 | + def cursor_iter(self, cursor, chunk_size, columns): |
| 321 | + """Yield chunks of results from cursor.""" |
| 322 | + chunk = [] |
326 | 323 | for row in cursor:
|
327 |
| - yield [self._make_result(row, columns)] |
| 324 | + chunk.append(self._make_result(row, columns)) |
| 325 | + if len(chunk) == chunk_size: |
| 326 | + yield chunk |
| 327 | + chunk = [] |
| 328 | + yield chunk |
328 | 329 |
|
329 | 330 | def check_query(self):
|
330 | 331 | """Check if the current query is supported by the database."""
|
|
0 commit comments