Performance: slow export with ForeignKeys id
See original GitHub issueI tried to investigate slow export on my model. I found out, that if I write dehydrate
function on ModelResource
like this:
def dehydrate_user_profile(self, search_report):
return search_report.user_profile_id
The export rapidly speeds up. I think, that django-import-export
has bad method of getting the id
values for ForeignKeys.
The bad performance is similar to this (notice the .
instead of _
)
def dehydrate_user_profile(self, search_report):
return search_report.user_profile.id
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
django-import-export export is slow when there is foreign key
when there is foreign key in model, exporting is very slow, when I exclude the foreign there is no problem, it starts to...
Read more >Foreign key creation is very slow - Oracle Communities
Foreign key creation is very slow ... Hello, We are about to migrate a database from 9iR2 to 10g by doing export/import. One...
Read more >3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >Common Issues with the SQL Server Import and Export Wizard
If there is more than one table with a timestamp you'll need to repeat this for each table. Constraints. If the table has...
Read more >Hidden secrets of SQL Server Foreign Keys - SQLShack
When a foreign key status is not trusted then the storage engine does not require to check the inserted foreign key is exists...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@andrewgy8 In my case the increase was extreme - when I try with just 1300 records it is almost instant vs. 20 seconds.
I investigated a little bit further. The problem is, that on line
90
infields.py
inget_value()
function the value of foreign key is fetched by:If the value is not already cached or not fetch by the queryset with
select_related
, it will trigger new DB query for every row of the export. That value is further processed byWidget.render()
where the value of certain key is fetched from the related object. This is unnecessary, if the key we want is primary key of the ForeignKey relation.I am not sure, how to fix this properly. One method might be to detect, that we want to fetch
pk
/id
key and bypass theget_value()->Widget.render()
mechanism. Downside is that it might change behaviour if one of these function was overridden by user.Second method is to automatically include
select_related
to the queryset. Upside is that it can greatly improve performance not only for primary keys, but also for related fields. Downside is that it might not work for some user modified querysets properly. Also in some cases it can lead to slowdown, because theselect_related
would increase the query complexity and/or fetch unwanted data (which might be solved by usingonly
).@mikaraunio Thanks for this update. I feel that updating the ‘Getting started’ docs would be a great way to move this forward. If you are able to submit a PR for this documentation update that would be appreciated.