【发布时间】:2015-06-12 03:54:36
【问题描述】:
以下视图包含 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;
由于上述视图被频繁使用,应用程序的速度受到极大影响。有什么办法可以加快它的性能?
【问题讨论】:
-
请为使用
SELECT的SELECT和视图内的SELECT提供EXPLAIN SELECT ...;。 -
通过表扫描应用
SELECT COUNT(*) FROMsaving_account_ledger_view_new1; _Probably_ that many rows are put into a tmp table with no indexes, then theWHERE` 子句的值是多少。 -
innodb_buffer_pool_size的值是多少?有多少 RAM 可用?
标签: mysql performance view