Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve database check performance: replace introspection.table_names() by a simple cursor query #171

Merged

Conversation

cristianemoyano
Copy link
Contributor

@cristianemoyano cristianemoyano commented Dec 9, 2021

Summary

The introspection.table_names() method in Django could produce a poor performance in large databases.

Take a look at the different backends implementations:

That's why I suggest executing a cheap query: SELECT 1 to test the database connection.

Testing

python runtests.py tests/test_decorators.py:TestDBConnection

(django-watchman) ➜  django-watchman git:(improve-database-check-performance) ✗ python runtests.py
nosetests tests -s --verbosity=1
Creating test database for alias 'default'...
...............................S............................
----------------------------------------------------------------------
Ran 60 tests in 0.158s

OK (SKIP=1)
Destroying test database for alias 'default'...

@mwarkentin
Copy link
Owner

@cristianemoyano Thanks for opening this PR! Interesting, I hadn't realized this could be an issue. Is it something you've run into yourself? Just curious what sort of performance you were seeing on the table_names call?

@cristianemoyano
Copy link
Contributor Author

cristianemoyano commented Dec 22, 2021

@cristianemoyano Thanks for opening this PR! Interesting, I hadn't realized this could be an issue. Is it something you've run into yourself? Just curious what sort of performance you were seeing on the table_names call?

Hey @mwarkentin,

You're welcome. Yes, we realized that running this health check many times in production results in database degradation. Clearly running "select 1" is much cheaper. Unfortunately, I can't show you metrics. I also did a "python timeit" comparing both approaches and my approach turned out to be 6x faster.

Another reason is that the purpose of this health check is to establish a connection with the database and instead the method is also returning the name of the tables which is unnecessary.

@mwarkentin
Copy link
Owner

@cristianemoyano I'm doing some work to modernize the testing and release processes for django-watchman at the moment - hope to get this in and released when that stabilizes.

@cristianemoyano
Copy link
Contributor Author

@cristianemoyano I'm doing some work to modernize the testing and release processes for django-watchman at the moment - hope to get this in and released when that stabilizes.

@mwarkentin that sounds Great! let me know if you need anything from my side.
Happy Holidays!

@mwarkentin
Copy link
Owner

mwarkentin commented Dec 23, 2021

@cristianemoyano looks like the added test is failing - can you take a look? Was it working for you locally?

https://github.com/mwarkentin/django-watchman/runs/4619579103?check_suite_focus=true

@cristianemoyano
Copy link
Contributor Author

cristianemoyano commented Dec 27, 2021

@cristianemoyano looks like the added test is failing - can you take a look? Was it working for you locally?

https://github.com/mwarkentin/django-watchman/runs/4619579103?check_suite_focus=true

That's weird. I will try again rebasing my code later this week.

GitHub
django-watchman exposes a status endpoint for your backing services like databases, caches, etc. - black + isort fmt · 217a80d

@cristianemoyano cristianemoyano force-pushed the improve-database-check-performance branch from 217a80d to f5bac54 Compare December 30, 2021 13:04
@cristianemoyano
Copy link
Contributor Author

Hey @mwarkentin, can you try again, please? Thanks!

@cristianemoyano
Copy link
Contributor Author

All checks are green! 🎉

@mwarkentin
Copy link
Owner

@cristianemoyano thanks! I'll take a look next week. 😊

Copy link
Owner

@mwarkentin mwarkentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks again! I'll update on this PR when I've released to pypi.

@mwarkentin mwarkentin merged commit b4c3c30 into mwarkentin:main Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants