Mysql – Why does table SELECT become twice slower after restoring a db from 5.0.24 to 8.0.20

MySQLoptimization

I've got a new server so i had to configure MySQL on it, the old server had installed MySQL 5.0.24 so i've done backup of my DBs and restored them on newly installed MySQL 8.0.20 on the new server..

First of all i've done some simple SELECT queryes to test the speed of old and new server and till here both were almost fast the same..

When the queryes become more complex so with WHERE and GROUP BY (test done on a MyISAM table), the old server was fetching almost 13000 rows from almost 3m table in 3s while the new server with new MySQL was fetching the same rows in 21s so i started to do some test like changing the table from MyISAM to InnoDB in the new server and after that and after setting innodb_buffer_pool_size to 32G and i've got a fetch time of 9s a better result than 21s but anyway three times slower than the old MySQL 5.0.24…

The query i'm testing is the following (the WHERE for NPV_SC and NCASSA_SC in this case select all rows):

SELECT 
    DESCRIZIONE_SC AS DESCRIZIONE,
    COUNT(DESCRIZIONE_SC) AS QTA,
    SUM(IMPORTO_SC) AS IMPORTO
FROM
    scontrini
WHERE
    (NPV_SC , NCASSA_SC) IN ((1 , 1) , (1 , 2),
        (1 , 2),
        (2 , 1),
        (2 , 2),
        (3 , 1),
        (4 , 1),
        (4 , 2),
        (5 , 1),
        (6 , 1),
        (6 , 2),
        (7 , 1),
        (10 , 1),
        (11 , 1),
        (12 , 1),
        (13 , 1),
        (16 , 1),
        (23 , 1),
        (24 , 1),
        (24 , 2),
        (108 , 1))
        AND DATA_SC BETWEEN '2019-11-01' AND '2020-05-07'
GROUP BY DESCRIZIONE_SC
ORDER BY IMPORTO_SC DESC;

So the query above equals to:

SELECT 
    DESCRIZIONE_SC AS DESCRIZIONE,
    COUNT(DESCRIZIONE_SC) AS QTA,
    SUM(IMPORTO_SC) AS IMPORTO
FROM
    scontrini
WHERE DATA_SC BETWEEN '2019-11-01' AND '2020-05-07'
GROUP BY DESCRIZIONE_SC
ORDER BY IMPORTO_SC DESC;

Which still has same execution time

UPDATED: the above queries has changed after suggestion in comments on how to set the WHERE but anyway the fetch time is 5 seconds VS 3/2.80 seconds of old server with MySQL 5.0.24 (so the fetch time by optimizing the WHERE changed from 9s to 5 but still "far" from the old MySQL..)

The index are on NPV_SC, NCASSA_SC, DATA_SC, DESCRIZIONE_SC and IMPORTO_SC

Here is the create statment (without insignificant columns that was like other 10)

CREATE TABLE `scontrini` (
  `NPV_SC` int NOT NULL DEFAULT '0',
  `NCASSA_SC` int NOT NULL DEFAULT '0',
  `TIPODOC_SC` varchar(250) NOT NULL DEFAULT '',
  `DATA_SC` date NOT NULL DEFAULT '2001-01-01',
  `AZZ_SC` varchar(10) NOT NULL DEFAULT '0000',
  `NSC_SC` int NOT NULL DEFAULT '0',
  `ID_SC` int NOT NULL DEFAULT '0',
  `DESCRIZIONE_SC` varchar(250) DEFAULT '',
  `IMPORTO_SC` float(10,3) DEFAULT '0.000',
  PRIMARY KEY (`NPV_SC`,`NCASSA_SC`,`TIPODOC_SC`,`DATA_SC`,`AZZ_SC`,`NSC_SC`,`ID_SC`),
  KEY `PIUMENOVEN` (`NPV_SC`,`NCASSA_SC`,`DATA_SC`),
  KEY `REP` (`REPARTO_SC`),
  KEY `RC` (`RC_SC`),
  KEY `MENU` (`MENU_SC`),
  KEY `idx_scontrini_IMPORTO_SC` (`IMPORTO_SC`),
  FULLTEXT KEY `idx_scontrini_DESCRIZIONE_SC` (`DESCRIZIONE_SC`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here is the explain

+----+-------------+-----------+------------+------+-------------------------------------------------+------+---------+------+---------+----------+----------------------------------------------+
| id | select_type | table     | partitions | type | possible_keys                                   | key  | key_len | ref  | rows    | filtered | Extra                                        |
+----+-------------+-----------+------------+------+-------------------------------------------------+------+---------+------+---------+----------+----------------------------------------------+
| 1  | SIMPLE      | scontrini | NULL       | ALL  | PRIMARY,PIUMENOVEN,idx_scontrini_DESCRIZIONE_SC | NULL | NULL    | NULL | 2483404 | 11.11    | Using where; Using temporary; Using filesort |
+----+-------------+-----------+------------+------+-------------------------------------------------+------+---------+------+---------+----------+----------------------------------------------+

enter image description here

The server has 64GB Ram, 1.5TB os SSD and 10x Intel(R) Xeon(R) CPU E5-2630 @ 2.20GHz

Here is my my.ini hosted on github

Best Answer

Igor, you may find this workaround improves your speed on your MySQL 8.0.20 instance

SET GLOBAL internal_tmp_mem_storage_engine=MEMORY;

Refer to dba.stackexchange.com Question 267143 and look for Shane Bester's mention with the bug report. You will discover this was an error in 8.0.20, that will be corrected when 8.0.21 becomes available. Make this change to your my.ini as well to be in place after stop/start of your 8.0.20 instance.