Skip to content

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

Closed
@abhisekharh1988

Description

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()

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions