将大型INSERT查询拆分为较小的部分

时间:2019-04-18 10:45:29

标签: mysql

我有一个非常大的INSERT查询,它将多个表中的数据组合到一个新表中。

此查询在具有十万行的表上运行,因此执行时会花费很长时间。

有没有一种方法可以将其拆分成较小的部分,因此它首先获取用于联接HypeAuditor - h.username的表的前200个用户名,然后进行插入,再获取下一批200个用户名,等等吗?

代码如下:

INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when hy.followers IS NULL AND nj.followers>0 THEN nj.followers
                when hy.followers>0 AND nj.followers IS NULL THEN hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when hy.engagement IS NULL AND nj.engagement IS NULL AND h.engagement>0 THEN h.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement IS NULL THEN nj.engagement/100
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement IS NULL THEN hy.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement>0 THEN (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement>0 AND h.engagement IS NULL THEN (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement>0 THEN (h.engagement/100+hy.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb

0 个答案:

没有答案