[Solved] I want to optimize this SQL Query

EverSQL Database Performance Knowledge Base

I want to optimize this SQL Query

Database type:

I have query like this:

DELIMITER $$

USE `kpbaru`$$

DROP PROCEDURE IF EXISTS `getAllUmurPegawai`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllUmurPegawai`(IN id_user VARCHAR(20),i_tahun INT)
BEGIN
    DECLARE currdate INT;
    DECLARE birthdate INT;
    DECLARE numRows INT;
    DECLARE numIteration INT;
    DECLARE tempMonth INT;
    DECLARE umur INT;
    SET numRows = (SELECT COUNT(*) FROM pegawai);
    SET numIteration = 1;

    WHILE numIteration <= numRows DO
        SET currdate = i_tahun; 
        SET birthdate = (SELECT EXTRACT(YEAR FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration AND ID_USER=id_user)));
        SET umur = currdate - birthdate;
        SET tempMonth = (SELECT EXTRACT(MONTH FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration AND ID_USER=id_user)));
        IF umur < 56 THEN
            UPDATE pegawai SET pegawai.STATUS_PEGAWAI='Belum Pensiun',pegawai.BULAN_PENSIUN=tempMonth,STATUS_PENSIUN=1 WHERE pegawai.INDEXING = numIteration AND ID_USER=id_user;
        ELSE 
            IF umur = 56 THEN
                UPDATE pegawai SET pegawai.STATUS_PEGAWAI='Pensiun',pegawai.BULAN_PENSIUN=tempMonth,STATUS_PENSIUN=1 WHERE pegawai.INDEXING = numIteration AND ID_USER=id_user;
            ELSE 
                UPDATE pegawai SET pegawai.STATUS_PEGAWAI='Pensiun',pegawai.BULAN_PENSIUN=tempMonth,STATUS_PENSIUN=0 WHERE pegawai.INDEXING = numIteration AND ID_USER=id_user; 
            END IF;
        END IF;
        SET numIteration = numIteration + 1;
    END WHILE;
END$$

DELIMITER ;

i want to optimize this query, because this query will search each age in eeach people. This query runs very slow if we have big data (>1000 rows). Any one know how to optimize it?

I've tried some query like this:

UPDATE pegawai AS p LEFT JOIN(SELECT INDEXING, CAST(CASE WHEN (i_tahun - EXTRACT(YEAR FROM(SELECT TGL_lAHIR FROM pegawai WHERE ID_USER=id_user))) < 56 THEN 'Belum Pensiun' ELSE 'Pensiun' END AS VARCHAR(20))AS statusPegawai, EXTRACT(MONTH FROM(SELECT TGL_LAHIR FROM pegawai WHERE ID_USER=id_user))AS bulanPensiun, CAST(CASE WHEN (i_tahun - EXTRACT(YEAR FROM(SELECT TGL_lAHIR FROM pegawai WHERE ID_USER=id_user))) <= 56 THEN 1 ELSE 0 END AS INT)AS statusPensiun FROM pegawai WHERE ID_USER=id_user GROUP BY INDEXING)AS m
    ON p.ID_USER = m.ID_USER
    SET p.STATUS_PEGAWAI = m.statusPegawai, p.BULAN_PENSIUN = m.bulanPensiun, p.STATUS_PENSIUN = m.statusPensiun
    WHERE p.ID_USER = id_user;

but it still wrong. Here the error is:

Query :

CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllUmurPegawai`(in id_user varchar(20),i_tahun int) BEGIN  DECLARE currdate INT;...

Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(20))as statusPegawai, extract(month from(select TGL_LAHIR from pegawai w' at line 29

Solution will be apreciated! :D

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `pegawai` ADD INDEX `pegawai_idx_id_user_indexing` (`ID_USER`,`INDEXING`);
The optimized query:
SELECT
        p.STATUS_PEGAWAI 
    FROM
        pegawai AS p 
    LEFT JOIN
        (
            SELECT
                pegawai.INDEXING,
                CAST(CASE 
                    WHEN (pegawai.i_tahun - EXTRACT(YEAR 
                FROM
                    (SELECT
                        TGL_lAHIR 
                    FROM
                        pegawai 
                    WHERE
                        ID_USER = id_user))) < 56 THEN 'Belum Pensiun' 
                    ELSE 'Pensiun' END AS VARCHAR (20)) AS statusPegawai,
EXTRACT(MONTH 
FROM
(SELECT
    TGL_LAHIR 
FROM
    pegawai 
WHERE
    ID_USER = id_user)) AS bulanPensiun,
CAST(CASE 
    WHEN (pegawai.i_tahun - EXTRACT(YEAR 
FROM
    (SELECT
        TGL_lAHIR 
    FROM
        pegawai 
    WHERE
        ID_USER = id_user))) <= 56 THEN 1 
    ELSE 0 END AS INT) AS statusPensiun 
FROM
pegawai 
WHERE
pegawai.ID_USER = pegawai.id_user 
GROUP BY
pegawai.INDEXING) AS m 
    ON p.ID_USER = m.ID_USER 
WHERE
p.ID_USER = p.id_user

Related Articles



* original question posted on StackOverflow here.