Back
Database Management

Reliable database management for modern, high-performance systems.

Back
Data Modernisation

Reliable database management for modern, high-performance systems.

Back
Data Engineering

Reliable database management for modern, high-performance systems.

Back
Analytics & Intelligence

Reliable database management for modern, high-performance systems.

Back
Data Strategy Consulting

Reliable database management for modern, high-performance systems.

How a Single Unfiltered Date Condition Crashed a GCP Cloud SQL Instance

Aravind Rajamani
Published on
December 4, 2026

A customer came to us after their GCP-managed MySQL instance went down in production. They needed a proper root cause analysis - not a guess, and not “add an index and hope.” We treat this the same way every time: find the exact failure window, find the exact query, prove the cause with the database's own execution data, and only then talk about a fix.

Working with the client, we pinned down the outage window precisely: the instance was unavailable from approximately 12:00 PM to 12:30 PM on September 9, 2026. That gave us a 30-minute window to search, rather than a haystack of a week's worth of traffic.

Architectural Diagram:

How the outage was traced and fixed, step by step

Step 1 - Catching the Culprit in Query Insights

The first stop for any GCP Cloud SQL slowdown is Query Insights. We filtered the load graph to the incident window (roughly 11:45 AM to 12:30 PM, giving ourselves a 15-minute buffer before the reported outage) and looked at what was consuming CPU right before the instance stopped responding.

One query stood out immediately: a single execution that ran for approximately 200 seconds. Nothing else executed on the instance for the rest of that window afterward - this wasn't just a slow query sitting alongside normal traffic, it was the query that took the instance down. That made it query #1 on our list of suspects, and as the investigation below shows, the only suspect we needed.

Cloud SQL Query Insights - CPU/CPU-wait load and the topqueries by total load, the starting point for isolating the incident.

Step 2 - Recovering the Full Query from Logs Explorer

Here's a detail that trips a lot of people up: Query Insights truncates long queries. Anything past a certain length shows up as a partial fragment - enough to get a lead, not enough to actually analyze. Our flagged query was a long, multi-join aggregation, so Query Insights gave us only a partial SELECT statement.

To get the exact, complete query text, we cross-validated against GCP Logs Explorer, which retains this project's Cloud SQL logs for six months. We applied the same time window and the same fragment we'd already identified from Query Insights as a search filter, and pulled the full, exact query as it was actually sent to the database - not a reconstruction, the real thing.

GCP Logs Explorer - filtering Cloud SQL database logs torecover the complete, untruncated query text.

That gave us the exact query responsible forthe outage:

SELECT DG.state_id AS state_id, DG.warehouse_id AS warehouse_id, wh.warehouse_code AS warehouse_code,   TRUNCATE(IF(wh.exclude_gain_loss = '1',       SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward' AND LOWER(DG.type) <> 'gain', DG.cargo_gross_wt, 0)))         - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward' AND LOWER(DG.type) <> 'loss', DG.cargo_gross_wt, 0))),       SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward', DG.cargo_gross_wt, 0)))         - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward', DG.cargo_gross_wt, 0)))   ), 3) AS qty,   IF(wh.exclude_gain_loss = '1',       SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward' AND LOWER(DG.type) <> 'gain', DG.no_of_bags, 0)))         - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward' AND LOWER(DG.type) <> 'loss', DG.no_of_bags, 0))),       SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward', DG.no_of_bags, 0)))         - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward', DG.no_of_bags, 0)))   ) AS bags,   DG.commodity AS commodity, wh.name AS name, client.entity_name AS entity_name,   category.category_name AS state, sub.category_name AS location,   client.id AS client_id, dtr_commodity_name.commodity_name AS commodity_name FROM dtr_audit INNER JOIN dtr_grain AS DG ON DG.dtr_audit_id = dtr_audit.id LEFT JOIN warehouse AS wh ON wh.id = DG.warehouse_id LEFT JOIN client AS client ON client.id = DG.client_id LEFT JOIN category AS category ON category.id = DG.state_id LEFT JOIN category AS sub ON sub.id = wh.address LEFT JOIN dtr_commodity_name AS dtr_commodity_name ON DG.commodity = dtr_commodity_name.id WHERE dtr_audit.approved = '2' AND wh.active = '1' AND wh.whType <> 'CM' AND (1 = 1)   AND DG.entry_date <= '2026-09-08' GROUP BY DG.client_id, DG.warehouse_id, DG.commodity ORDER BY DG.state_id, DG.warehouse_id;

Step 3 - Validating Every Index and the Execution Plan

With the exact query in hand, the next instinct for most people is “check the indexes.” We did - thoroughly. We pulled the full index listing for every table this query touches and mapped every column used in the query (every JOIN key, every WHERE condition, every GROUP BY and ORDER BY column) against what was actually indexed.

The finding: nothing was missing. Every join in this query already resolves through the cheapest possible access path adatabase offers - a single-row primary-key lookup. We confirmed this directlywith EXPLAIN ANALYZE, which runs the query for real and reports exactly whathappened, row by row, rather than an estimate:

-> Sort: DG.state_id, DG.warehouse_id (actual time=46127.288..46133.401 rows=15583 loops=1)   -> Table scan on <temporary> (actual time=46094.964..46110.682 rows=15583 loops=1)     -> Aggregate using temporary table (actual time=46094.954..46094.954 rows=15583 loops=1)       -> Nested loop inner join (cost=1427659.10 rows=184688) (actual time=5.805..27637.043 rows=1547185 loops=1)         -> Nested loop left join (cost=1298377.61 rows=369376) (actual time=5.779..21601.023 rows=1588808 loops=1)           -> Nested loop left join (cost=1169096.12 rows=369376) (actual time=5.762..19474.066 rows=1588808 loops=1)             -> Nested loop left join (cost=1039814.63 rows=369376) (actual time=5.751..17613.536 rows=1588808 loops=1)               -> Nested loop left join (cost=910533.14 rows=369376) (actual time=5.736..16165.011 rows=1588808 loops=1)                 -> Nested loop inner join (cost=781251.65 rows=369376) (actual time=5.700..12123.715 rows=1588808 loops=1)                   -> Filter: (DG.entry_date <= DATE'2026-09-08') (cost=299211.35 rows=1377258) (actual time=0.125..6959.339 rows=2927605 loops=1)                     -> Table scan on DG (cost=299211.35 rows=2754516) (actual time=0.123..6603.953 rows=2928404 loops=1)                   -> Filter: (dtr_audit.approved = '2') (cost=0.25 rows=0.5) (actual time=0.003..0.004 rows=1 loops=1588808)                     -> Single-row index lookup on dtr_audit using PRIMARY (id=DG.dtr_audit_id) (cost=0.25 rows=1) (actual time=0.003..0.003 rows=1 loops=1588808)                 -> Filter: ((wh.`active` = '1') and (wh.whType <> 'CM')) (cost=0.25 rows=0.3) (actual time=0.002..0.002 rows=1 loops=2927605)                   -> Single-row index lookup on wh using PRIMARY (id=DG.warehouse_id) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=2927605)               -> Filter: (`client`.id = DG.client_id) (cost=0.25 rows=1) (actual time=0.002..0.002 rows=1 loops=1588808)                 -> Single-row index lookup on client using PRIMARY (id=DG.client_id) (cost=0.25 rows=1) (actual time=0.002..0.002 rows=1 loops=1588808)             -> Single-row index lookup on category using PRIMARY (id=DG.state_id) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=1588808)           -> Single-row index lookup on sub using PRIMARY (id=wh.address) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=1588808)         -> Filter: (cast(DG.commodity as double) = cast(dtr_commodity_name.id as double)) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=1588808)           -> Single-row index lookup on dtr_commodity_name using PRIMARY (id=DG.commodity) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=1588808)  Total actual time: 46,133.401 ms (~46.13 s)  |  Rows returned: 15,583

Every index was correctly built and correctly used. That's precisely what made this outage interesting - the standard first fix didn't apply.

Step 4 - The Real Finding: Scanning 100% of the Table for 15,000 Rows

The execution plan told the real story. The report this query powers only ever needed to return about 15,000 summary rows. To produce those 15,000 rows, the query was scanning and joining approximately 2.9 million transaction records - essentially the entire underlying table.

The reason traced back to a single WHERE condition: a date filter written as “entry_date <= today,” with no lower bound. Measured directly from the plan, that condition let through 2,927,605 of2,928,404 scanned rows - 99.97% of the table. It wasn't filtering anything. Every time this report ran, it was being asked to recompute a running total from the entire transaction history, from day one, rather than from any meaningful starting point.

This is the part worth underlining for anyonedoing this kind of investigation: an index only helps when a condition excludesmost of the data. When a condition matches almost everything, the query planneris right to ignore the index and scan the table directly - which is exactlywhat MySQL did here. No index change could have fixed this.

Step 5 - The Fix: A Business Decision, Not a Database Tweak

Because the bottleneck was data volume, not access method, the most effective fix available was bounding that date filter - giving the report a real starting point instead of “the entire history, every time.” We took this to the client's business team.

Their answer was clear: the reporting time window could not be changed - the report genuinely needs to reflect the full history for their use case. What the business team could agree to, after review, was a change to which warehouse types are included in the calculation: replacing

wh.whType <> 'CM'

with

wh.whType NOT IN ('CM', 'Virtual', 'EX')

We also restructured the query itself: the four supporting lookups used only to label the output (client name, category, warehouse location, commodity name) were moved to run after the totals are calculated instead of before, so they execute against the ~15,000 final summary rows instead of against every one of the ~1.5 million rows in-flight. This is a pure rewrite - same logic, same result set - and it required no schema or index changes.

Together, these two changes took the query from a measured 46.1 seconds (the reproducible baseline for this query pattern, captured immediately after the incident) down to 35.04 seconds - validated the same way as every other number in this analysis, with a real EXPLAIN ANALYZE run against production data:

SELECT   agg.state_id,   agg.warehouse_id,   wh.warehouse_code,   TRUNCATE(agg.qty_raw, 3) AS qty,   agg.bags,   agg.commodity,   wh.name,   client.entity_name,   category.category_name AS state,   sub.category_name        AS location,   client.id                AS client_id,   dtr_commodity_name.commodity_name FROM (   SELECT     DG.client_id,     DG.warehouse_id,     DG.commodity,     ANY_VALUE(DG.state_id) AS state_id,     IF(wh.exclude_gain_loss = '1',        SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward' AND LOWER(DG.type) <> 'gain', DG.cargo_gross_wt, 0)))          - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward' AND LOWER(DG.type) <> 'loss', DG.cargo_gross_wt, 0))),        SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward', DG.cargo_gross_wt, 0)))          - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward', DG.cargo_gross_wt, 0)))     ) AS qty_raw,     IF(wh.exclude_gain_loss = '1',        SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward' AND LOWER(DG.type) <> 'gain', DG.no_of_bags, 0)))          - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward' AND LOWER(DG.type) <> 'loss', DG.no_of_bags, 0))),        SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'inward', DG.no_of_bags, 0)))          - SUM(COALESCE(IF(LOWER(dtr_audit.form_type) = 'outward', DG.no_of_bags, 0)))     ) AS bags   FROM dtr_audit   INNER JOIN dtr_grain AS DG ON DG.dtr_audit_id = dtr_audit.id   INNER JOIN warehouse AS wh ON wh.id = DG.warehouse_id   WHERE dtr_audit.approved = '2'     AND wh.active = '1'     AND wh.whType NOT IN ('CM', 'Virtual', 'EX')          -- business-approved change     AND DG.entry_date <= '2026-09-08'   GROUP BY DG.client_id, DG.warehouse_id, DG.commodity ) AS agg LEFT JOIN warehouse AS wh              ON wh.id = agg.warehouse_id LEFT JOIN client                       ON client.id = agg.client_id LEFT JOIN category                     ON category.id = agg.state_id LEFT JOIN category AS sub              ON sub.id = wh.address LEFT JOIN dtr_commodity_name           ON agg.commodity = dtr_commodity_name.id ORDER BY agg.state_id, agg.warehouse_id;
-> Nested loop left join (cost=138587.60 rows=0) (actual time=34917.219..35044.363 rows=14357 loops=1)   -> Nested loop left join (cost=110870.60 rows=0) (actual time=34917.207..35017.238 rows=14357 loops=1)     -> Nested loop left join (cost=83153.60 rows=0) (actual time=34917.195..35006.860 rows=14357 loops=1)       -> Nested loop left join (cost=55436.60 rows=0) (actual time=34917.190..34997.843 rows=14357 loops=1)         -> Nested loop left join (cost=27719.60 rows=0) (actual time=34917.161..34929.241 rows=14357 loops=1)           -> Sort: agg.state_id, agg.warehouse_id (cost=2.60..2.60 rows=0) (actual time=34917.134..34920.107 rows=14357 loops=1)             -> Table scan on agg (cost=2.50..2.50 rows=0) (actual time=34907.668..34909.678 rows=14357 loops=1)               -> Materialize (cost=0.00..0.00 rows=0) (actual time=34907.665..34907.665 rows=14357 loops=1)                 -> Table scan on <temporary> (actual time=34891.292..34901.767 rows=14357 loops=1)                   -> Aggregate using temporary table (actual time=34891.281..34891.281 rows=14357 loops=1)                     -> Nested loop inner join (cost=859087.49 rows=110869) (actual time=4.473..19761.816 rows=1412103 loops=1)                       -> Nested loop inner join (cost=781479.35 rows=221738) (actual time=4.452..12830.383 rows=1449273 loops=1)                         -> Filter: ((DG.entry_date <= DATE'2026-09-08') and (DG.warehouse_id is not null)) (cost=299294.15 rows=1377672) (actual time=0.113..7449.916 rows=2927883 loops=1)                           -> Table scan on DG (cost=299294.15 rows=2755344) (actual time=0.111..6938.069 rows=2929232 loops=1)                         -> Filter: ((wh.`active` = '1') and (wh.whType not in ('CM','Virtual','EX'))) (cost=0.25 rows=0.2) (actual time=0.002..0.002 rows=1 loops=2927883)                           -> Single-row index lookup on wh using PRIMARY (id=DG.warehouse_id) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=2927883)                       -> Filter: (dtr_audit.approved = '2') (cost=0.25 rows=0.5) (actual time=0.004..0.005 rows=1 loops=1449273)                         -> Single-row index lookup on dtr_audit using PRIMARY (id=DG.dtr_audit_id) (cost=0.25 rows=1) (actual time=0.004..0.004 rows=1 loops=1449273)           -> Single-row index lookup on wh using PRIMARY (id=agg.warehouse_id) (cost=0.25 rows=1) (actual time=0.000..0.000 rows=1 loops=14357)         -> Filter: (`client`.id = agg.client_id) (cost=0.25 rows=1) (actual time=0.005..0.005 rows=1 loops=14357)           -> Single-row index lookup on client using PRIMARY (id=agg.client_id) (cost=0.25 rows=1) (actual time=0.004..0.004 rows=1 loops=14357)       -> Filter: (category.id = agg.state_id) (cost=0.25 rows=1) (actual time=0.000..0.000 rows=1 loops=14357)         -> Single-row index lookup on category using PRIMARY (id=agg.state_id) (cost=0.25 rows=1) (actual time=0.000..0.000 rows=1 loops=14357)     -> Single-row index lookup on sub using PRIMARY (id=wh.address) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=14357)   -> Filter: (cast(agg.commodity as double) = cast(dtr_commodity_name.id as double)) (cost=0.25 rows=1) (actual time=0.002..0.002 rows=1 loops=14357)     -> Single-row index lookup on dtr_commodity_name using PRIMARY (id=agg.commodity) (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=14357)  Total actual time: 35,044.363 ms (~35.04 s)  |  Rows returned: 14,357

Wrapping Up

Indexing and parameter tuningare not universal fixes. Sometimes the real lever is a WHERE condition or adate boundary - and that's a business conversation, not just a database one.

Every index this query used was correct. No configuration flag or query hint would have changed the outcome. The instance went down because a single report was written to reprocess its entire transaction history on every run, and the fix that mattered most wasn't technical at all - it was getting the business team to agree on what data the report actually needs to include.

That's the pattern worth remembering: before reaching for an index, a bigger cache, or a bigger instance tier, look at what the query is actually being asked to filter. If the WHERE clause isn't excluding meaningful data, no amount of indexing will fix it - and the fastest path forward may be a five-minute conversation with the people who own the business logic, not a longer one with the database.

Aravind Rajamani

Aravind Rajamani is a data engineering professional with 5+ years of experience in designing and building scalable data pipelines and architectures. His expertise includes ETL, data warehousing, real-time data processing, MongoDB, Python, and enterprise data engineering solutions.

Our Latest Blogs

From monitoring to modernization, GeoPITS provides expert database management services that enhance performance, strengthen security, and reduce operational costs.

Ready to Transform Your Data?

Geopits works alongside your team as a strategic partner, starting with stabilizing your current databases, then modernizing your data infrastructure, and ultimately helping you unlock the full potential of AI.

170

Happy Clients so far

2100+

Databases Managed

142+

Successful Migrations

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.