mysql sum count 性能優化 ----測試數據22萬條


 原語句:

select

sum(sale_price) as account_money,

count(distinct point_card_no) as use_count,

sum(add_point_value) as point_sum

from tb_rm_coupon_point

where

delete_flg='0' and add_point_datetime >='2019/05/13' and add_point_datetime < '2019/05/20'

 執行時間:時間為24.9

 

結果:

注:tb_rm_coupon_point 此表有295288條數據

 

此語句掃描了24032條數據 得到了結果 and 用了索引掃描

 

改進查詢方法:

 語句:

SELECT

  SUM( CASE WHEN add_point_datetime >='2019/05/13' and add_point_datetime < '2019/05/20' THEN

  sale_price ELSE 0 END) as 'account_money',

   SUM( CASE WHEN add_point_datetime >='2019/05/13' and add_point_datetime < '2019/05/20' THEN

  add_point_value ELSE 0 END) as 'point_sum'

count( CASE WHEN add_point_datetime >='2019/05/13' and add_point_datetime < '2019/05/20'

THEN  true ELSE null END) as 'use_count'

FROM  tb_rm_coupon_point where delete_flg='0'

執行時間為:10秒左右   相比快了15

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM