I am using MySQL 8.0.27-18, with InnoDB as engine. I have a huge database, with few tables containing 100M+ rows in them. For example: TABLE_A with 110M and TABLE_B with 300M+ rows. Sometimes I had to join these tables in fact, and the even worst part is, need to perform SORTing on the results.
Instead of going with complex queries, I wanted to start with a simple query like the SELECT statement below,
SELECT MERCH_NUM,TRANS_DATE, TRANS_AMOUNT, ETC... FROM TABLE_A WHERE MERCH_NUM = 'XXXXYYYYZZZZ' ;
This table TABLE_A have 110M rows, and the given MERCH_NUM have ~9M rows. Also, we have an Index on the MERCH_NUM column too. Now, I can able to execute this query at the cost of Minutes. So, when I looked at statistics, I see below.
Duration / Fetch
0.78 sec / 270.782 sec
As I understood, MySQL took less than 1 sec to complete the query execution but took forever to fetch or retrieve or transfer the results data to client, If I am not wrong.
This fetch part became a huge bottleneck to my application now.
What is causing this huge fetch times, particularly when dealing with huge data? And, How to resolve this?
P.S: Our server has 24GB of RAM allocated.
IN ( SELECT ... )is quite inefficient. Can you turn tha into aJOIN? It sounds like the system is giving the user too much flexibility in constructing the query -- heading to inefficient queries on the large table.