这个mysql插件可以加速吗?

时间:2013-05-10 14:34:51

标签: php optimization insert mysqli

insert是我的应用程序的一部分,允许用户拍照并将其存储在数据库中。对于存储在数据库中的所有文件名,有一个非常具体的命名约定,这就是我的插入具有如此强烈的case语句的原因。我猜这与我的慢表现有关。为shots插入新patient时,数据库中存储的shots很少或没有patient,我的应用程序运行得更快。但是,如果shots我添加insert已经在数据库中存储了很多,那么性能会慢得多。

是否有可能加快phpMyAdmin声明的速度?如果是这样,怎么样?我的应用程序是用PHP编码的,我正在使用mysqliINSERT INTO shot (ID, VISIT_ID, PATIENT_ID ,IMG_FILENAME ,SHRUNK_IMG_FILENAME ,SUBDIR ,SUBSUBDIR ,IMG_FILE_FORMAT ,EYE) SELECT ( SELECT (max(ID) + 1) FROM SHOT ) ,( SELECT max(ID) FROM visit WHERE visit.patient_id = " . $_SESSION['id'] ." ) ," . $_SESSION['id'] . " ,( SELECT CASE WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100 THEN CONCAT ( '00000000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99 THEN CONCAT ( '0000000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999 THEN CONCAT ( '000000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999 THEN CONCAT ( '00000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99999 THEN CONCAT ( '0000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999999 THEN CONCAT ( '000' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999999 THEN CONCAT ( '00' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99999999 THEN CONCAT ( '0' ,(MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999999999 THEN CONCAT ( (MAX(shot.ID) + 1) ,'r.jpg' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999999999 THEN CONCAT ( (MAX(shot.ID) + 1) ,'r.jpg' ) END ) ,( SELECT CASE WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100 THEN CONCAT ( '00000000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99 THEN CONCAT ( '0000000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999 THEN CONCAT ( '000000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999 THEN CONCAT ( '00000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99999 THEN CONCAT ( '0000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999999 THEN CONCAT ( '000' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999999 THEN CONCAT ( '00' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 1000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 99999999 THEN CONCAT ( '0' ,(MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 10000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 999999999 THEN CONCAT ( (MAX(shot.ID) + 1) ,'s.bmp' ) WHEN ( SELECT MAX(shot.ID) + 1 FROM shot ) < 100000000000 AND ( SELECT MAX(shot.ID) + 1 FROM SHOT ) > 9999999999 THEN CONCAT ( (MAX(shot.ID) + 1) ,'s.bmp' ) END ) ,$SUB ,$ISUB ,'124' ,'0' FROM SHOT

{{1}}

2 个答案:

答案 0 :(得分:2)

它可能会更快,但至少要短得多..为什么在使用LPAD时使用案例?

INSERT INTO shot (ID, VISIT_ID, PATIENT_ID ,IMG_FILENAME ,SHRUNK_IMG_FILENAME ,SUBDIR ,SUBSUBDIR ,IMG_FILE_FORMAT ,EYE)
SELECT 
    x.ID + 1,
    (
        SELECT max(ID)  FROM visit  WHERE visit.patient_id = " . $_SESSION['id'] ."
    ),
    " . $_SESSION['id'] . ",
    CONCAT (LPAD(MAX(x.ID) + 1, 10, 0), 'r.jpg'),
    CONCAT (LPAD(MAX(x.ID) + 1, 10, 0), 's.bmp'),
    $SUB,
    $ISUB,
    '124',
    '0'
FROM SHOT x

答案 1 :(得分:1)

将这些巨大的case语句替换为lpad(MAX(shot.ID) + 1, 10, '0'),您就可以了。