Hi i have lots of objects which i get from query, the queryset is not yet evaluated, when i pass objectlist to paginator object it took 14 seconds to return paginator object, it is bec开发者_Python百科ause it is evaluating the all objects in list which took time (hitting db may be).
I forcefully evaluated queryset before sending it to paginator object as:
ds_objects = list(ds_objects)
Guess what now pagination took 0.0 seconds in returning object, but the problem still remain now all time is taken by ds_objects = list(ds_objects), how can i do it efficiently so that my view response time reduced to just 2 or 3 seconds? i have not found any best solution :s
My guess is that the JOINs (so the select_related
) are not important for the query speed. Anyway when you do a values()
query you don't really need select_related
because the only fields you'll ever get are those specified as values()
arguments, and they are retrieved by JOINs anyway.
To see why it takes so long, you can do the following:
Print out the "raw sql" with
print tbl_action_log.objects.order_by('-TicketID', '-ActionLogID').query
Modify it (it's not actually SQL, but it should be near enough) to be run in a SQL client, like django's own manage.py dbshell
.
Execute it, take note of the time. Execute then EXPLAIN ...
(put your query in the dots), you will probably see some "full table scan" message
Add a proper index to the database table, over the affected columns, and execute the query again. The index will probably be this:
CREATE INDEX idx_action_log_on_tkid_logid ON tbl_action_log (TicketID, ActionLogID);
When you are satisfied with the index, store its creation commmand in the
file app/sql/modelname.sql
, this will create the index in any new deployment.
精彩评论