加快MySQL的观点

时间:2015-06-12 03:54:36

标签: mysql performance view

以下视图包含2000行(并不多),但获取值需要4秒左右。

CREATE 
ALGORITHM = UNDEFINED 
DEFINER = `root`@`localhost` 
SQL SECURITY DEFINER
VIEW `saving_account_ledger_view_new1` AS
SELECT 
    (CASE
        WHEN
            ((`mt`.`Tr_Type` = 'DJV')
                OR (`mt`.`Tr_Type` = 'DCA')
                OR (`mt`.`Tr_Type` = 'DBK'))
        THEN
            'Deposit'
        ELSE (CASE
            WHEN
                ((`mt`.`Tr_Type` = 'WJV')
                    OR (`mt`.`Tr_Type` = 'WCA')
                    OR (`mt`.`Tr_Type` = 'WBK'))
            THEN
                'Withdraw'
        END)
    END) AS `Particulars`,
    `mt`.`SBAc_No` AS `SBAc_No`,
    `mt`.`Tr_Date` AS `Tr_Date`,
    `mt`.`Tr_No` AS `Tr_No`,
    SUM((CASE
        WHEN (`mt`.`Tr_Type` = 'DCA') THEN `mt`.`Pri_Amt`
        WHEN (`mt`.`Tr_Type` = 'DJV') THEN `mt`.`Pri_Amt`
        WHEN (`mt`.`Tr_Type` = 'DBK') THEN `mt`.`Pri_Amt`
        ELSE 0
    END)) AS `Deposit`,
    SUM((CASE
        WHEN (`mt`.`Tr_Type` = 'WCA') THEN `mt`.`Pri_Amt`
        WHEN (`mt`.`Tr_Type` = 'WJV') THEN `mt`.`Pri_Amt`
        WHEN (`mt`.`Tr_Type` = 'WBK') THEN `mt`.`Pri_Amt`
        ELSE 0
    END)) AS `Withdraw`,
    (IFNULL((SELECT 
                    SUM((CASE
                            WHEN
                                ((`mt2`.`Tr_Type` = 'DJV')
                                    OR (`mt2`.`Tr_Type` = 'DCA')
                                    OR (`mt2`.`Tr_Type` = 'DBK'))
                            THEN
                                `mt2`.`Pri_Amt`
                            ELSE 0
                        END))
                FROM
                    `sb_loan_trans` `mt2`
                WHERE
                    ((`mt2`.`Tr_Date` <= `mt`.`Tr_Date`)
                        AND (`mt2`.`SBAc_No` = `mt`.`SBAc_No`)
                        AND (`mt2`.`Tr_No` < `mt`.`Tr_No`))),
            0) - IFNULL((SELECT 
                    SUM((CASE
                            WHEN
                                ((`mt2`.`Tr_Type` = 'WJV')
                                    OR (`mt2`.`Tr_Type` = 'WCA')
                                    OR (`mt2`.`Tr_Type` = 'WBK'))
                            THEN
                                `mt2`.`Pri_Amt`
                            ELSE 0
                        END))
                FROM
                    `sb_loan_trans` `mt2`
                WHERE
                    ((`mt2`.`Tr_Date` <= `mt`.`Tr_Date`)
                        AND (`mt2`.`SBAc_No` = `mt`.`SBAc_No`)
                        AND (`mt2`.`Tr_No` < `mt`.`Tr_No`))),
            0)) AS `Balance`
FROM
    `sb_loan_trans` `mt`
GROUP BY `mt`.`Tr_Date` , `mt`.`Tr_No` , `mt`.`SBAc_No` , `mt`.`Tr_Type`

表格结构如下: -

CREATE TABLE `sb_loan_trans` (
`Tr_No` bigint(20) NOT NULL,
`SBAc_No` bigint(20) NOT NULL,
`Tr_Date` datetime NOT NULL,
`Tr_Type` char(10) NOT NULL,
`Pri_Amt` double NOT NULL,
`Int_Amt` double NOT NULL DEFAULT '0',
`Penal_Int_Amt` double NOT NULL DEFAULT '0',
`FA_Pri` bigint(20) NOT NULL DEFAULT '0',
`FA_Int` bigint(20) NOT NULL DEFAULT '0',
`Fa_Penal_Int` bigint(20) NOT NULL DEFAULT '0',
`Recept_No` varchar(10) DEFAULT '0',
`Disurb_Code` int(11) DEFAULT '0',
`CREATEDID` char(10) DEFAULT NULL,
`CREATEDDATETIME` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`UPDATEDID` char(10) DEFAULT NULL,
`UPDATEDDATETIME` datetime DEFAULT NULL,
PRIMARY KEY (`Tr_No`),
KEY `IDX_SB_LN1` (`Tr_Date`,`Tr_No`,`SBAc_No`,`Tr_Type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

由于经常使用上述视图,因此应用程序的速度受到严重影响。有没有办法加快其表现?

1 个答案:

答案 0 :(得分:0)

  

什么是物质化的视图?物化视图(MV)就是   预先计算的(物化的)查询结果。与简单的VIEW不同   物化视图的结果存储在某个地方,通常是在   表。当需要立即响应时,使用物化视图   以及物化视图所基于的查询将花费很长时间   产生结果。物化视图必须刷新一次   而。它取决于物化视图的频率   刷新,内容的实际程度如何。基本上是物化视图   可以立即刷新或推迟,可以完全刷新或   到某个时间点。 MySQL不提供物化视图   通过它自己。但是你自己很容易构建物化视图。

在此处查看更多信息 http://www.fromdual.com/mysql-materialized-views

虽然这就是你想要阅读的内容