table invalidation condition enhanced #213
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
During our recent efforts to increase speed of our system we noticed that in some situations django-cachalot performs unnecessary table invalidations after SELECT SQL statements.
Problematic situation
Many models in our system have DateTimeFields which track when model instance was created and updated. We name these fields "created_at" and "updated_at". When retrieving objects from the database, Django's ORM performs SELECT SQL statement behind the scenes - e.g. for our models with "created_at" and "updated_at" DateTimeFields the statement will look like this:
'SELECT some_field_name, some_other_field_name, created_at, updated_at from some_table_name;'
This SELECT SQL statement should not invalidate "some_table_name" table, because SELECT SQL statement does not change data in "some_table_name" table, but combination of "in" operator in this block of code and our field names "created_at" and "updated" trigger unnecessary "some_table_name" table invalidation.
Proposed solution
To get rid of this issue I propose to use "startswith" string method instead of "in" operator. The problematic block of code would look like this after the fix:
Rationale
These unnecessary table invalidations make table with field name that contains any of these SQL keywords('update', 'insert', 'delete', 'alter', 'create', 'drop') effectively uncacheable, because table is invalidated by all types of SQL statements.