Materialized View Tuning
You can improve performance of materialized views.
Limit Number of Join Rows
Performance impact: Variable
For example, use only equality conditions that map one row from one table to one or at most a few rows from the other table.
Use Indexes on Join Columns
Performance impact: Variable
SELECT
statement that creates the join. Also consider creating
an index on the materialized view itself. This can improve the performance of keeping
the materialized view updated.
If an UPDATE
or DELETE
operation on a detail table is often based on a condition on a column, try to create an index on the materialized view on this column if possible.
For example, cust_order
is a materialized view of customer orders, based on two tables. The tables are customer
and book_order
. The former has two columns (cust_id
and cust_name
) and the latter has three columns (order_id
, book
, and cust_id
). If you often update the book_order
table to change a particular order by using the condition book_order.order_id=const
, then create an index on cust_order.order_id
. On the other hand, if you often update based on the condition book_order.cust_id=const
, then create an index on Cust_order.cust_id
.
If you often update using both conditions and cannot afford to create both indexes, you may want to add book_order.ROWID
in the view and create an index on it instead. In this case, TimesTen updates the view for each detail row update instead of updating all of the rows in the view directly and at the same time. The scan to find the row to be updated is an index scan instead of a row scan, and no join rows need to be generated.
If ViewUniqueMatchScan
is used in the execution plan, it is a sign that the execution may be slower or require more space than necessary. A ViewUniqueMatchScan
is used to handle an update or delete that cannot be translated to a direct update or delete of a materialized view, and there is no unique mapping between a join row and the associated row in the materialized view. This can be fixed by selecting a unique key for each detail table that is updated or deleted.
Avoid Unnecessary Updates
Performance impact: Variable
GROUP BY
column
because this involves deleting the old value and inserting the new value.
Try not to update an expression that references more than one table. This may disallow direct update of the view because TimesTen may perform another join operation to get the new value when one value in this expression is updated.
View maintenance based on an update or delete is more expensive when:
-
The view cannot be updated directly. For example, not all columns specified in the detail table
UPDATE
orDELETE
statement are selected in the view, or -
There is not an indication of a one-to-one mapping from the view rows to the join rows.
For example:
Command> CREATE MATERIALIZED VIEW v1 AS SELECT x1 FROM t1, t2 WHERE x1=x2; Command> DELETE FROM t1 WHERE y1=1;
The extra cost comes from the fact that extra processing is needed to ensure that one and only one view row is affected due to a join row.
The problem is resolved if either x1
is UNIQUE
or a unique key from t1
is included in the select list of the view. ROWID
can always be used as the unique key.
Avoid Changes to the Inner Table of an Outer Join
Performance impact: Variable
When possible, perform INSERT
operations on an inner table
before inserting into the associated join rows into an outer table. Likewise, when
possible perform DELETE
operations on the outer table before deleting
from the inner table. This avoids having to convert non-matching rows into matching rows
or vice versa.
Limit Number of Columns in a View Table
Performance impact: Variable
SelectList
can impact performance. As the number of
columns in the select list grows, the time to prepare operations on detail
tables increases. In addition, the time to run operations on the view detail
tables also increases. Do not select values or expressions that are not
needed.
The optimizer considers the use of temporary indexes when preparing operations on detail tables of views. This can significantly slow down prepare time, depending upon the operation and the view. If prepare time seems slow, consider using ttOptSetFlag
to turn off temporary range indexes and temporary hash scans.