开发更好的代码
差异
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两者之间不会有性能差异。
就我个人而言,我更倾向于基于 CASE 的解决方案。 CASE 是 ANSI 标准而不是 Oracle 特定的函数。来自其他数据库平台的开发人员可能必须查找 DECODE 语句的语义。无论开发人员熟悉哪种语言,CASE 对于开发人员来说也是相对明显的。几乎每种语言都有 CASE 语句,因此任何开发人员都应该能够快速了解您在做什么。
话虽如此,我倾向于提倡这样的方法:
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
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.