我正在尝试编写一个查询,以获取在工厂工作的最新班次统计信息,但是myquery变得如此复杂,以至于在mysql工作台中执行将近12秒。是否有任何优化我的查询的建议?
SELECT c_time,
shift
INTO @c_time,
@shift
FROM roll_header_view
WHERE id = 'id';
SELECT f.*,
employee.name as employee_name,
employee.employee_code as employee_code
FROM roll_header_view f
LEFT OUTER JOIN employee_view employee
ON employee.id = f.employee_id
WHERE c_time <= @c_time
AND c_time >
(SELECT IFNULL(max(c_time), 0)
FROM roll_header_view
WHERE shift != @shift
AND c_time <= @c_time);
'CREATE TABLE `employee` (
`id` bigint(20) NOT NULL DEFAULT ''0'',
`mac_id` int(11) NOT NULL DEFAULT ''100'',
`c_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`u_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`c_by` varchar(100) NOT NULL DEFAULT ''100'',
`u_by` varchar(100) NOT NULL DEFAULT ''100'',
`isactive` varchar(1) NOT NULL DEFAULT ''Y'',
`employee_code` varchar(100) NOT NULL DEFAULT '''',
`name` varchar(100) NOT NULL DEFAULT '''',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1'
'CREATE TABLE `roll_header` (
`id` bigint(20) NOT NULL DEFAULT ''0'',
`mac_id` int(11) NOT NULL DEFAULT ''100'',
`c_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`u_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`c_by` varchar(100) NOT NULL DEFAULT ''100'',
`u_by` varchar(100) NOT NULL DEFAULT ''100'',
`isactive` varchar(1) NOT NULL DEFAULT ''Y'',
`roll_no` bigint(20) NOT NULL DEFAULT ''0'',
`actual_id` bigint(20) NOT NULL DEFAULT ''0'',
`req_cuttable_width` float NOT NULL DEFAULT ''0'',
`act_cuttable_width` float NOT NULL DEFAULT ''0'',
`color` varchar(100) NOT NULL DEFAULT '''',
`contract_no` varchar(100) NOT NULL DEFAULT '''',
`construction` varchar(100) NOT NULL DEFAULT '''',
`finish` varchar(100) NOT NULL DEFAULT '''',
`meter_yard_length` float NOT NULL DEFAULT ''0'',
`meter` float NOT NULL DEFAULT ''0'',
`yard` float NOT NULL DEFAULT ''0'',
`is_meter_yard` varchar(1) NOT NULL DEFAULT ''Y'',
`total_l_point` int(11) NOT NULL DEFAULT ''0'',
`penalty_point` float NOT NULL DEFAULT ''0'',
`trolly_no` varchar(100) NOT NULL DEFAULT '''',
`description` varchar(1000) NOT NULL DEFAULT '''',
`shift` varchar(100) NOT NULL DEFAULT '''',
`shift_end` varchar(1) NOT NULL DEFAULT ''0'',
`shift_start` varchar(1) NOT NULL DEFAULT ''0'',
`quality` varchar(100) NOT NULL DEFAULT ''A'',
`employee_id` bigint(20) NOT NULL DEFAULT ''1'',
PRIMARY KEY (`id`),
KEY `fk_roll_header_employee_id` (`employee_id`),
CONSTRAINT `roll_header_ibfk_1` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1'
我已经共享了此查询的所有相关数据,即表结构ER图解释等,请帮助我以更好的和面向性能的方式优化此查询 解释计划
答案 0 :(得分:0)
我试图弄清楚您的查询应该怎么做-我敢肯定有一种更好的方法来编写它,但是对我来说意义不大。
我已将其转换为SQL Fiddle,在这里:http://sqlfiddle.com/#!9/5435e2/6
问题是您的子查询
(SELECT IFNULL(max(c_time), 0)
FROM roll_header_view
WHERE shift != @shift
AND c_time <= @c_time);
正在对其比较的每一行执行,并且不能使用索引。如果您按以下方式重新编写查询,则该问题将消失。
SELECT c_time,
shift
INTO @c_time,
@shift
FROM roll_header
WHERE id = 'id';
SELECT @max_time = IFNULL(max(c_time), 0)
FROM roll_header
WHERE shift != @shift
AND c_time <= @c_time;
SELECT f.*,
employee.name as employee_name,
employee.employee_code as employee_code
FROM roll_header f
LEFT OUTER JOIN employee employee
ON employee.id = f.employee_id
WHERE f.c_time <= @c_time
AND f.c_time >
@max_time;