Machine Specs : macOS Sierra, Version 10.12.5, Memory 8 GB
MySQL Version: MySQL Enterprise Server - Advanced Edition (Commercial)3 innodb_version : 5.7.18
Problem : When same stored procedure is called concurrently by different sessions, the response time gets delayed a lot.
The below SQL is the one which takes 0.12s per execution. For simulating the issue, I am running the below SQL in a while loop which iterates for 30 times thereby taking an average of 3 seconds per Stored procedure execution.
However, when I run the same stored proc CONCURRENTLY from a different terminal/session, both the stored procs take nearly 50 seconds.
The tables are created in innodb and the buffer size and read/write io's are set with normal values.
Query
SELECT MIN(BEGIN_date) , MAX(END_date)
FROM employee e, department d
WHERE e.employee_ID = d.employee_ID
AND d.Department_ID = 72641 ;
Explain Extended Plan is below ( Indexes are perfectly used)
----+-------------+-------+------------+------+------------------+---------- --------+---------+-------------------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+------------------+------------------+---------+-------------------------------+------+----------+-------------+
| 1 | SIMPLE | d | NULL | ref | deptmnt_dept_idx | deptmnt_dept_idx | 4 | const | 808 | 100.00 | NULL |
| 1 | SIMPLE | e | NULL | ref | Emp_Name_Dt _idx | Emp_Name_Dt _idx | 4 | radar_bridge_db.d.Employee_ID | 156 | 100.00 | Using index |
+----+-------------+-------+------------+------+------------------+------------------+---------+-------------------------------+------+----------+-------------+
Stored Proc:
CREATE PROCEDURE `fetchEmployeeDetails`()
BEGIN
DECLARE l_StartDate DATE DEFAULT CURRENT_DATE()-INTERVAL 7 DAY;
DECLARE l_EndDate DATE DEFAULT CURRENT_DATE();
DECLARE I_Range INT DEFAULT 0;
WHILE I_Range < 30 DO
SELECT MIN(BEGIN_date) , MAX(END_date)
INTO l_StartDate, l_EndDate FROM employee e, department d
WHERE e.employee_ID = d.employee_ID AND d.Department_ID = 72641 ;
SET I_Range = I_Range + 1;
END WHILE;
END;
The same SP when called CONCURRENTLY from a different terminal/session, both the SPs take nearly 50 seconds.
The sample is run on both MySQL Community Version and also on Enterprise edition ( with thread pool ON) on the same Mac Machine, but the issue persists. Please let me know if you have any ways to resolve this performance issue.
CREATE TABLE `employee` (
`COMPANY_ID` int(11) NOT NULL,
`COMPANY_NAME` varchar(255) CHARACTER SET utf8 NOT NULL,
`EMPLOYEE_ID` int(11) NOT NULL,
`BEGIN_DATE` timestamp NULL DEFAULT NULL,
`END_DATE` timestamp NULL DEFAULT NULL,
KEY `Emp_Name_Dt_idx` (`EMPLOYEE_ID`,`BEGIN_DATE`,`END_DATE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `department` (
`Employee_ID` int(11) NOT NULL,
`Department_ID` int(11) NOT NULL,
KEY `deptmnt_dept_idx` (`Department_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1