muety / wakapi

📊 A minimalist, self-hosted WakaTime-compatible backend for coding statistics
https://wakapi.dev
MIT License
2.57k stars 158 forks source link

Improve metrics endpoint performance #146

Open muety opened 3 years ago

muety commented 3 years ago

According to the recently introduced Sentry performance monitoring, half of all requests to the /metrics endpoint currently take around five seconds. The endpoint involves multiple full-table COUNT(*) queries. But we still might be able to optimize here.

muety commented 3 years ago

Best chance would be to compute the metrics once and from then on count changes, i.e. incrementing or decrementing the counters on a per-request basis. However, this would be way too much effort for optimizing a maintenance endpoint only.

muety commented 3 years ago

This query is quite expensive, but I can't think of a way to make it any more efficient.

image

muety commented 3 years ago

Maybe at least attempt to get rid of these preloads here, which each result in a separate index range scan query.