Slow Queries in Discourse
A discussion on slow queries in Discourse has identified several opportunities for optimization.
Unnecessary ORDER BY in Subquery
wtune found a query with an unnecessary ORDER BY in a subquery, which can be removed to improve performance. According to tests, this can improve query performance by 18%-90%.
Unnecessary DISTINCT
wtune also found a query with an unnecessary DISTINCT clause, which can be removed when tags.id and notification_level are specified values. This optimization can improve query performance by 30%.
Slow Query with Many Subqueries and Union
wtune identified a slow query with many subqueries and union operations. The query can be simplified and optimized, resulting in a 93% improvement in performance.
Incorrect Rewritten Query
sam questioned the correctness of the rewritten query, pointing out that it would return groups with no visibility level checks. wtune apologized for the mistake and provided a corrected rewritten query.
Slow Query with Unnecessary JOIN and DISTINCT
wtune found a slow query with unnecessary JOIN and DISTINCT operations. The query can be optimized by removing unnecessary tables and DISTINCT clauses, resulting in a 17.49% improvement in performance.
Slow Query with Unnecessary JOIN
wtune identified a slow query with an unnecessary JOIN operation. The query can be optimized by replacing the subquery with a simple predicate, resulting in a 19% improvement in performance.
codinghorror asked if a database scanner is being used to identify slow queries, but wtune clarified that they are running specs in Discourse and analyzing database logs to identify slow queries.