question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance: slow export with ForeignKeys id

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
PetrDlouhycommented, Jul 24, 2019

@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 in fields.py in get_value() function the value of foreign key is fetched by:

value = getattr(value, attr, None)

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 by Widget.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 the get_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 the select_related would increase the query complexity and/or fetch unwanted data (which might be solved by using only).

0reactions
matthewhegartycommented, Feb 17, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found