Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
problem-builder
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
problem-builder
Commits
e2e4b8a3
Commit
e2e4b8a3
authored
Feb 25, 2017
by
Matjaz Gregoric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid count(*) query in management command.
Count(*) is very slow on large tables.
parent
c559a665
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
7 deletions
+9
-7
problem_builder/management/commands/copy_deprecated_course_id.py
+9
-7
No files found.
problem_builder/management/commands/copy_deprecated_course_id.py
View file @
e2e4b8a3
import
math
import
time
from
django.core.management.base
import
BaseCommand
from
problem_builder.models
import
Answer
...
...
@@ -29,18 +28,21 @@ class Command(BaseCommand):
batch_size
=
options
[
'batch_size'
]
sleep_time
=
options
[
'sleep'
]
queryset
=
Answer
.
objects
.
filter
(
course_key__isnull
=
True
)
batch_count
=
int
(
math
.
ceil
(
queryset
.
count
()
/
float
(
batch_size
)))
self
.
stdout
.
write
(
"Copying Answer.course_id field into Answer.course_key in batches of {}"
.
format
(
batch_size
)
)
for
batch
in
xrange
(
1
,
batch_count
+
1
):
for
answer
in
queryset
[:
batch_size
]:
idx
=
0
while
True
:
idx
+=
1
batch
=
queryset
[:
batch_size
]
if
not
batch
:
break
for
answer
in
batch
:
answer
.
course_key
=
answer
.
course_id
answer
.
save
()
self
.
stdout
.
write
(
"Processed batch {} of {}"
.
format
(
batch
,
batch_count
))
if
batch
!=
batch_count
:
time
.
sleep
(
sleep_time
)
self
.
stdout
.
write
(
"Processed batch {}"
.
format
(
idx
))
time
.
sleep
(
sleep_time
)
self
.
stdout
.
write
(
"Successfully copied Answer.course_id into Answer.course_key"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment