Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Tuning. overview Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept;

Similar presentations


Presentation on theme: "Query Tuning. overview Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept;"— Presentation transcript:

1 Query Tuning

2 overview

3 Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept; SELECT location FROM techlocation WHERE ssnum = 43253265; Optimizers expand views when identifying the query blocks to be optimized. The selection from techlocation is expanded into a join: SELECT location FROM employee, tech WHERE employee.dept = tech.dept AND ssnum = 43253265;

4 Avoid Views with unnecessary Joins Join with Techdept unnecessarily CREATE VIEW Techlocation AS SELECT ssnum, Techdept.dept, location FROM Employee, Techdept WHERE Employee.dept = Techdept.dept SELECT dept FROM Techlocation WHERE ssnum = 4444 SELECT dept FROM Employee WHERE ssnum = 4444

5 Some Idiosyncrasies OR may stop the index being used –break the query and use UNION Order of tables may affect join implementation

6 Performance Impact of the Query Rewritings Running Example 100000 Employees 100000 Students 10 tech

7 Aggregate Maintenance Materialize an aggregate if needed “frequently” The accounting department of a convenience store chain issues queries every twenty minutes to obtain: –The total dollar amount on order from a particular vendor –The total dollar amount on order by a particular store outlet. Original Schema: Ordernum(ordernum, itemnum, quantity, purchaser, vendor) Item(itemnum, price) Ordernum and Item have a clustering index on itemnum The total dollar queries are expensive. Can you see why?

8 Aggregate Maintenance Add: –VendorOutstanding(vendor, amount), where amount is the dollar value of goods on order to the vendor, with a clustering index on vendor –StoreOutstanding(purchase r, amount), where amount is the dollar value of goods on order by the purchaser store, with a clustering index on purchaser. Each update to order causes an update to these two redundant tables (triggers can be used to implement this explicitely, materialized views make these updates implicit) Trade-off between update overhead and loopup speed-up.

9 Materialized Views in Oracle9i Oracle9i supports materialized views: CREATE MATERIALIZED VIEW VendorOutstanding BUILD IMMEDIATE REFRESH COMPLETE ENABLE QUERY REWRITE AS SELECT orders.vendor, sum(orders.quantity*item.price) FROM orders,item WHERE orders.itemnum = item.itemnum group by orders.vendor; Some Options: –BUILD immediate/deferred –REFRESH complete/fast –ENABLE QUERY REWRITE Key characteristics: –Transparent aggregate maintenance –Transparent expansion performed by the optimizer based on cost. It is the optimizer and not the programmer that performs query rewriting

10 Aggregate Maintenance Use trigger to update create trigger updateVendorOutstanding on orders for insert as update vendorOutstanding set amount = (select vendorOutstanding.amount+sum(inserted.quantity*item.price) from inserted,item where inserted.itemnum = item.itemnum ) where vendor = (select vendor from inserted) ;

11 Aggregate Maintenance SQLServer on Windows2000 accounting department schema and queries 1000000 orders, 1000 items Using triggers for view maintenance On this experiment, the trade-off is largely in favor of aggregate maintenance

12 summary


Download ppt "Query Tuning. overview Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept;"

Similar presentations


Ads by Google