I am running one query which is faster on MySQL5.6 than MySQL8. It takes around 4 seconds in MySQL5.6 whereas takes 32 secs in MySQL8. As per MySQL documentation MySQL8 is faster but I have seen the opposite case.
Query:
select id
from defaultitem
where masterId='3b1e9726-12ea-4adf-a6ef-f0950075fec4'
and mainID in (
select id
from campaign
where deleteflag=0
and validflag=1
and deptid='9d9d7e0f-7256-4ac6-9398-378ae9a0239e')
Show create table:
defaultitem - CREATE TABLE `defaultitem` (
`id` varchar(255) NOT NULL,
`value` varchar(50) NOT NULL,
`masterId` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK22505D768EC27364` (`masterId`),
CONSTRAINT `FK22505D768EC27364` FOREIGN KEY (`masterId`) REFERENCES `combomaster` (`masterid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
campaign - CREATE TABLE `campaign` (
`id` varchar(255) NOT NULL,
`deptid` varchar(255) DEFAULT NULL,
`deleteflag` tinyint(4) DEFAULT '0',
`validflag` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `FK47DDEA51F157E732` (`deptid`),
CONSTRAINT `FK47DDEA51F157E732` FOREIGN KEY (`deptid`) REFERENCES `dept` (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
EXPLAIN query in MySQL5.6 -
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+--------+----------------------------+--------------------+---------+-----------------------------------+--------+------------------------------------+
| 1 | SIMPLE | defaultitem | ref | FK22505D768EC27364 | FK22505D768EC27364 | 768 | const | 812528 | Using index condition; Using where |
| 1 | SIMPLE | campaign | eq_ref | PRIMARY,FK47DDEA51F157E732 | PRIMARY | 767 | db.defaultitem.mainID | 1 | Using where |
+----+-------------+-------------------+--------+----------------------------+--------------------+---------+-----------------------------------+--------+------------------------------------+
EXPLAIN QUERY in MySQL8 -
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+------------+------+----------------------------+--------------------+---------+-------+--------+----------+-------------+
| 1 | SIMPLE | campaign | NULL | ref | PRIMARY,FK47DDEA51F157E732 | FK47DDEA51F157E732 | 768 | const | 15 | 1.00 | Using where |
| 1 | SIMPLE | defaultitem | NULL | ref | FK22505D768EC27364 | FK22505D768EC27364 | 768 | const | 787517 | 10.00 | Using where |
+----+-------------+-------------------+------------+------+----------------------------+--------------------+---------+-------+--------+----------+-------------+
innodb_buffer_pool_size is same in both versions.
I assumed that MySQL 8 would be faster.
Your query is functionally identical to:
SELECT DISTINCT x.id
FROM defaultitem x
JOIN campaign y
ON y.id = x.mainid
AND y.deleteflag = 0
AND y.validflag = 1
AND y.deptid = '9d9d7e0f-7256-4ac6-9398-378ae9a0239e'
WHERE x.masterId = '3b1e9726-12ea-4adf-a6ef-f0950075fec4';
...so why not write it that way.
A key on some combination of (deptid,validflag,deleteflag) would probably help.
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.