with t as( select t1.video_id,COUNT(case when DATEDIFF(SECOND,t1.start_time,t1.end_time)>=t2.duration then 1 end) complete_qty, count(1) total_qty,COUNT(IIF(if_like=1,1,null)) like_qty,COUNT(IIF(if_retweet=1,1,null)) zf_qty, COUNT(comment_id) pl_qty,max(CAST(end_time as date)) v_last_playdate from tb_user_video_log t1 join tb_video_info t2 on t1.video_id=t2.video_id where DATEDIFF(DAY,release_time,(select max(end_time) from tb_user_video_log))<=29 group by t1.video_id ) select t.video_id,CAST((100*complete_qty*1.0/total_qty+5*like_qty+3*pl_qty+2*zf_qty)*1.0 /((DATEDIFF(DAY,v_last_playdate,(select max(end_time) from tb_user_video_log)))+1) as int) from t 优化以上sql
时间: 2024-04-16 16:28:41 浏览: 120
VB_data_time.rar_vb data time
以下是对以上 SQL 进行优化的改进:
```sql
WITH t AS (
SELECT t1.video_id,
COUNT(CASE WHEN DATEDIFF(SECOND, t1.start_time, t1.end_time) >= t2.duration THEN 1 END) complete_qty,
COUNT(*) total_qty,
COUNT(CASE WHEN if_like = 1 THEN 1 END) like_qty,
COUNT(CASE WHEN if_retweet = 1 THEN 1 END) zf_qty,
COUNT(comment_id) pl_qty,
MAX(CAST(end_time AS DATE)) v_last_playdate
FROM tb_user_video_log t1
JOIN tb_video_info t2 ON t1.video_id = t2.video_id
WHERE DATEDIFF(DAY, release_time, (SELECT MAX(end_time) FROM tb_user_video_log)) <= 29
GROUP BY t1.video_id
)
SELECT t.video_id,
CAST((100 * complete_qty * 1.0 / total_qty + 5 * like_qty + 3 * pl_qty + 2 * zf_qty) * 1.0
/ (DATEDIFF(DAY, v_last_playdate, max_end_time) + 1) AS INT)
FROM t, (SELECT MAX(end_time) as max_end_time FROM tb_user_video_log) AS max_end
```
此优化后的 SQL 语句主要做了以下改进:
1. 使用 WITH 子句创建了一个临时视图,避免了嵌套子查询。
2. 将子查询 (SELECT MAX(end_time) FROM tb_user_video_log) 移至外部,并使用别名 max_end。
3. 在主查询中使用了 max_end.max_end_time,避免了重复计算。
4. 将 FROM t, (SELECT MAX(end_time) as max_end_time FROM tb_user_video_log) AS max_end 用于将两个查询结果合并。
这些优化措施可以提高查询的性能和可读性。请注意,具体的优化效果还需要考虑数据库的索引和数据量等因素。
阅读全文