Disable ONLY_FULL_GROUP_BY
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
SQL's ONLY_FULL_GROUP_BY is an essential component of MySQL that affects how GROUP BY clauses are processed in queries. It ensures that the queries follow precise SQL standards, enforcing that any column in the SELECT clause is either part of an aggregate function or present in the GROUP BY clause.
Although this behavior ensures data integrity and clear semantics, some developers opt to disable it, often to accommodate legacy code or simplify certain operations. This article delves into how to disable ONLY_FULL_GROUP_BY, examining the implications and providing technical insights and examples.
Understanding ONLY_FULL_GROUP_BY
ONLY_FULL_GROUP_BY is part of MySQL's set of SQL modes. SQL modes define the framework for SQL syntax and semantics, providing flexibility in handling operations. By default, many modern installations have ONLY_FULL_GROUP_BY enabled to comply with SQL standards.
Here’s the behavior enforced by ONLY_FULL_GROUP_BY:
- All columns in the
SELECTstatement that are not part of an aggregate function must be included in theGROUP BYclause. - The underlying rationale is rooted in data consistency and predictability when rolling up rows into summary rows.
Disabling ONLY_FULL_GROUP_BY
Disabling ONLY_FULL_GROUP_BY can offer temporary relief for legacy SQL codebases or specific use-cases. However, you should proceed cautiously, armed with the knowledge of potential pitfalls.
Methods to Disable ONLY_FULL_GROUP_BY
- Session Level:
- Use the following SQL command to disable the mode temporarily for the session:
- Global Level:
- Execute this command to change the setting globally (for new sessions):
- Note: This requires the MySQL server to restart to enforce the changes for all open connections.
- Configuration File:
- Add or modify the following line in your MySQL configuration file (e.g.,
my.cnformy.ini):
- Ensure that
ONLY_FULL_GROUP_BYis not listed among the SQL modes.
Precautions
- Legacy Code: Many developers disable
ONLY_FULL_GROUP_BYto support older applications. Investigate these areas for refactoring opportunities to modernize SQL code. - Data Integrity: Disabling the mode can introduce unclear results with non-deterministic behavior, potentially leading to inaccurate query outputs.
- Performance: Queries might run faster due to fewer constraints, but this is not guaranteed and should be benchmarked.
Technical Examples
Example with ONLY_FULL_GROUP_BY Enabled
Consider a table orders:
| id | customer_id | amount |
| 1 | 101 | 500.00 |
| 2 | 102 | 700.00 |
| 3 | 101 | 350.00 |
Executing the following query with ONLY_FULL_GROUP_BY enabled:
The query would result in an error because amount is neither aggregated nor included in the GROUP BY clause.
Example with ONLY_FULL_GROUP_BY Disabled
If we disable ONLY_FULL_GROUP_BY and rerun the query, you'd get a result, such as a random amount for each customer_id due to the absence of any aggregating mechanism:
| customer_id | amount |
| 101 | 500.00 |
| 102 | 700.00 |
Results like this underscore the ambiguity and unpredictability when ONLY_FULL_GROUP_BY is turned off.
Key Considerations Table
| Aspect | Enabled | Disabled |
| Data Consistency | Ensures columns follow GROUP BY or aggregation rules. | May lead to non-deterministic and ambiguous results. |
| Performance | May be slightly impacted due to additional checks. | Potentially faster but lacks crucial integrity checks. |
| Legacy Support | Might break legacy queries not aligned with SQL standards. | Facilitates compatibility with older SQL codebases. |
| Modernization | Encourages updating code to follow standard practices. | Enables use of old practices but discourages updates. |
Conclusion
Disabling ONLY_FULL_GROUP_BY can be tempting for short-term fixes or supporting legacy systems, but the move must be carefully considered in light of its potential to cause data anomalies and obscure query logic. Instead, aim for code modernization, using disabling as a last resort or temporary measure. Understanding the full implications will empower developers to make sound decisions, ensuring both integrity and functionality in their SQL operations.
For further insights, consider diving deeper into MySQL's SQL modes and exploring the role of aggregations and GROUP BY strategy in ensuring performant, reliable data management.
Related reading
- Disable redis when many timeouts using spring boot
- Disconnect nodes in Cassandra
- Discuss the main issues governing concurrency control in a large distributed database environment
- Disk Space not freed up even after deleting keyspace from cassandra db and compaction
- Distributed C++ game server which use database
- Distributed Cache (redis) prefix keys
- Distributed Cache with Serialized File as DataStore in Oracle Coherence
- Distributed caching and locking with Redis in .NET Core

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.