python.sqlalchemy.performance.performance-improvements.len-all-count

profile photo of semgrepsemgrep
Author
7,173
Download Count*

Using QUERY.count() instead of len(QUERY.all()) sends less data to the client since the SQLAlchemy method is performed server-side.

Run Locally

Run in CI

Defintion

rules:
  - id: len-all-count
    pattern: len($X.all())
    message: Using QUERY.count() instead of len(QUERY.all()) sends less data to the
      client since the SQLAlchemy method is performed server-side.
    languages:
      - python
    severity: WARNING
    metadata:
      category: performance
      technology:
        - sqlalchemy
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

performance-improvements.py

# ruleid:batch-import
for song in songs:
    db.session.add(song)

# ruleid:len-all-count
len(persons.all())