【发布时间】: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告诉任何结果,因为我们对数据关系一无所知。例如,allbilling行总是与来自 'cover? If all tables always match all other tables, it _might_ help to reorder theinner joins` 的行匹配,但优化可能会自动完成。由于缺乏信息,我们无法确定。我们甚至不知道存在哪些索引。 -
@Ian Bjorhovde:当前版本的 DB 9.5 不支持解释计划,因此无法提供。
标签: sql optimization db2 sql-tuning query-tuning