1. 程式人生 > 其它 >SQL優化一例:通過改變分組條件(減少計算次數)來提高效率

SQL優化一例:通過改變分組條件(減少計算次數)來提高效率

#與各人授權日期相關,所以有十萬使用者,就有十萬次查詢(相關子查詢)


@Run.ExecuteSql("更新各人應聽正課數",@"
update bi_data.study_manual_{{課程id}} c join
(
select
a.user_id,
(
select count(*) from bi_data.dict_course_all d
where d.course_id={{課程id}}
and belong_date<='{{作業日期}}'
and d.belong_date>a.grant_course_date
) should_zhengke_count
from bi_data.study_manual_{{課程id}} a
) d on d.user_id=d.user_id
set
c.should_zhengke_count=d.should_zhengke_count
")


######
#與各人授權日期相關,因為課程只執行幾十天,所以只有幾十次查詢。


@Run.ExecuteSql("更新各人應聽正課數",@"
update bi_data.study_manual_{{課程id}} c join
(
select
a.grant_course_date,
(
select count(*) from bi_data.dict_course_all d
where d.course_id={{課程id}}
and belong_date<='{{作業日期}}'
and d.belong_date>a.grant_course_date


) should_zhengke_count
from (select distinct grant_course_date from bi_data.study_manual_{{課程id}}) a
) d on d.grant_course_date=d.grant_course_date
set
c.should_zhengke_count=d.should_zhengke_count
")

本文來自部落格園,作者:xiaoyongdata(微訊號:xiaoyongdata),轉載請註明原文連結:https://www.cnblogs.com/xiaoyongdata/p/15725276.html