简体   繁体   English

如何减少查询的执行时间

[英]How can I reduce execution time of a query

When I run this query, it takes almost around 30 min to complete.当我运行此查询时,大约需要 30 分钟才能完成。 How can I reduce the execution time?如何减少执行时间?

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

These are my tables - I have a lot of data in this table这些是我的表 - 我在这个表中有很多数据

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

I need to run this query randomly but for taking log time I can't run it.我需要随机运行此查询,但由于需要记录时间,我无法运行它。 Because it's stuck all other因为它被卡住了所有其他

Create an index for the combination of columns [userid] and [work_date] in table [vfusion.attendance_checkout]为表 [vfusion.attendance_checkout] 中列 [userid] 和 [work_date] 的组合创建索引

This speeds up the JOIN这加快了 JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM