【问题标题】:Need to optimize slow query in DB2 using group by on timestamp需要在 DB2 中使用基于时间戳的 group by 优化慢查询
【发布时间】:2014-06-16 19:26:05
【问题描述】:

要求:按年和省创建的计费记录数。

数据库: DB2 9.5

以下是用于获取详细信息的查询,它需要大量时间,超过 1 小时并且超时。

每张表的记录数如下。 计费:900万 封面:100万 客户:300万 地址:400万。

select year(bill.created), addr.province,count(1) as yearprovicecount  from billing bill 
inner join cover cov  on ( bill.coveragecid = cov.coveragecid) 
inner join customer cust on (cust.customercid= cov.customercid)
inner join address addr on (cust.customercid=addr.customercid)
group by year(bill.created), addr.province;

Created 是一个时间戳列。

由于查询中没有where子句,查询变慢了。请让我知道是否有任何方法可以优化查询。

【问题讨论】:

  • 仅仅通过查看查询,您就不必要地加入到客户表中,因为地址也有 customercid。尝试删除此联接,看看是否有任何明显的性能改进。
  • 没有其他信息——DDL、解释计划等。你不会得到太多有用的帮助——只能猜测潜在的解决方案。
  • 如果 billing 中也包含 customercid,那么您也可以删除连接以覆盖。我希望我们可以假设您对所有涉及的 id 字段都有索引。
  • 缺少太多内容。我们需要更多信息来猜测,尽管在那里有customer 表似乎没有意义。我们无法从inner joins 告诉任何结果,因为我们对数据关系一无所知。例如,all billing 行总是与来自 'cover? If all tables always match all other tables, it _might_ help to reorder the inner joins` 的行匹配,但优化可能会自动完成。由于缺乏信息,我们无法确定。我们甚至不知道存在哪些索引。
  • @Ian Bjorhovde:当前版本的 DB 9.5 不支持解释计划,因此无法提供。

标签: sql optimization db2 sql-tuning query-tuning


【解决方案1】:

谢谢大家,

我现在可以提取报告了,感谢您的建议。

1.我删除了不需要的连接

2。我添加了 where 子句来限制扫描的行数。

select year(bill.created), addr.province,count(1) as yearprovicecount  from billing bill 
inner join cover cov  on ( bill.coveragecid = cov.coveragecid) 
-- inner join customer cust on (cust.customercid= cov.customercid)
inner join address addr on (cust.customercid=addr.customercid)
where year(billhis.created) = 2014
group by year(bill.created), addr.province;

注意:DB 9.5当前版本不支持解释计划,因此无法提供。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 2011-05-29
    • 2011-08-29
    • 2019-09-12
    • 2012-12-31
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多