当前位置:   article > 正文

探索在数据开发中利用chatgpt提高代码质量---第一篇_order订单表,字段为:goods_id, amount ; pv 浏览表,字段为:goods_i

order订单表,字段为:goods_id, amount ; pv 浏览表,字段为:goods_id,uid; goods按

题目一:

order订单表,字段为:goods_id, amount ;

pv 浏览表,字段为:goods_id,uid;

goods按照总销售金额排序,分成top10,top10~top20,其他三组

求每组商品的浏览用户数(同组内同一用户只能算一次)

(1)数据准备:

 

  1. -- 创建订单表 (order_table)
  2. CREATE TABLE bdms.order_table ( goods_id INT, amount DECIMAL(10, 2) );
  3. -- 插入示例数据
  4. INSERT INTO bdms.order_table (goods_id, amount) VALUES (1, 10.5), (2, 20.0), (1, 15.3), (3, 8.2), (2, 18.7);
  5. -- 创建浏览表(pv_table)
  6. CREATE TABLE bdms.pv_table (
  7. goods_id INT,
  8. uid INT
  9. );
  10. -- 插入示例数据
  11. INSERT INTO bdms.pv_table (goods_id, uid)VALUES
  12. (1, 1001),
  13. (2, 1002),
  14. (1, 1003),
  15. (3, 1001),
  16. (2, 1004),
  17. (3, 1005),
  18. (1, 1002);

(2)自己写法: 

  1. with t1 as(
  2. select goods_id
  3. ,case when nn<= 10 then 'top10'
  4. when 10<nn<= 20 then 'top10~top20'
  5. else 'other'
  6. end as goods_group
  7. from
  8. (
  9. select goods_id
  10. ,row_number() over(partition by goods_id order by sale_sum desc) as nn
  11. from
  12. (
  13. select goods_id,sum(amount) as sale_sum -- 每种商品总金额
  14. from bdms.order_table
  15. group by goods_id
  16. ) aa
  17. ) bb
  18. )
  19. select b.goods_group,count(distinct a.uid) as num
  20. from bdms.pv_table a
  21. left join t1 b
  22. on a.goods_id = b.goods_id
  23. group by b.goods_id;

 (3)Chatgpt生成:

  1. WITH top_goods AS (
  2. SELECT goods_id, total_sales,rn
  3. FROM (
  4. SELECT goods_id, SUM(amount) AS total_sales,
  5. ROW_NUMBER() OVER (ORDER BY SUM(amount) DESC) AS rn
  6. FROM bdms.order_table
  7. GROUP BY goods_id
  8. ) t
  9. WHERE rn <= 20
  10. ),
  11. grouped_goods as(
  12. SELECT tg.goods_id,
  13. CASE WHEN tg.rn <= 10 THEN 'top10'
  14. WHEN 10<tg.rn <= 20 THEN 'top10~top20'
  15. ELSE 'other'
  16. END AS sales_group
  17. FROM top_goods tg
  18. )
  19. SELECT g.sales_group, COUNT(DISTINCT pv.uid) AS unique_users
  20. FROM grouped_goods g
  21. LEFT JOIN (
  22. SELECT DISTINCT goods_id, uid
  23. FROM bdms.pv_table pv
  24. ) pv
  25. ON g.goods_id = pv.goods_id
  26. GROUP BY g.sales_group;

 (4)分析结果:

a.对比运行时长:

自己写的运行时长:88.2s

Chatgpt运行时长:107.6s

b.结果阐述:

缺点:根据运行时长对比,Chatgpt生成的代码运行时间相对较长,而且根据本题来说要求同组内同一用户只能算一次,利用Chatgpt优化生成的代码时,并不能每次都生成满足符合题目要求的sql,还需要自己再修改一下

优点:在top_goods查询中,通过限定条件rn <= 20,使临时表的结果数据量大大减少,当数据量很大的时候,为后面查询该表时提高了查询效率。在外部查询中,将浏览表(pv_table)的子查询改为获取 DISTINCT 的商品ID和用户ID,以减少左连接时的数据量。虽然运行时长相对较长,但是对于代码的质量和严谨上是过关的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/99128
推荐阅读
相关标签
  

闽ICP备14008679号