选择指数数据库

时间:2016-11-19 15:38:50

标签: sql database indexing db2

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

- 查询1

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory(container));

// Or...

DependencyResolver.SetResolver(new MyDependencyResolver(container));

- 查询2

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

- 查询3

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

- 查询4

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;

- 查询5

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'));

- 查询6

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

- 查询7

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

- 查询8

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);

- 查询9

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

- 查询10

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

和ER图: ER Diagram

我的任务是选择两个可以提供最大收益的索引属性。没有提及对表的任何数量的查询或修改。感谢。

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

此外,非群集群集是否通常更好?感谢。

1 个答案:

答案 0 :(得分:0)

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

如果您只有小表,优化器可能会选择一个表扫描,根本不需要索引。通常,尝试确定谓词列和连接列上的索引是否有帮助。

您是否已经使用过db2 explain工具或design/index advisor (db2advis)来研究这个?