我的MYSQL JOIN查询有问题。
为了获取数据列表,我总共有5个表需要将它们全部连接起来,这些表是:(daily_activity_control_card-man,personnels_master_data,daily_activity_control_card,machine_master_data和work_sheet_machine)。
以下是通过此查询获得的总行 SELECT COUNT(*) FROM table
我认为每个表中当前的总行数不会减慢查询过程。我有
daily_activity_control_card-man (52818 total rows),
personnel_master_data (203 total rows),
daily_activity_control_card (8115 total rows),
machine_master_data (204 total rows) and
work_sheet_machine (19959 total rows).
下面是解释的结果:
EXPLAIN `daily_activity_control_card-man`;
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| wo_number | varchar(50) | YES | MUL | NULL | |
| sheet_no | bigint(20) | YES | MUL | NULL | |
| step | bigint(20) | YES | MUL | NULL | |
| reg_no | varchar(12) | YES | | NULL | |
| start_time | datetime | YES | | NULL | |
| finish_time | datetime | YES | | NULL | |
| status_shift | varchar(14) | YES | | NULL | |
| status_working | varchar(16) | YES | | NULL | |
| status_man | varchar(12) | YES | | NULL | |
| machine_code | varchar(12) | YES | | NULL | |
| act_hours | float | NO | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
EXPLAIN `personnel_master_data`;
+----------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+--------------+------+-----+---------+-------+
| reg_no | varchar(25) | NO | PRI | NULL | |
| personnel_name | varchar(50) | YES | | NULL | |
| password | varchar(100) | YES | UNI | NULL | |
| web_token | longtext | NO | | NULL | |
| permission | int(11) | YES | | NULL | |
| status | varchar(35) | NO | | NULL | |
| online_status | varchar(20) | NO | | NULL | |
+----------------------------+--------------+------+-----+---------+-------+
EXPLAIN `daily_activity_control_card`;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| wo_number | varchar(50) | YES | MUL | NULL | |
| sheet_no | bigint(20) | YES | MUL | NULL | |
| step | bigint(20) | YES | MUL | NULL | |
| machine_code | varchar(12) | YES | MUL | NULL | |
| start_time | datetime | YES | | NULL | |
| finish_time | datetime | YES | | NULL | |
| note | longtext | YES | | NULL | |
| act_hours | float | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
EXPLAIN `machine_master_data`;
+-----------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+-------+
| code | varchar(12) | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| specs | varchar(50) | NO | | NULL | |
| manufucture | varchar(50) | NO | | NULL | |
| year_manufucture | int(6) | NO | | NULL | |
| purchase_price_dollar | bigint(20) | NO | | NULL | |
| purchase_price_rupiah | bigint(20) | NO | | NULL | |
| hour_day | int(12) | NO | | NULL | |
| rate_hour | int(12) | NO | | NULL | |
| machine_group | varchar(25) | NO | | NULL | |
| status | varchar(12) | NO | | NULL | |
| location | varchar(25) | NO | | NULL | |
+-----------------------+-------------+------+-----+---------+-------+
EXPLAIN `work_sheet_machine`;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| date_act | date | YES | | NULL | |
| wo_number | varchar(50) | YES | | NULL | |
| sheet | float | YES | | NULL | |
| reg_no | varchar(20) | YES | | NULL | |
| serial | float | YES | | NULL | |
| machine_code | varchar(20) | YES | | NULL | |
| description | varchar(500) | YES | | NULL | |
| crew_est | bigint(20) | YES | | NULL | |
| crew_act | bigint(20) | YES | | NULL | |
| jam_est | varchar(20) | YES | | NULL | |
| jam_act | varchar(20) | YES | | NULL | |
| note | varchar(100) | YES | | NULL | |
| manhour_est | float | YES | | NULL | |
| manhour_act | float | YES | | NULL | |
| machine_rate | bigint(50) | YES | | NULL | |
| totalhour_est | bigint(50) | YES | | NULL | |
| totalhour_act | bigint(50) | YES | | NULL | |
| status | varchar(12) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
以下是我的联接查询,用于从表中获取数据。
SELECT a.id
, a.wo_number
, a.sheet_no
, a.step
, a.reg_no
, DATE_FORMAT(a.start_time, '%Y-%m-%d %H:%i') start_time
, DATE_FORMAT(a.finish_time, '%Y-%m-%d %H:%i') finish_time
, CASE WHEN a.status_shift = 'shift_shift' THEN 'SF'
WHEN a.status_shift = 'shift_nonshift' THEN 'NS'
WHEN a.status_shift = 'long_shift' THEN 'LS' END status_shift
, CASE WHEN a.status_working = 'normal' THEN 'NN'
WHEN a.status_working = 'overtime' THEN 'OT' END status_working
, ROUND(a.act_hours, 1) act_hours
, b.personnel_name
, a.machine_code
, c.note
, e.manhour_est
, CASE WHEN a.status_man = 'CONTINUE' THEN 'C'
WHEN a.status_man = 'FINISH' THEN 'F' END status
, d.name
, d.specs
FROM `daily_activity_control_card-man` a
LEFT
JOIN personnel_master_data b
ON a.reg_no = b.reg_no
LEFT
JOIN daily_activity_control_card c
on a.wo_number = c.wo_number
AND a.sheet_no = c.sheet_no
AND a.machine_code = c.machine_code
LEFT
JOIN machine_master_data d
on a.machine_code = d.code
LEFT
JOIN work_sheet_machine e
on a.wo_number = e.wo_number
AND a.sheet_no = e.sheet
AND a.step = e.serial
WHERE a.status_man IN ('CONTINUE', 'FINISH')
GROUP
BY a.id HAVING COUNT(*) >= 1
有什么我可以执行的更快的查询吗?
我尝试将这种条件添加到查询WHERE DATE_FORMAT(a.start_time, '%Y-%m-%d') between '2018-07-09' AND '2018-08-09'
中,但根据phpmyadmin信息,它需要45秒才能获得2500多个行。
任何帮助将不胜感激。 谢谢。
答案 0 :(得分:0)
WHERE DATE_FORMAT(a.start_time, '%Y-%m-%d') between '2018-07-09' AND '2018-08-09'
不能使用索引,但是可以...
WHERE a.start_time BETWEEN '2018-07-09 00:00:00' AND '2018-08-09 23:59:59'
答案 1 :(得分:-4)
关于这种JOIN,MySQL很慢。
您可以尝试一些操作:
可能会减少执行时间。