r/mysql • • Oct 15 '26

question Question on Memory parameters

Hi,

Its aurora mysql serverless version 8.0, with Min ~8ACU and MAX ~80ACU.

We have a complex UI query with "union all" and other criterias and its avg response time is <1 sec. But we see many times the query runs for 15minutes+ causing the errors in app side. This query executed ~5k times per hour on an avg and most executions finishin <1sec.

We see this query creates on an avg ~5 temp tables internally because of its internal "union" operator etc.., thus suspects if the below two parameters are actually playing a role in making this query crawl intermittently from ~1sec till ~15minutes. i.e. "temptable_max_ram" and "temptable_max_mmap". As because we run the same query manually and it does finish in 1sec. So suspect if during peak its causing the issue for some high volume customers even the same runs fine manually when we runs in the database.

We see the swap usage is staying ~1-2GB (sometime spikes to 3GB). Free memory trend showing spiky , but at worst scenarios it shows ~40GB(not below that). So want to understand, if in this situation, its fine to bump up the above parameters to 2GB each from curent 1GB (which is default value)?

3 Upvotes

11 comments sorted by

1

u/AnnaLeeCoop Oct 16 '26

Yes, bumping both temptable_max_ram and temptable_max_mmap to 2GB is completely safe if you consistently have 40GB of free memory sitting there. We run into this on our own production setups too. That massive 15-minute spike happens because once the result set exceeds the RAM limit, the engine spills those temp tables to disk, and the resulting I/O latency crushes the query.

I'd also recommend double-checking the EXPLAIN plan during peak load, since sometimes a UNION ALL gets forced into a disk-based temp table anyway if an outer sort or grouping shifts unexpectedly.

1

u/Stock-Dark-1663 Oct 17 '26

What is the symptom we should look for to ensure and confident about , we really getting short of these two parameters "temptable_max_ram and temptable_max_mmap" so it needs a increase? Also by howmuch.

1

u/Significant_Tune9219 Oct 16 '26

I would not bump those two to 2GB hoping the 15 minute runs go away. Free memory that never drops under about 40GB, with swap sitting around 1-2GB, means the instance is not out of RAM when you look at it, so a cap of 1GB is probably not the cliff. Five internal temp tables on a query that runs thousands of times an hour can still spill, but a spill turns one second into a few, not fifteen, unless the plan itself changed. Compare a slow execution to a fast one before you touch the defaults: same plan, and the slow one converting the temptable to disk, then the cap matters. Different plan, or the slow ones lined up with the instance sitting at the 8 ACU floor, and raising temptable_max_ram just spends memory you didn't need.

1

u/Stock-Dark-1663 Oct 17 '26

You are spot on u/Significant_Tune9219 , u/AnnaLeeCoop

We were also wondering, if the temp table spill to disk can cause these quick queries to run for so many minutes. However, It appeared to be a plan change , we luckily got one bind value for which we reproduced the bad plan. Its seems really odd that there is no instrumentation in aurora mysql to go back in time and see if the plan which query took was bad or good.

It seems once this particular bind( with comparatively high volume results) gets hardparsed and compiled and gets the bad plan , subsequent runs go for this same suboptimal plan untill the hard parse again occurs and optimizer picks the good binds. So that creates a bad situation. Also not sure , if this means we should rather use literals in the code then?

Also now when we think to pin the exact good plan , it seems there is no way but to play around using manual hints. For adding the hints to mimic the goodplan will be code change. Hoe my understanding is correct here.

ALso team mates suggesting , a parameter called "STATS_SAMPLE_PAGES" which is set as default -20, will bumping that up(to ~200+) will make the optimizer by default go for good plan. Is this correct understanding?

In regards to the parameters "temptable_max_ram" and "temp_table_max_mmap" , is there any symptoms based on which one can take decision if the temp memory is getting short for these temp heavy which are critical UI queries and rely on subsecond response?

1

u/Significant_Tune9219 Oct 17 '26

Raising STATS_SAMPLE_PAGES only makes the next stats refresh less noisy, and it will not stop Aurora from caching the plan of whichever bind was hard-parsed, so the high-volume value still sticks until the next hard parse. Switching those calls to literals trades that for a plan per value, so a hint in the statement is the actual pin. For the temp caps, Created_tmp_disk_tables only tells you something when the plan hash stayed the same; if the hash flipped, the minutes are the bad plan, not the memory limit.

1

u/Stock-Dark-1663 Oct 17 '26 edited Oct 17 '26

Thank you u/Significant_Tune9219

I was thinking , this parameter STATS_SAMPLE_PAGES will make the table/column/index statistics more accurate such that, the first occurrence of bad plan which is happening for certain bind value(which holds comparatively more data) will not happen in the first place itself. Basically i was assuming this parameter to make the optimizer enable with more accurate stats to come up with correct plan all the time. Please correct me if my understanding is wrong here?

Edit:- I see something interesting in below blog, its saying "MySQL has no execution plan cache" does that means it does hardparse or does the optimization every time for each of the thousands executions of the same query?

https://use-the-index-luke.com/blog/2011-07-16/planning-for-reuse

1

u/MatterDecent9033 Oct 20 '26

I don't think STATS_SAMPLE_PAGES will help here. MySQL doesn't keep a plan around between executions, Aurora included, so there's no bad plan from the first bind that sticks. Every run gets planned with the value it was actually given. And for `col = ?` the optimizer barely uses the sampled stats. It looks the value up in the index (an index dive) to estimate the rows, so it most likely already knows the big value matches a lot of them. It picks that plan anyway, and that plan is slow.

I'd look at why it's slow. Run EXPLAIN ANALYZE for the slow value and for a fast one and compare where the rows go. I had something similar at work, where the same query was fast or slow depending on which account it ran for. The fix there was a composite index, and after that it was fast for every account. If you can't add an index, a hint in the query will pin the plan.

The big temp tables are probably the same story. The bad plan reads far more rows, so it builds bigger temp tables. Raising the caps won't hurt with 40 GB free, but I don't think that's where the 15 minutes go.

1

u/Stock-Dark-1663 Oct 20 '26

Thank you u/MatterDecent9033

As you said "MySQL doesn't keep a plan around between executions, Aurora included, so there's no bad plan from the first bind that sticks. Every run gets planned with the value it was actually given. "

In regards to the root cause, i am wondering , in such scenario what must be the cause of the same binds running so much longer(In minutes) during run time and fast(in sub seconds) when trying to run manually by passing the exact values.

Or the binds for which the slowness has been complained might be different than what we think of? will try to check this.

And yes as you rightly said "Raising the caps won't hurt with 40 GB free, but I don't think that's where the 15 minutes go." it does not appear to be just the saturated temp/disk spill, cause the sub second query to go for minutes run time. So we are hesitating to bump up value of "temptable_max_ram" and "temptable_max_mmap" to 2gb from default 1gb.

1

u/MatterDecent9033 Oct 21 '26

Tbh, sounds like it could just be the buffer pool. When you run it by hand after a slow run, the slow run has already pulled that customer's pages into memory, so yours is fast. A big customer means a lot of pages, and on Aurora every miss goes to network storage, so I see how cold vs warm could be minutes vs a second.

It's easy to check. Pick a big customer nobody has queried for a while and run it twice by hand. If the first run is slow and the second one is fast, that's it. The slow query log helps too: if the slow runs show the same Rows_examined as your manual run, it's the same plan, just reading from storage instead of memory.

If that's it, the fix is reading fewer pages, usually an index that covers the filter and the sort so it doesn't jump to the table for every row. On Serverless v2 the buffer pool also grows with capacity, so a higher minimum ACU keeps more of it cached.

1

u/Aggressive_Ad_5454 Oct 15 '26

What varies between these short and long query runs? Can you identify anything, maybe a difference in a WHERE, ON, or ORDER BY clause?

The parameters you mention will make a difference if you have a vast filesort step in your execution plan.

At any rate it will help us help you if you read this and then edit your post to tell us more.

1

u/razin_the_furious Oct 15 '26

Is optimization of the query out of the question? Or is it as optimized as it can be, and more memory is the next step?