# Avoid ORM querys at Global Scope

Because …

### (1) The query runs every time the application comes up

Elastic applications that scale with load, result in an unnecessary DB hits when new instances are added. The effect is multiplicative for *each* process of your application per pod or container.

### (2) The connection associated with instance will be closed

More importantly, using this long lived, stale ORM object will result in an `server has gone away` error on usage. The default setting `CONN_MAX_AGE` is `0` where:

> `0` closes connection / request; `None` persistent connection; `0` &lt; keep alive in seconds;

Move the query into the respective view or method. You could [cache](https://github.com/Suor/django-cacheops) the object too.

## References

* [Django’s Persistent Connections](https://docs.djangoproject.com/en/5.1/ref/databases/#persistent-connections)
    
* setting [CONN\_MAX\_AGE](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-CONN_MAX_AGE)
    
* [Django Cache Ops](https://github.com/Suor/django-cacheops)
