可以使用存储过程或表值函数来创建有关视图的文本索引,但是,不允许您包括任何标量函数,例如,CONTAINS。
另一个主要缺点是不能对视图创建触发器,因此,不能识别底层基本表中的任何更改。
因此,对于增量索引更新,用户必须清楚已经添加、更新或删除了哪些文档以便使文本索引与数据库同步。为此,必须将所有更改添加至日志表。以下样本中说明了此过程:
db2 "create table sample (key INTEGER not null PRIMARY KEY, name VARCHAR(50) not null, comment VARCHAR(90))"
db2 "insert into sample values(1,'Claus','works in room 301')" db2 "insert into sample values(2,'Manja','is in the same office as Juergen')" db2 "insert into sample values(2,'Juergen','has the longest way to Raiko')" db2 "insert into sample values(3,'Raiko','is sitting in the office besides Claus ')"
db2 "create view sampleview as select key, comment from sample"
db2text "create index indexview for text on hde.sampleview(comment) cache table (comment) maximum cache size 1 key columns for index on view (key)" db2text "update index indexview for text" db2text "activate cache for index indexview for text"
需要指定高速缓存表以便能够对视图创建文本索引。要创建正确的日志表,必须对视图的索引指定键列。如果按此方式创建索引,则还可以将索引与表值函数配合使用。
当在分布式 DB2 环境中使用存储过程搜索时,必须在单个节点上为管理表显式指定表空间,并在此节点上显式调用。为了确保连接至正确的节点,使用 DB2NODE 环境变量。
db2 "insert into sample values(4,'Bernhard','is working in the same floor as Manja, but not as Claus')" db2 "insert into sample values(5,'Guenter','shares the office with Raiko')"
db2 "select INDSCHEMA,INDNAME,LOGVIEWSCHEMA,LOGVIEWNAME from db2ext.textindexes"以下是日志表的布局:
sqltype sqllen sqlname.data sqlname.length -------------------- ------ ----------------------- -------------- 496 INTEGER 4 OPERATION 9 392 TIMESTAMP 26 TIME 4 497 INTEGER 4 PK01 4要将条目添加到日志表中,使用下列命令:
db2 "insert into sample values(0,CURRENT TIMESTAMP,4)" db2 "insert into sample values(0,CURRENT TIMESTAMP,5)"第一个值用来描述操作(0 = 插入,1 = 更新,2 = 删除)。第二个值应当始终是 CURRENT TIMESTAMP,而最后一个值是已经插入的键。
db2text "update index indexview for text"
现在,可以使用存储过程对新值进行搜索了。