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

Redundant Database Queries Occurring In Select Operations When No Data Is Present #2871

Closed
abhisekharh1988 opened this issue Apr 16, 2024 · 3 comments

Comments

@abhisekharh1988
Copy link

Observed redundant database queries occurring in select operations when no data is present. Please find below the code snippet:

@classmethod
def get_for_resource(cls, resource_type: str, resource_id: str) -> List[SomeModel]:
    return list(
        cls.select().where(
            cls.resource_id == resource_id, cls.resource_type == resource_type
        )
    )

The list() function invokes the BaseQuery's len() function, followed by the iter() function, and then again len() function. Within len() and iter(), Peewee includes a check called _ensure_execution():

def _ensure_execution(self):
    if not self._cursor_wrapper:
        if not self._database:
            raise ValueError('Query has not been executed.')
        self.execute()

Despite self._cursor_wrapper existing with a result set size of 0, the query still executes. We're unsure if this behaviour is intentional or if we should modify the condition to:

def _ensure_execution(self):
    if self._cursor_wrapper is None:
        if not self._database:
            raise ValueError('Query has not been executed.')
        self.execute()
@abhisekharh1988
Copy link
Author

Furthermore, I've observed that modifying the code as shown below prevents redundant queries. However, considering our legacy code base, it may not be feasible to implement this change in multiple places:

@classmethod
def get_for_resource(cls, resource_type: str, resource_id: str) -> List[SomeModel]:
    cls.select().where(
        cls.resource_id == resource_id, cls.resource_type == resource_type
    ).objects()

@coleifer
Copy link
Owner

Thanks, this was a surprise. Fixed now.

@abhisekharh1988
Copy link
Author

Thanks for the fix. :)

coleifer added a commit that referenced this issue Apr 17, 2024
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

No branches or pull requests

2 participants