فهرست منبع

头条-sql优化

yangzian 3 سال پیش
والد
کامیت
f011f6a675

+ 68 - 23
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/advertise/mapper/xml/ByteDanceVideoInfoMapper.xml

@@ -292,34 +292,79 @@
 
     <!-- 通投智选 - 横图大图 -->
     <select id="getModeImageLarge" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
-        select distinct image.*,
-        (select count(1) from ctop_bytedance_creative c
-        where SUBSTRING_INDEX(SUBSTRING_INDEX(image_ids,'["',-1),'"]',1) in(image.id)
-        and c.account_id = '${accountId}') as creativeCnt,
-        'image' as materialType
-        from ctop_bytedance_image_info image
-        where account_id = #{accountId}
-        and image.type = '1'
-        and image.image_create_time &gt;= #{startTime}
-        and image.image_create_time &lt;= #{endTime}
-        order by  creativeCnt ASC
+        SELECT
+        image.*,
+        IFNULL(t.creativeCnt,0) as creativeCnt
+        FROM
+        ctop_bytedance_image_info image
+        LEFT JOIN
+        (
+            SELECT c2.*,
+            COUNT(c2.imageId) as creativeCnt
+            from (
+                SELECT c1.*,
+                substring_index(substring_index(c1.quchu,',',s.num),',',-1) as imageId
+                from (
+                    SELECT
+                        c.account_id,
+                        c.image_ids,
+                        REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(c.image_ids, '["', -1), '"]', 1),'"','') quchu
+                    FROM ctop_bytedance_creative  c
+                    WHERE c.account_id = '${accountId}'
+                    ) c1
+                         cross join ctop_dim_group_by as s
+                        on s.num &lt;= (length(c1.image_ids)-length(replace(c1.image_ids,',',''))+1)
+                ) c2
+            GROUP BY c2.imageId
+        )t on image.account_id = t.account_id and t.imageId = image.id
+        WHERE
+            image.account_id = #{accountId}
+        AND image.type = '1'
+        AND image.image_create_time &gt;= #{startTime}
+        AND image.image_create_time &lt;= #{endTime}
+        GROUP BY
+            image.id
+        ORDER BY
+            creativeCnt,image.id ASC
         limit #{videoCnt}
     </select>
 
     <!-- 通投智选 - 竖图大图 -->
     <select id="getModeImageVertical" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
-        select distinct image.*,
-        (select count(1) from ctop_bytedance_creative c
-        where SUBSTRING_INDEX(SUBSTRING_INDEX(image_ids,'["',-1),'"]',1) in(image.id)
-        and c.account_id = '${accountId}') as creativeCnt,
-        'image' as materialType
-        from ctop_bytedance_image_info image
-        where account_id = #{accountId}
-        and image.type = '2'
-        and image.image_create_time &gt;= #{startTime}
-        and image.image_create_time &lt;= #{endTime}
-        order by  creativeCnt ASC
-        limit #{videoCnt}
+        SELECT
+            image.*,
+            IFNULL(t.creativeCnt,0) as creativeCnt
+        FROM
+            ctop_bytedance_image_info image
+        LEFT JOIN
+        (
+            SELECT c2.*,
+            COUNT(c2.imageId) as creativeCnt
+            from (
+                  SELECT c1.*,
+                  substring_index(substring_index(c1.quchu,',',s.num),',',-1) as imageId
+                    from (
+                          SELECT c.account_id,
+                                 c.image_ids,
+                                REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(c.image_ids, '["', -1), '"]', 1),'"','') quchu
+                          FROM ctop_bytedance_creative  c
+                          WHERE c.account_id = '${accountId}'
+                          ) c1
+                           cross join ctop_dim_group_by as s
+                           on s.num &lt;= (length(c1.image_ids)-length(replace(c1.image_ids,',',''))+1)
+                    ) c2
+                        GROUP BY c2.imageId
+        )t on image.account_id = t.account_id and t.imageId = image.id
+        WHERE
+            image.account_id = #{accountId}
+        AND image.type = '2'
+        AND image.image_create_time &gt;= #{startTime}
+        AND image.image_create_time &lt;= #{endTime}
+        GROUP BY
+            image.id
+        ORDER BY
+            creativeCnt,image.id ASC
+            limit #{videoCnt}
     </select>