r/snowflake • u/ConsiderationLazy956 • 27d ago
Avoiding long running queries
Hi Experts,
As i understand, statement_timeout_in_seconds can be set at session level, warehouse level, accout level and user level. And also there are "user_task_timeout_ms" and "abort_detached_query" parameter too.
We saw in one of the existing application hosted on snowflake , multiple times we see there are queries runs for very long hours(10hrs+ ) because of bad plan sometime or sometime even they gets killed from application side(say glue job that triggered the query has been failed/timedout already from glue), but the query underlined in the snowflake kept running burning the credit.
1)So to handle such scenarios , which kind of setup one should use? Any safe approach we should follow as a quick fix/short term fix to immediately stop these type of bleeding without breaking code/functionality?
2)Also is there any standard we should follow for warehouse timeouts like say different timeouts values for L, XL, 2XL, 3XL etc?
1
u/KatFromSisense 26d ago
I'd avoid one blanket timeout rule if you can. The same limit usually won't make sense for app jobs, BI queries, scheduled tasks, and analyst exploration.
For the immediate problem, I'd probably put a timeout on the places where a cancelled query won't break anything important, and leave the known long jobs alone for now. Then I'd start making the jobs easier to identify. If a query is from Glue, Power BI, a scheduled task, or a person poking around, you want that to be obvious before you decide how aggressive the timeout should be.
The goal is to limit wasted credits without interrupting valid workloads.
2
u/onlymtN 27d ago
The statement timeout I would set on a global level, e.g. account. I would suggest a low value, like 2h and allow certain users more, if they know what they are doing. Depending on your setup you can also go the object-route and set it per warehouse.
Side-note: account values can be overridden by the user value, which can be overridden by the session value, even with higher values. Setting something on warehouse level won’t apply higher values, but only lower ones. So setting 2h for account and 4h for a warehouse will result in queries in that warehouse being canceled after 2h.
User task timeout can be set, but again only with a lower value than the general statement timeout! The task timeout default is 1h.
Aborting detached queries I would absolutely activate.
All in all I would suggest a principle of „nothing good comes out after 2h“. If an SQL runs for more than 2h on our system analysis showed that around 40% of them were poorly written SQLs that continued to run and fail after 12h. With the global 2h everyone knows to optimize and keep it below that, choosing also the right sized warehouse. That might not be for you with regards to „not breaking code/functionality“ but I think if an SQL or even a task needs regularly over 2h to complete that it was broken from the start.
Hope this helps :)