open-contracting / credere-backend

A tool that facilitates the participation of Micro, Small, and Medium businesses (MSMEs) in the Colombian public procurement market.
https://credere.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5 stars 0 forks source link

Missing SQL indexes #184

Open jpmckinney opened 10 months ago

jpmckinney commented 10 months ago

Can maybe start with the slow queries listed at https://open-contracting-partnership.sentry.io/performance/?project=4505799907672064&statsPeriod=14d

For single-column indexes, would need to add index=True to Field() calls to add these indexes.

Not sure if https://sqlmodel.tiangolo.com supports composite indexes. Might need to do SQLAlchemy or Alembic directly.

JOIN

Found using:

(?<!os\.path)\.join\(

WHERE

Found using these regexes:

\.filter\((?:\n +)?\b(?:\w+\.)+(?!id\b\s)\w+\b\s
\.filter\((?:\n +)?\b(?:and_\(|or_\(|cast\(|text\()
\.filter\((?!(?:\n +)?\S+\.\w+)
\.where\(

To avoid a ton of indexes, I grouped some WHERE matches under one composite index.

Application

ApplicationAction

Award

Borrower

BorrowerDocument

CreditProduct

Message

User

get_general_statistics() and get_msme_opt_in_stats() have a lot of filters, but not sure how important to optimize these functions. I didn't add some from statistics.py or update_statistic.py.

I think the queries in application_utils.py are too complex for an index.

jpmckinney commented 10 months ago

Note that creating automatic migrations incorrectly detects nullable changes. These seem to be fixed in more recent 1.4.x versions of SQLAlchemy, but we need SQLModel to release 0.0.9 to unpin SQLAlchemy: https://github.com/tiangolo/sqlmodel/issues/434#issuecomment-1775601658