Имеется несколько хранимок()не моих. Вопос: Можно ли их как нибудь оптимизировать(в плане экономии времяни выполнения)
Первая:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102.
DELIMITER $$
USE `sisense`$$
DROP PROCEDURE IF EXISTS `DataTable`$$
CREATE DEFINER=`root`@`%` PROCEDURE `DataTable`()
BEGIN
DROP TABLE IF EXISTS sisense.datatable;
CREATE TABLE sisense.datatable
SELECT c.`id` AS `CustomerID`, c.`FirstName`, c.`LastName`,cn.name AS `Country`, cn7.name AS Campaign, c.`subCampaignId`, c.`regTime`,
IF(c.`regTime`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`regTime`))) AS LastRegdate,
MONTH(c.`regTime`) AS RegMonth,
YEAR(c.`regTime`) AS RegYear,
CASE
WHEN (IF(c.`regTime`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`regTime`)))) >30 THEN '30+'
WHEN (IF(c.`regTime`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`regTime`)))) < 8 THEN '0-7'
WHEN (IF(c.`regTime`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`regTime`)))) < 15 THEN '8-14'
WHEN (IF(c.`regTime`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`regTime`)))) < 31 THEN '15-30'
END AS RegCategory,
c.`lastlogindate`,
IF(c.`lastlogindate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastlogindate`))) AS LastLogdate,
IF(c.`lastlogindate`='0000-00-00 00:00:00','NOLOGIN',(IF(DATEDIFF(DATE(NOW()),DATE(c.`lastlogindate`)) > 14, 'PASSIVE' , 'ACTIVE'))) AS ClientStatus,
c.`lastDepositDate`,
IF(c.`lastDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastDepositDate`))) AS LastDepDate,
c.`lastTimeInvestment`,
IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`))) AS LastInvDate,
CASE
WHEN (IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`)))) = -1 THEN 'Non Trader'
WHEN (IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`)))) >30 THEN '30+'
WHEN (IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`)))) < 8 THEN '0-7'
WHEN (IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`)))) < 15 THEN '8-14'
WHEN (IF(c.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastTimeInvestment`)))) < 31 THEN '15-30'
END AS lastInvestmentCategory,
c.`lastCallDate`,
IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`))) AS LastComentDate,
CASE
WHEN (IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`)))) = -1 THEN 'NOCOMMENT'
WHEN (IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`)))) >30 THEN '30+'
WHEN (IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`)))) < 8 THEN '0-7'
WHEN (IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`)))) < 15 THEN '8-14'
WHEN (IF(c.`lastCallDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`lastCallDate`)))) < 31 THEN '15-30'
END AS CommentCategory,
(SELECT COUNT(`calls`.`id`) FROM nrgbinary_platform.`calls` WHERE nrgbinary_platform.`calls`.`clientId` = c.id AND `lastCallDate` != '0000-00-00 00:00:00') commentCount,
c.`firstDepositDate`,
IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`))) AS LastFTDDate,
MONTH(c.`firstDepositDate`) AS FTDMonth,
YEAR(c.`firstDepositDate`) AS FTDYear,
CASE
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)))) = -1 THEN 'NOFTD'
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)))) >30 THEN '30+'
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)))) < 8 THEN '0-7'
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)))) < 15 THEN '8-14'
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)))) < 31 THEN '15-30'
END AS FTDCategory,
c.`currency`, c.`isLead`, c.`isDemo`, c.`saleStatus`, c.`leadStatus`, cn3.lastBalance AS lastBalance ,
CASE
WHEN cn3.lastBalance IS NULL THEN 'NULL'
WHEN cn3.lastBalance < 501 THEN '0-500'
WHEN cn3.lastBalance >500 THEN '500+'
END AS BalanceCategory,
cn3.pnl AS pnl ,
cn4.totalBonusWithdrawals AS totalBonusWithdrawals,
cn4.firstDepositAmount AS firstDepositAmount,cn5.username AS UserName,
cn6.param AS CampaignsParam,
IFNULL((SELECT NAME FROM nrgbinary_platform.desks WHERE id = (SELECT u.`deskId` FROM nrgbinary_platform.user_desks u WHERE u.`userId` LIKE c.`employeeInChargeId` AND u.`deskId` != 40 LIMIT 1)),'no desk') AS DeskName,
# Deposit Count to and from 1.3.2014
(
(SELECT IFNULL(COUNT(amountUSD),0) FROM nrgbinary_platform.customer_deposits WHERE customerId = c.`Id` AND DATE(confirmTime) < '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod != 'WebMoney' ))+
(SELECT IFNULL(COUNT(amountUSD),0) FROM nrgbinary_platform.customer_deposits WHERE customerId = c.`Id` AND DATE(confirmTime) >= '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod NOT LIKE 'Payoneer' ))
) AS DepositCount,
# Deposit amount to and from 1.3.2014
(
(SELECT IFNULL(SUM(amountUSD),0) FROM nrgbinary_platform.customer_deposits WHERE customerId = c.`Id` AND DATE(confirmTime) < '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod != 'WebMoney' ))+
(SELECT IFNULL(SUM(amountUSD),0) FROM nrgbinary_platform.customer_deposits WHERE customerId = c.`Id` AND DATE(confirmTime) >= '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod NOT LIKE 'Payoneer' ))
) AS DepositAmount,
IF(c.`firstDepositDate`='0000-00-00 00:00:00','NOFTD',(IF(DATEDIFF(DATE(NOW()),DATE(c.`firstDepositDate`)) > 30, '30+' , 'NEW'))) AS FTDStatus,
# FTD Amount to and from 1.3.2014
CASE
WHEN (IF(c.`firstDepositDate`='0000-00-00 00:00:00',1,0)) = 1 THEN 0
WHEN (IF(DATE(c.`firstDepositDate`)<'2014-03-01',1,0)) = 1 THEN (SELECT SUM(d.`amountUSD`) FROM nrgbinary_platform.customer_deposits d WHERE DATE(d.`confirmTime`)=DATE(c.`firstDepositDate`) AND d.`customerid`=c.`id` AND paymentMethod NOT LIKE 'Bonus' AND paymentMethod NOT LIKE 'Wallet1' AND paymentMethod NOT LIKE 'WebMoney')
WHEN (IF(DATE(c.`firstDepositDate`)>='2014-03-01',1,0)) = 1 THEN (SELECT SUM(d.`amountUSD`) FROM nrgbinary_platform.customer_deposits d WHERE DATE(d.`confirmTime`)=DATE(c.`firstDepositDate`) AND d.`customerid`=c.`id` AND paymentMethod NOT LIKE 'Bonus' AND paymentMethod NOT LIKE 'Wallet1' )
END AS FTDAmount,
(
(SELECT IFNULL(SUM(amountUSD),0) FROM nrgbinary_platform.withdrawals WHERE customerId = c.`Id` AND DATE(confirmTime) < '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod != 'WebMoney' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod NOT LIKE 'InactivityFee' AND paymentMethod NOT LIKE 'Fees' AND STATUS LIKE 'approved' ))+
(SELECT IFNULL(SUM(amountUSD),0) FROM nrgbinary_platform.withdrawals WHERE customerId = c.`Id` AND DATE(confirmTime) >= '2014-03-01' AND (paymentMethod NOT LIKE 'Wallet1' AND paymentMethod != 'Bonus' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod NOT LIKE 'InactivityFee' AND paymentMethod NOT LIKE 'Fees' AND STATUS LIKE 'approved'))
) AS WithdrawAmount,
(SELECT username FROM nrgbinary_platform.users WHERE id = (SELECT employeeid FROM nrgbinary_platform.calls d WHERE d.`clientid` = c.`id` LIMIT 1)) AS FCEmployee,
c.`referlink` AS ReferalLink
FROM nrgbinary_platform.customers c
LEFT JOIN nrgbinary_platform.country cn ON cn.id=c.`Country`
LEFT JOIN nrgbinary_platform.country cn2 ON cn2.id=c.`registrationCountry`
LEFT JOIN nrgbinary_platform.customer_balance cn3 ON cn3.customerId=c.`id`
LEFT JOIN nrgbinary_platform.customer_total_statistics cn4 ON cn4.customerId=c.`id`
LEFT JOIN nrgbinary_platform.users cn5 ON cn5.id=c.`employeeInChargeId`
LEFT JOIN nrgbinary_platform.sub_campaigns cn6 ON cn6.id=c.`subCampaignId`
LEFT JOIN nrgbinary_platform.campaigns cn7 ON cn7.id=c.`campaignId`
;
END$$
DELIMITER ;
Вторая:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55.
DELIMITER $$
USE `sisense`$$
DROP PROCEDURE IF EXISTS `withdraw`$$
CREATE DEFINER=`root`@`%` PROCEDURE `withdraw`()
BEGIN
DROP TABLE IF EXISTS sisense.withdraw;
CREATE TABLE sisense.withdraw
SELECT c.`customerId`, c.`paymentMethod`, c.`amountUSD`, c.`requesttime`, c.`confirmTime`, c.`status`, cn1.`saleStatus`, c.`confirmationcode`,c.`cancelReason`,
cn3.username AS UserName,
IFNULL(cn2.name,'no desk') deskName,
cn4.name AS Country,
cn1.isdemo ,
cn7.name AS Campaign,
cn6.param AS Affiliate,
MONTH(c.`confirmTime`) AS ConfirmMonth,
YEAR(c.`confirmTime`) AS ConfirmYear,
cn1.firstDepositDate AS FTDdate,
MONTH(cn1.firstDepositDate) AS FTDMonth,
YEAR(cn1.firstDepositDate) AS FTDYear,
IF(cn1.lastlogindate='0000-00-00 00:00:00','NOLOGIN',(IF(DATEDIFF(DATE(NOW()),DATE(cn1.lastlogindate)) > 14, 'PASSIVE' , 'ACTIVE'))) AS ClientStatus,
(SELECT COUNT(*) FROM nrgbinary_platform.withdrawals WHERE customerId = c.`customerId` AND paymentMethod NOT LIKE 'Bonus' AND paymentMethod NOT LIKE 'Wallet1' AND paymentMethod NOT LIKE 'WebMoney' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod NOT LIKE 'InactivityFee' AND paymentMethod NOT LIKE 'Fees' AND STATUS LIKE 'approved') AS WDCount,
CASE
WHEN (IF(cn1.lastCallDate='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.lastCallDate)))) = -1 THEN 'NOCOMMENT'
WHEN (IF(cn1.lastCallDate='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.lastCallDate)))) >30 THEN '30+'
WHEN (IF(cn1.lastCallDate='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.lastCallDate)))) < 8 THEN '0-7'
WHEN (IF(cn1.lastCallDate='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.lastCallDate)))) < 15 THEN '8-14'
WHEN (IF(cn1.lastCallDate='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.lastCallDate)))) < 31 THEN '15-30'
END AS CommentCategory,
cn1.verification,
MONTH(c.`requesttime`) AS RequestMonth,
YEAR(c.`requesttime`) AS RequestYear,
IF(cn1.`lastTimeInvestment`='0000-00-00 00:00:00',-1,DATEDIFF(DATE(NOW()),DATE(cn1.`lastTimeInvestment`))) AS LastInvDate,
cc.cc_info,
bal.lastBalance,
clear.ClearedBy_info,
(SELECT COUNT(*) FROM nrgbinary_platform.withdrawals WHERE customerId = c.`customerId` AND paymentMethod NOT LIKE 'Bonus' AND paymentMethod NOT LIKE 'Wallet1' AND paymentMethod NOT LIKE 'WebMoney' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod NOT LIKE 'InactivityFee' AND paymentMethod NOT LIKE 'Fees' AND STATUS LIKE 'canceled') AS WDCountCanceled,
(SELECT COUNT(*) FROM nrgbinary_platform.withdrawals WHERE customerId = c.`customerId` AND paymentMethod NOT LIKE 'Bonus' AND paymentMethod NOT LIKE 'Wallet1' AND paymentMethod NOT LIKE 'WebMoney' AND paymentMethod NOT LIKE 'Payoneer' AND paymentMethod NOT LIKE 'InactivityFee' AND paymentMethod NOT LIKE 'Fees' AND STATUS LIKE 'pending') AS WDCountPending,
c.`clearedby`
FROM nrgbinary_platform.withdrawals c
LEFT JOIN nrgbinary_platform.customers cn1 ON cn1.id=c.`customerId`
LEFT JOIN nrgbinary_platform.desks cn2 ON cn2.id=(SELECT u.`deskId` FROM nrgbinary_platform.user_desks u WHERE u.`userId` LIKE cn1.employeeInChargeId AND u.`deskId` != 40 LIMIT 1)
LEFT JOIN nrgbinary_platform.users cn3 ON cn3.id=cn1.employeeInChargeId
LEFT JOIN nrgbinary_platform.country cn4 ON cn4.id=cn1.Country
LEFT JOIN nrgbinary_platform.sub_campaigns cn6 ON cn6.id=cn1.subCampaignId
LEFT JOIN nrgbinary_platform.campaigns cn7 ON cn7.id=cn1.campaignId
LEFT JOIN nrgbinary_platform.customer_balance bal ON bal.customerId=cn1.`id`
LEFT JOIN (SELECT customerId, GROUP_CONCAT(CONCAT('-CardNum:', cardNum, '-CardType:', ccType) SEPARATOR ', ') AS cc_info FROM nrgbinary_platform.creditcard_users WHERE STATUS='active' GROUP BY customerId) cc ON cc.customerId=c.customerId
LEFT JOIN (SELECT customerId, GROUP_CONCAT(CONCAT('-ClearedBy:', ClearedBy) SEPARATOR ', ') AS ClearedBy_info FROM nrgbinary_platform.customer_deposits WHERE STATUS='approved' AND paymentmethod != 'Bonus' AND paymentmethod != 'Wallet1' AND paymentmethod != 'Payoneer' GROUP BY customerId) clear ON clear.customerId=c.customerId
;
END$$
DELIMITER ;
По моему тут модно только некоторые подзросы в функции вынести для удобочитаемости.
А вы что скажите?
|