【问题标题】:Choosing Indices Databases选择指数数据库
【发布时间】:2017-04-03 08:16:33
【问题描述】:

我在为这个问题选择最佳索引时遇到了一些问题。给定一组查询:

-- 查询1

select CUSTOMERS.PID
from CUSTOMERS join
     PARTICIP
     on CUSTOMERS.PID = PARTICIP.PID
group by CUSTOMERS.PID
having COUNT(CUSTOMERS.PID) = 1;

-- 查询2

select CUSTOMERS.PID
from CUSTOMERS join
     PARTICIP
     on CUSTOMERS.PID = PARTICIP.PID
group by CUSTOMERS.PID
having COUNT(CUSTOMERS.PID) >= 2;

-- 查询 3

select hasa.pid,strno as "Street #", street, city, prov as Province
from ((addresses join hasa on hasa.addrid = addresses.addrid) join 
      customers
      on customers.pid = hasa.pid
     )
order by city;

-- 查询 4

select * 
from guides 
where pid in (select pid 
              from particip 
              where tid in (select unique tid 
                            from itineraries 
                            where date >= '2015-01-01' and date <= '2015-12-31'));

-- 查询 5

select age, gender 
from persons 
where persons.pid in (select hase.pid 
                      from hase 
                      where hase.pid in (select * from employees));

-- 查询 6

select provid 
from isp 
where provid NOT in (select provid as service 
                     from isp 
                     group by provid, svctype 
                     having svctype = 'ACCOM');

-- 查询 7

select provid 
from isp 
where provid in (select provid 
                 from isp 
                 where ((svctype != 'ACCOM' and svctype = 'MEAL') 
                        or (svctype = 'ACCOM' and svctype != 'MEAL')) 
                 group by provid 
                 having count(unique svctype) = 1);

-- 查询 8

select unique provid, svctype 
from isp 
where penalty = 100 or penalty = 200;

-- 查询 9

select pid, sum(amount) as "totalSalary", count(pid) as "numTours" 
from hasco 
group by pid 
order by sum(amount) desc;

-- 查询 10

select distinct hasco.TID 
from hasco 
join (select * 
      from tours 
      where status = 'I' OR status = 'F') as table1 on hasco.tid = table1.tid  
where amount >= 5000;
terminate;

和ER图: ER Diagram

我的任务是选择两个属性来索引,这将提供最大的好处。没有提及任何数量的查询或对表的修改。谢谢。

编辑:我只需要一两个属性来索引,并在其背后进行一些推理来帮助理解。

另外,非集群比集群更好吗?谢谢。

【问题讨论】:

  • 我开始了,但您应该尝试格式化问题以便阅读。
  • 现在正在尝试这样做,对不起。
  • 非常感谢您帮助格式化它!

标签: sql database indexing db2


【解决方案1】:

仅考虑到上述查询和 ER 图,不可能选择“最好的”或任何具有“最大”好处的东西。您(和优化器)还需要了解数据(“统计信息”)。涉及多少行,值如何分布,以及更多问题。

如果您只有小表,优化器可能会选择表扫描,根本不需要索引。一般来说,试着弄清楚谓词列和连接列上的索引是否有帮助。

您是否已经使用过 db2 解释工具或 design/index advisor (db2advis) 对此进行过调查?

【讨论】:

  • 不,我没有,如果没有“最佳”解决方案,任何解决方案都可以。我正在考虑索引 isp.svctype,因为它在几个查询的 where 子句中使用。我一直在决定是让索引聚集还是非聚集。我假设 db2 通过集群方式自动索引主键。这是真的吗?
  • 非常有用的工具,我会试着看看它说什么,谢谢!
猜你喜欢
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多