Thank you for patch, it is very appreciated.
Sadly I don't think it will be merged, and here is the explanation:
The statement as it is, generates a single (although large) query. The Django ORM is smart enough to detect that when passing the primary key result of a query to another query, both can be merged as a subquery of the other. Splitting and collapsing of them using a loop (or similar like list comprehension) causes the following degradation:
1- The query is now split into two.
2- There is a loop whose size is the configured recent documents count.
3- Part of the query processing now runs on Python instead of the database.
Code: Select all
# Current code (unrolled for clarity)
Code: Select all
# Patch (unrolled for clarity)
pks = [doc['pk'] for doc in Document.objects.order_by('-date_added')[:setting_recent_added_count.value].values('pk')]
Here is an excerpt of the version 3.2 release notes to better explain the reasoning:
The permission inherited computation was improved and now operates mostly at the
database layer instead of being mostly a Python computation. A similar
improvement was done to the ACL calculation system. The ACL calculation now
operates mostly on the database layer freeing many resources and scaling better.
Moving query manipulation to the database lowered memory used dramatically for that release and allowed Mayan to run again on devices like single board computers and small VMs with 2GB or less of RAM. Also speeds things up because the computation task of processing the query result is now offloaded to the database instead of running it on the frontend.
Granted, the default value for recent documents is just 400 and this is not something that should be expected to be setup to a ridiculous high number. But, like almost every feature in Mayan, we much rather make it as open ended as possible because we don't know how many use cases there might be for this feature.
Like every patch and change it will be queued for review and we will discuss it further, just wanted to share some of the reasoning we use for code changes and show my appreciation for your contribution.