問(wèn)題
我們提供的服務(wù)有:網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、渾源ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的渾源網(wǎng)站制作公司
開(kāi)發(fā)有一條insert SQL 是定時(shí)任務(wù),內(nèi)容如下, 每天需要約執(zhí)行5-10次,筆者從select部分開(kāi)始跑了一會(huì)沒(méi)有結(jié)果,就放棄了,效率很差.
INSERT INTO bs_sf_yd_flow_check ( `merchantNo`, `sfBusinessId`, `sfMerOrderId`, `sfTradeTime`, `sfTradeAmount`, `sfDebitAmount`, `sfCreditAmount`, `sfBalance`, `sfAccNo`, `ydBusinessId`, `ydMerOrderId`, `ydTradeTime`, `ydTradeAmount`, `ydDebitAmount`, `ydCreditAmount`, `ydBalance`, `ydAccNo`, `tradeType`, `status`, `checkStatus`, `account_date`, `checkTime`, `createTime`, `updateTime`, `sfTradeType` ) SELECT sf.merchantNo, sf.businessId, sf.merOrderId, sf.completeTime, sf.tradeAmount, sf.debitAmount, sf.creditAmount, sf.balance, sf.accNo, mr.businessId, mr.localOrderId, mr.tradeTime, CASE WHEN mr.fromUserId = '116' THEN mr.amount * - 1 ELSE mr.amount END AS ydTradeAmount, CASE WHEN mr.fromUserId = '116' THEN mr.amount WHEN mr.toUserId = '116' THEN 0 END AS ydDebitAmount, CASE WHEN mr.fromUserId = '116' THEN 0 WHEN mr.toUserId = '116' THEN mr.amount END AS ydCreditAmount, mr.accountBalance, IFNULL( CASE WHEN mr.fromUserId = '116' THEN mr.fromUserId WHEN mr.toUserId = '116' THEN mr.toUserId END, '' ) AS ydAccNo, mr.tradeType, 0, CASE WHEN ABS(sf.tradeAmount) = mr.amount THEN 3 WHEN mr.amount IS NULL THEN 6 ELSE 5 END AS checkStatus, sf.tradeTime, NOW(), NOW(), NOW(), sf.tradeType FROM bs_sf_flow sf LEFT JOIN money_record mr ON mr.bussflowno = sf.merOrderId AND sf.tradeTime = DATE_FORMAT(mr.tradeTime, '%Y-%m-%d') AND mr.ischeck = 1 AND ( mr.fromUserId = '116' OR mr.toUserId = '116' ) WHERE sf.tradeTime = '20161212' AND sf.accNo = '00620000000010269449'
兩個(gè)表的表結(jié)構(gòu)如下
SF: CREATE TABLE `bs_sf_flow` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵', `businessId` varchar(40) NOT NULL COMMENT '賬戶流水號(hào)', `merchantNo` varchar(40) NOT NULL COMMENT '商戶號(hào)', `merOrderId` varchar(40) NOT NULL COMMENT '商戶訂單號(hào)', `completeTime` datetime DEFAULT NULL COMMENT '訂單完成時(shí)間', `tradeAmount` decimal(14,2) DEFAULT NULL COMMENT '交易金額', `debitAmount` decimal(14,2) DEFAULT NULL COMMENT '借方發(fā)生額', `creditAmount` decimal(14,2) DEFAULT NULL COMMENT '貸方發(fā)生額', `balance` decimal(14,2) DEFAULT NULL COMMENT '虛擬賬戶余額', `accNo` varchar(40) NOT NULL COMMENT '虛擬賬戶賬號(hào)', `tradeType` varchar(40) NOT NULL COMMENT '業(yè)務(wù)類型', `status` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '狀態(tài)0:初始', `createTime` datetime NOT NULL COMMENT '創(chuàng)建時(shí)間', `updateTime` datetime DEFAULT NULL COMMENT '更新時(shí)間', `merPlatAcctAlias` varchar(32) DEFAULT NULL COMMENT '商戶平臺(tái)收款賬戶別名,平臺(tái)開(kāi)立的賬戶賬號(hào)別名,當(dāng)商戶開(kāi)立多個(gè)賬戶時(shí),必輸', `merPlatAcctNo` varchar(80) DEFAULT NULL COMMENT '平臺(tái)賬戶別名對(duì)應(yīng)的賬號(hào)', `tradeTime` date NOT NULL COMMENT '賬務(wù)日期', PRIMARY KEY (`id`), KEY `idx_merOrderId` (`merOrderId`), KEY `idx_tradeTime` (`tradeTime`,`accNo`) ) ENGINE=InnoDB AUTO_INCREMENT=1502748 DEFAULT CHARSET=utf8 COMMENT='三方流水表'; MR: CREATE TABLE `money_record` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `toUserId` int(10) unsigned DEFAULT NULL COMMENT '資金匯入者的userid', `toUserNickname` varchar(100) DEFAULT NULL COMMENT '資金匯入者的昵稱', `fromUserId` int(10) unsigned DEFAULT NULL COMMENT '資金匯出者的userid', `fromUserNickname` varchar(100) DEFAULT NULL COMMENT '資金匯出者的昵稱', `amount` decimal(14,2) NOT NULL COMMENT '交易金額金額', `accountBalance` decimal(14,2) NOT NULL COMMENT '此交易完成后的賬戶余額', `businessId` varchar(50) NOT NULL DEFAULT '' COMMENT '關(guān)聯(lián)的業(yè)務(wù)id,如充值訂單號(hào),提現(xiàn)批次號(hào)等', `tradeType` smallint(5) unsigned NOT NULL COMMENT '資金來(lái)往類型,小于1000的都是匯入,即資金增加;大于1000的都是匯出,即資金減少。詳情見(jiàn)CommonDef中TRADE_TYPE_*', `tradeTime` datetime NOT NULL COMMENT '交易發(fā)生時(shí)間', `tradeChannel` smallint(5) unsigned DEFAULT NULL COMMENT '交易渠道', `tradeComment` varchar(300) DEFAULT NULL COMMENT '交易備注', `loanId` int(10) unsigned DEFAULT NULL COMMENT '關(guān)聯(lián)的loanId', `loanTitle` varchar(64) DEFAULT NULL COMMENT '標(biāo)名稱', `loanPortraitPath` varchar(64) DEFAULT NULL COMMENT '標(biāo)頭像圖片的路徑', `bussflowno` varchar(128) DEFAULT NULL COMMENT '三方訂單號(hào)', `localOrderId` varchar(128) DEFAULT NULL COMMENT '本地業(yè)務(wù)id', `ischeck` smallint(2) DEFAULT '1' COMMENT '是否與三方對(duì)賬,0不對(duì),1對(duì)', `fromAccBalance` decimal(14,2) DEFAULT NULL COMMENT '轉(zhuǎn)出賬戶交易后余額', `toAccBalance` decimal(14,2) DEFAULT NULL COMMENT '轉(zhuǎn)入賬戶處理后余額', `batchNo` varchar(32) DEFAULT NULL COMMENT '交易批次號(hào)', `originUserId` int(10) DEFAULT '0' COMMENT '交易來(lái)源用戶', `updateTime` datetime DEFAULT NULL COMMENT '更新時(shí)間', `projectId` varchar(32) DEFAULT NULL COMMENT '項(xiàng)目ID', `tradePlatformType` smallint(5) unsigned DEFAULT '0' COMMENT '流水平臺(tái)類型0:三方;10:托管;20:三方至托管;30:托管至三方;', PRIMARY KEY (`id`), KEY `fk_money_record_toUserId` (`toUserId`), KEY `idx_money_record_fromUserId` (`fromUserId`), KEY `idx_money_record_tradeTime` (`tradeTime`), KEY `idx_money_record_businessId` (`businessId`), KEY `idx_tradeType` (`tradeType`,`loanId`), KEY `idx_updateTime` (`updateTime`,`amount`) ) ENGINE=InnoDB AUTO_INCREMENT=7166457 DEFAULT CHARSET=utf8
分析處理
單獨(dú)看select部分的執(zhí)行計(jì)劃如下,可以發(fā)現(xiàn)被驅(qū)動(dòng)表mr使用的是mr.fromUserId和mr.toUserId列上的索引,但是這兩個(gè)索引作為刪選條件并不好,rows有150多萬(wàn)行;
最好的情況是使用連接條件on mr.bussflowno = sf.merOrderId中mr.bussflowno列的索引,所以建議在mr.bussflowno上創(chuàng)建一個(gè)普通索引。
創(chuàng)建索引之后的執(zhí)行計(jì)劃如下,rows變?yōu)?,type從index_merge變成等值查詢r(jià)eg,只需0.2s便跑出select結(jié)果。
總結(jié)
被驅(qū)動(dòng)表的連接條件on后面一定要有索引;
驅(qū)動(dòng)表where后面的列最后要有篩選條件比較好的索引;