Common SQL Errors

MySQL Error 3170 ER_CAPACITY_EXCEEDED: Memory Capacity Exceeded Fix Guide

Galaxy Team
August 8, 2025

The server aborts a statement because it requests more memory than the internal capacity limit allows.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 3170 ER_CAPACITY_EXCEEDED?

MySQL error 3170 ER_CAPACITY_EXCEEDED occurs when a query needs more RAM than the server’s built-in limit. Reduce the data processed or raise tmp_table_size and max_heap_table_size to resolve the issue.

Error Highlights

Typical Error Message

ER_CAPACITY_EXCEEDED

Error Type

Resource Error

Language

MySQL

Symbol

ER_CAPACITY_EXCEEDED was added in 5.7.9.

Error Code

3170

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 3170 ER_CAPACITY_EXCEEDED?

MySQL raises error 3170 (SQLSTATE HY000) when an operation runs out of the in-memory capacity hard limit. The engine stops the statement to protect stability, so queries fail or transactions roll back until memory use is reduced or limits are increased.

The error string shows the number of bytes requested and the component that triggered the allocation, helping you trace the root cause quickly.

What Causes This Error?

Large hash joins, sorts, GROUP BY aggregations, or temporary tables can exceed the allocator threshold, especially when join_buffer_size, sort_buffer_size, or tmp_table_size are too small for the dataset processed.

Operations against the MEMORY storage engine, oversized JSON documents, or heavy use of user variables may also exhaust the server’s memory quota and raise ER_CAPACITY_EXCEEDED.

How to Fix MySQL error 3170

Locate the failing statement via Performance Schema or the slow query log, then either rewrite it to process fewer rows or raise the memory parameters involved.

If system RAM allows, increase tmp_table_size and max_heap_table_size so MySQL can materialize larger internal tables without failing. Monitor memory status counters to confirm the change works.

For heavy joins and sorts, carefully enlarge join_buffer_size and sort_buffer_size, tracking overall consumption to avoid operating-system OOM.

Common Scenarios and Solutions

A reporting query with nested derived tables often spikes memory. Break it into smaller stages or persist intermediate results to on-disk tables to stay below the limit.

Insert activity on user-created MEMORY tables can exceed size caps. Convert those tables to InnoDB or add LIMIT clauses to keep them small.

Best Practices to Avoid This Error

Monitor memory usage metrics continuously in tools like Galaxy so you detect growth early. Align tmp_table_size and max_heap_table_size with peak load expectations and review them after every data-volume jump.

Write queries with selective WHERE filters, use covering indexes, and avoid SELECT * on large tables. Schedule resource-intensive ETL during off-peak windows.

Related Errors and Solutions

ER_OUT_OF_SORTMEMORY (1038) arises when sort_buffer_size is too small. ER_TOO_BIG_ROWSIZE (1114) appears when a single row exceeds engine limits. Both errors are resolved by reducing data size or tuning memory variables similarly.

Common Causes

Insufficient tmp_table_size or max_heap_table_size

Materialized derived tables and GROUP BY results use MEMORY tables until they reach the lower of these two variables. If the result grows beyond that limit, error 3170 fires.

Large hash joins or sorts

Complex joins on wide tables allocate large hash structures inside join_buffer_size or sort_buffer_size, quickly breaching the capacity threshold.

Unbounded MEMORY engine tables

User-created MEMORY tables never spill to disk. Continuous inserts can exceed their size cap and trigger ER_CAPACITY_EXCEEDED.

Huge JSON documents or user variables

Storing or parsing very large JSON blobs in memory can surpass the allocator’s ceiling within a single statement.

Related Errors

ER_OUT_OF_SORTMEMORY (1038)

Raised when sort_buffer_size is insufficient during ORDER BY or GROUP BY operations.

ER_TOO_BIG_ROWSIZE (1114)

Occurs when a single row exceeds the maximum size allowed by the storage engine.

ER_TMP_TABLE_FULL (1114)

Triggered when an on-disk temporary table exhausts its file-size limit.

FAQs

How do I know which variable to increase?

Check the operation named in the error text. Memory tables rely on tmp_table_size and max_heap_table_size, while joins and sorts depend on join_buffer_size and sort_buffer_size.

Is raising memory limits always safe?

Only if the server has free RAM. Increase gradually and monitor total_allocated_memory to avoid operating-system OOM.

Can Galaxy help prevent this error?

Galaxy highlights memory-heavy queries, profiles their resource use, and suggests optimizations via its AI copilot before they reach production.

Does restarting MySQL clear the problem?

A restart frees memory temporarily, but the error returns if the same query still exceeds limits. Fix the query or tune settings instead.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo