Admin list runs query for each claim

Issue #13 resolved
Jelmer van der Linde created an issue

I turned on the SQLAlchemy debug thing to look why the admin screen takes so long to load, and was surprised to see many of these queries, one for each claim it seems:

<query statement="SELECT claim.id AS claim_id, claim.export_id AS claim_export_id, claim.cover_id AS claim_cover_id, claim.name AS claim_name, claim.email AS claim_email, claim.phone AS claim_phone, claim.iban AS claim_iban, claim.amount AS claim_amount, claim.packaging_deposit_in AS claim_packaging_deposit_in, claim.packaging_deposit_out AS claim_packaging_deposit_out, claim.committee_id AS claim_committee_id, claim.description AS claim_description, claim.receipt_name AS claim_receipt_name, claim.submitted AS claim_submitted, claim.updated AS claim_updated, claim.status_code AS claim_status_code, claim.parent_id AS claim_parent_id
FROM claim
WHERE %s = claim.parent_id" parameters=(453L,) duration=0.001>

I haven't been able to find the origin of the query yet.

Comments (4)

  1. Jelmer van der Linde reporter

    Found it. Models.py, line 107:

        @property
        def sub_claims(self):
            return self._sub_claims.all()
    
  2. Jelmer van der Linde reporter

    Not really a bug yet, more of a probable issue once Reclaim has been used more.

    Possible work-around: pagination, since this seems to be related to code in the templates that queries whether a row has sub-rows.

  3. Martijn Luinstra

    This is (partially) due to improper use of sqlalchemy. For years, I've been thinking that the relationship property loaded everything by default. That is not the case, it only queries the DB the first time the relationship is accessed, and then caches the data. The current implementation uses dynamic queries, which are not cached, so I can cut the amount of queries used in half by switching from dynamic loading to the default lazy loading. It will still run a query for every claim though.

  4. Log in to comment