1đź‘Ť
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.