Skip to content

Skip data validation when fetching data from the DB #858

Closed

Description

@Abdeldjalil-H

Problem Desctription

I have noticed that it takes a long time to return the result of simple query. At first glance, I thought it is because of some unnecessary joins. But it turns out that the queries are too fast (less than 10ms), while creating and validating pydantic models take a lot of time (~600ms).

Proposed solution

When I check pydantic docs I find this:

pydantic also provides the construct() method which allows models to be created without validation this can be useful when data has already been validated or comes from a trusted source and you want to create a model as efficiently as possible (construct() is generally around 30x faster than creating a model with full validation).

I changed the method ModelRow.from_row to use construct method:

- instance = cast("Model", cls(**item))
+ instance = cast("Model", cls.construct(**item))

this significantly reduces response time. I think it is safe to skip data validation since it has been validated at creation time.
If that sounds reasonable I can create a PR to fix this.

Activity

  1. collerek commented on Oct 2, 2022

    @collerek
    Collaborator

    That's not so simple, unfortunately. A lot of things happen in __init__ of ormar models, among them registration and population of relations. Probably there is a chance to skip fields validation on loads (it already happens for pk_only models) but you cannot just switch to construct.

  2. Abdeldjalil-H commented on Oct 3, 2022

    @Abdeldjalil-H
    ContributorAuthor

    @collerek
    The method construct calls _construct_relations. So this will create the relations. There is a bug in _construct_relations but this can be fixed.

  3. collerek commented on Oct 4, 2022

    @collerek
    Collaborator

    Ah ok, I forgot I already did inherit construct method 😅
    Yeah we can try that (switching to construct in from_row I mean).

    If you found a bug please report it in a separate issue to handle it first.

    Thanks!

  4. blazing-gig commented on Oct 10, 2022

    @blazing-gig

    Hey @collerek @Abdeldjalil-H any updates on this ? Just curious to knw. Thanks.

  5. Abdeldjalil-H commented on Oct 10, 2022

    @Abdeldjalil-H
    ContributorAuthor

    @blazing-gig I am waiting for pr #870 to be merged. After that I'll try to work on it. There are about 22 tests that are failing.

  6. vaibhavsagar commented on May 28, 2024

    @vaibhavsagar

    Has there been any update on this? I'd love to be able to skip validation on load.

  7. collerek commented on Oct 26, 2026

    @collerek
    Collaborator

    Implemented in #1875: rows of models without custom validators, encrypted or base64 fields are now built directly from the database values without __init__ or re-validation (1.4–2.2× faster loading). Models that need validation keep it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions