开发更好的代码

发布于 2024-10-14 13:33:20 字数 1249 浏览 2 评论 0原文

差异

  select CE1.CLASS_ID, 
         CE1.LOCATION_ID,
         count(case CE1.FORMAT_ID  when 5 then 1 end) as LIVE,
         count(case CE1.FORMAT_ID  when 14 then 1 end) as LB,
         count(case CE1.FORMAT_ID  when 15 then 1 end) as WEB,
         SUM(DECODE (CE1.ROLE_ID,5,1,0)) FACULTY,
         SUM(DECODE  (CE1.ROLE_ID ,2,1,0)) MODERATOR,
         SUM(DECODE  (CE1.ROLE_ID ,3,1,0)) PANELIST,  
         SUM(DECODE  (CE1.ROLE_ID,4,1,0)) PRESENTER,
         COUNT(CE1.USER_ID) TOT 
    from C_EDUCATION1 CE1 
GROUP BY CE1.LOCATION_ID, CE1.CLASS_ID

我们正在开发现有系统,我们发现混合使用 COUNT 和 DECODE。 如果我们理解正确的话,我们可以转换为以下代码。

  select CE1.CLASS_ID,  
         CE1.LOCATION_ID, 
         SUM(DECODE (CE1.FORMAT_ID ,5,1,0)) LIVE,
         SUM(DECODE  (CE1.FORMAT_ID ,14,1,0)) LB, 
         SUM(DECODE  (CE1.FORMAT_ID,15,1,0)) WEB,
         SUM(DECODE (CE1.ROLE_ID,5,1,0)) FACULTY,
         SUM(DECODE  (CE1.ROLE_ID ,2,1,0)) MODERATOR,
         SUM(DECODE  (CE1.ROLE_ID ,3,1,0)) PANELIST,  
         SUM(DECODE  (CE1.ROLE_ID,4,1,0)) PRESENTER,
         COUNT(CE1.USER_ID) TOT 
    from C_EDUCATION1 CE1 
GROUP BY CE1.LOCATION_ID, CE1.CLASS_ID

或者 所有 COUNT

我们想知道性能差异或其他问题。 有更好的方式存在吗?

DIFFERENCES

  select CE1.CLASS_ID, 
         CE1.LOCATION_ID,
         count(case CE1.FORMAT_ID  when 5 then 1 end) as LIVE,
         count(case CE1.FORMAT_ID  when 14 then 1 end) as LB,
         count(case CE1.FORMAT_ID  when 15 then 1 end) as WEB,
         SUM(DECODE (CE1.ROLE_ID,5,1,0)) FACULTY,
         SUM(DECODE  (CE1.ROLE_ID ,2,1,0)) MODERATOR,
         SUM(DECODE  (CE1.ROLE_ID ,3,1,0)) PANELIST,  
         SUM(DECODE  (CE1.ROLE_ID,4,1,0)) PRESENTER,
         COUNT(CE1.USER_ID) TOT 
    from C_EDUCATION1 CE1 
GROUP BY CE1.LOCATION_ID, CE1.CLASS_ID

We are developing existing system and we found mixed use COUNT and DECODE.
If we understand correctly, we could convert to following codes.

  select CE1.CLASS_ID,  
         CE1.LOCATION_ID, 
         SUM(DECODE (CE1.FORMAT_ID ,5,1,0)) LIVE,
         SUM(DECODE  (CE1.FORMAT_ID ,14,1,0)) LB, 
         SUM(DECODE  (CE1.FORMAT_ID,15,1,0)) WEB,
         SUM(DECODE (CE1.ROLE_ID,5,1,0)) FACULTY,
         SUM(DECODE  (CE1.ROLE_ID ,2,1,0)) MODERATOR,
         SUM(DECODE  (CE1.ROLE_ID ,3,1,0)) PANELIST,  
         SUM(DECODE  (CE1.ROLE_ID,4,1,0)) PRESENTER,
         COUNT(CE1.USER_ID) TOT 
    from C_EDUCATION1 CE1 
GROUP BY CE1.LOCATION_ID, CE1.CLASS_ID

OR
All COUNT

We are wondering differences in performance, or others issues.
Is it better way exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2024-10-21 13:33:20

两者之间不会有性能差异。

就我个人而言,我更倾向于基于 CASE 的解决方案。 CASE 是 ANSI 标准而不是 Oracle 特定的函数。来自其他数据库平台的开发人员可能必须查找 DECODE 语句的语义。无论开发人员熟悉哪种语言,CASE 对于开发人员来说也是相对明显的。几乎每种语言都有 CASE 语句,因此任何开发人员都应该能够快速了解​​您在做什么。

话虽如此,我倾向于提倡这样的方法:

SELECT ce1.class_id,
       ce1.location_id,
       SUM( CASE WHEN format_pkg.is_live( format_id ) = 'Y' THEN 1 ELSE 0 END ) live,
       SUM( CASE WHEN format_pkg.is_lb(   format_id ) = 'Y' THEN 1 ELSE 0 END ) lb,
       ...

This 使用第一个查询中的 CASE,第二个查询中的 SUM,并添加一些函数调用来确定正在讨论的格式/角色类型。这可以防止您必须在各处对 FORMAT_ID/ROLE_ID 值进行硬编码。使用 SUM 而不是 COUNT 更清晰一些,因为对于开发人员来说,COUNT 排除 NULL 值可能不会立即显而易见。

There will be no performance difference between the two.

Personally, I would tend to prefer a CASE-based solution. CASE is ANSI standard rather than an Oracle-specific function. Developers that are coming from other database platforms will probably have to look up the semantics of the DECODE statement. CASE is also relatively obvious to developers regardless of the language they're familiar with. Virtually every language has a CASE statement so any developer should be able to quickly see what you're doing.

That being said, I would tend to advocate something like

SELECT ce1.class_id,
       ce1.location_id,
       SUM( CASE WHEN format_pkg.is_live( format_id ) = 'Y' THEN 1 ELSE 0 END ) live,
       SUM( CASE WHEN format_pkg.is_lb(   format_id ) = 'Y' THEN 1 ELSE 0 END ) lb,
       ...

This uses the CASE from the first query, the SUM from the second, and adds some function calls to determine what type of format/ role is being discussed. This prevents you from having to hard-code the FORMAT_ID/ ROLE_ID values all over the place. And using SUM rather than COUNT is a bit clearer since it's probably not immediately obvious to a developer that COUNT excludes the NULL values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文