0

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

是否有可能加快这一insert说法?如果是这样,怎么做?我的应用程序是用 PHP 编码的,我正在使用phpMyAdminmysqli

INSERT 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
4

2 回答 2

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
于 2013-05-10T14:43:04.600 回答
1

替换那些巨大的case陈述,lpad(MAX(shot.ID) + 1, 10, '0')你会没事的。

于 2013-05-10T14:38:54.460 回答