0

I'm afraid this functionality may not exist [yet]. I wanted to use an index that spans multiple tables in DB2. I know Oracle and SQL server implement them (with more or less options) and that PostreSQL doesn't seem to implement them yet.

Note: I asked a similar question about PosgreSQL a few weeks ago.

A multi-table index could be very beneficial for some specific queries.

For reference, here are the multi-table index examples for Oracle and SQL Server:

Oracle Example

Oracle can create bitmap join indexes, as shown below:

create table dealer (
  id int primary key not null,
  city varchar2(20) not null
);

create table car (
  id int primary key not null,
  brand varchar2(20),
  price int,
  dealer_id int references dealer (id)
);

create bitmap index bix1 on car (d.city, c.brand)
from car c, dealer d
where d.id = c.dealer_id;

select avg(c.price)
from dealer d
join car c on c.dealer_id = d.id
where d.city = 'Chicago' and c.brand = 'Buick';

SQL Server Example

SQL Server can create indexed views:

create table dealer (
  id int primary key not null,
  city varchar(20) not null
);

create table car (
  id int primary key not null,
  brand varchar(20),
  price int,
  dealer_id int references dealer (id)
);

create view v with schemabinding as
select d.city, c.brand, c.price, c.dealer_id
from dbo.dealer d
join dbo.car c on c.dealer_id = d.id;

create unique clustered index uix1 on v (city, brand, price);

select avg(c.price)
from dealer d
join car c on c.dealer_id = d.id
where d.city = 'Chicago' and c.brand = 'Buick';

Is there anything similar in DB2?

The Impaler
  • 45,731
  • 9
  • 39
  • 76

2 Answers2

3

Db2 (for Linux, UNIX, and Windows) supports indexes on tables, i.e., you can only index a single table.

The table can be a MQT (materialized query table) which could be based on a view. This is different from indexing multiple tables directly.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • What about MQT ? – Hogan Aug 25 '20 at 15:16
  • 2
    added it to my answer – data_henrik Aug 25 '20 at 15:18
  • @data_henrik I like this option. It seems that using `REFRESH IMMEDIATE` may do all the hard word behind the scenes for me (of course at the cost of engine resources). Thanks. For the record, this article (https://www.ibm.com/developerworks/data/library/techarticle/dm-0708khatri/) explains the differences between Oracle and DB2. – The Impaler Aug 25 '20 at 15:35
0

Yes like in SQL Server you can create indexes on views that span multiple tables.

Note -- you must set the view up in a special way (called materialized query table) for it to work.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • MQT is a way to work round it - so I would anser the question as No but there is a workaround - that builds a single table first. So @data_henrik is absolutely right in his statement – MichaelTiefenbacher Aug 25 '20 at 15:19
  • @MichaelTiefenbacher -- ok, if the OP is claiming that SQL Server does this then it is the same (under the hood) -- note the key words `with schemabinding` in his example. This is the SQL Server syntax for MQT – Hogan Aug 25 '20 at 15:21
  • @Hogan From your answer it transpires that only Oracle has true multi-table indexes. DB2, PostgreSQL, and SQL Server go with the strategy of indexing materialized views; ...and their data refresh strategies are also different, in terms of opportunity and cost. – The Impaler Aug 25 '20 at 15:40
  • @TheImpaler -- your statement is valid. However, I believe that DB2 gives significantly more options for refresh "strategies" than the others. You can set materialized views to refresh on any change for example. – Hogan Aug 25 '20 at 17:25