简体   繁体   中英

MySQL won't establish connections while running query that uses temporary

I have some SQL similar to the following:

select code, count(id) as count_total, count(distinct email) as 
count_distinct
from submissions where page_id = ?
group by code order by count_total desc

It's not the fastest query because the submissions table is big and there are a lot of possible emails and codes, but it doesn't need to run that often, so that's fine.

My problem though is that for some reason MySQL won't seem to accept new connections while this query is running. Existing connections can do their thing just fine, but new connections won't be established until it finishes. It's not a problem with too many connections either, I'm way under my max_connections and this happens whether there are a small number of connections or lots.

I feel I have basically narrowed it down to using temporary because when I remove group by code so that it no longer needs a temporary table it doesn't have this problem.

What would cause MySQL to not accept new connections while this query runs? If it matters, this server is on AWS RDS.

I believe because Group by wants to aggregate the values of a DB, it will acquire a write lock on the same else the results might be inconsistent. MySQL should ideally still accept read requests.

You can even lock a bunch of rows, without fetching any data by grouping them together using a dummy GROUP BY clause.

SELECT 1 FROM sometable WHERE somecondition GROUP BY 1 FOR UPDATE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM