如何减少查询的执行时间

时间:2019-01-10 13:42:10

标签: mysql

运行此查询时,大约需要30分钟才能完成。如何减少执行时间?

INSERT INTO vfusion.attendance_report_data_2
    SELECT 
        CONCAT(attendance_checkin.userid, UNIX_TIMESTAMP(DATE(IFNULL(attendance_checkin.work_date, 0)))) AS id,
        attendance_checkin.userid, 
        attendance_checkin.work_date,
        attendance_checkin.checkintime_data as in_time,
        attendance_checkout.checkouttime_data as out_time,
        IFNULL(attendance_checkin.work_shift,0) as work_shift
    FROM 
        vfusion.attendance_checkin
    INNER JOIN 
        vfusion.attendance_checkout ON attendance_checkin.userid = attendance_checkout.userid
                                    AND attendance_checkin.work_date = attendance_checkout.work_date
     ON DUPLICATE KEY 
          UPDATE
              in_time = in_time,
              out_time = out_time,
              work_shift = attendance_checkin.work_shift

这些是我的表-该表中有很多数据

CREATE TABLE attendance_checkout 
(
    id_attendance_checkout BIGINT(11) NOT NULL,
    userid INT(11) DEFAULT NULL,
    work_date DATE DEFAULT NULL,
    checkouttime_data DATETIME DEFAULT NULL,
    work_shift INT(11) DEFAULT NULL,
    PRIMARY KEY (id_attendance_checkout)
)  ENGINE=INNODB DEFAULT CHARSET=LATIN1;

CREATE TABLE attendance_checkin 
(
    id_attendance_checkin BIGINT(11) NOT NULL,
    userid INT(11) DEFAULT NULL,
    work_date DATE DEFAULT NULL,
    checkintime_data DATETIME DEFAULT NULL,
    work_shift INT(11) DEFAULT NULL,
    PRIMARY KEY (id_attendance_checkin)
)  ENGINE=INNODB DEFAULT CHARSET=LATIN1;

CREATE TABLE attendance_report_data_2 
(
    id_attendance_report_data BIGINT(11) NOT NULL,
    userid INT(11) NOT NULL DEFAULT '0',
    work_date DATE NOT NULL DEFAULT '0000-00-00',
    in_time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
    out_time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
    work_shift INT(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (id_attendance_report_data , in_time , out_time , work_date , userid , work_shift)
)  ENGINE=INNODB DEFAULT CHARSET=LATIN1

我需要随机运行此查询,但是要花费日志时间,我无法运行它。 因为它卡住了其他所有

1 个答案:

答案 0 :(得分:0)

为表[vfusion.attendance_checkout]中的[userid]和[work_date]列的组合创建索引

这可以加快JOIN