如何为SQL表达式创建数据库INDEX?

时间:2012-10-09 09:37:35

标签: sql indexing db2 database-indexes

我是索引的初学者。我想为这个SQL表达式创建索引,这需要花费太多时间来执行,所以我想在什么样的列上创建索引?  我正在使用DB2 db,但没关系,我认为这个问题非常普遍。

我的SQL表达式是:

select * from incident  where (relatedtoglobal=1) 
and globalticketid in (select ticketid from INCIDENT where status='RESOLVED')
     and statusdate <='2012-10-09 12:12:12'

我应该用这5列创建索引还是如何?

由于

1 个答案:

答案 0 :(得分:2)

您的查询:

select * 
from incident  
where relatedtoglobal = 1 
  and globalticketid in ( select ticketid 
                          from INCIDENT 
                          where status='RESOLVED'
                        )
  and statusdate <='2012-10-09 12:12:12' ;

里面的子查询:

select ticketid 
from INCIDENT 
where status='RESOLVED'
  • (status, ticketid)上的索引肯定会有助于子查询评估的效率,从而有助于提高查询效率。

  • 对于查询,除了上一个索引之外,还需要一个索引。 (relatedtoglobal, globalticketid)可能就足够了。

我不确定DB2引擎是否会/可以使用更复杂的索引。

  • (relatedtoglobal, globalticketid) INCLUDE (statusdate)

  • 上的人一样
  • 两个索引,一个在(relatedtoglobal, globalticketid)上,另一个在(relatedtoglobal, statusdate)


DB2文档不是一个简单的阅读,但有许多细节。从 CREATE INDEX 语句和 Implementing Indexes 开始。