SQL Performance Tuning
To this point, we've talked only about physical representations, as if all tables are base tables. Does it matter if a table is a view? To find out, we tested three well-known propositions .
Portability MySQL doesn't support views. The gains shown in this section are for only seven DBMSs.
Proposition one
A query of a view takes longer to prepare because it requires an extra lookup in the system catalog.
It turns out this proposition is true. To test it, we created a view on a simple table: CREATE VIEW View1 AS SELECT * FROM Table1 Then we timed the relative speed of Query #1 as opposed to Query #2: Query #1: SELECT * FROM View1 Query #2: SELECT * FROM Table1 GAIN: 7/7 Proposition two
It is faster to execute a prepared query on a materialized view twice, than to materialize the view twice. (A materialized view is a view whose rows take up space.) It turns out this proposition is true too. To test it, we created a materializable view on a simple table: CREATE VIEW View1 AS SELECT SUM(column1) AS sum_column1 FROM Table1 Then we timed the relative speed of Query #1 as opposed to Query #2: Query #1: SELECT SUM(column1) FROM Table1 Query #2: SELECT * FROM View1 GAIN: 2/7 Note: We ran each query twice so that the prepared plan might still be in cache. Proposition three
It is madness to use a view of a join or a union, unless you intend to use all columns . It turns out this proposition is also true, provided that a primary-key/foreign-key relationship exists between the tables. To test it, we created these two tables, as well as a view on a join of Table1 and Table2 : CREATE TABLE Table1 ( column1 INT PRIMARY KEY, column2 INT) CREATE TABLE Table2 ( column1 INT REFERENCES Table1, column2 INT) CREATE VIEW View1 AS SELECT Table1.column1 AS column1, Table2.column2 AS column2 FROM Table1, Table2 WHERE Table2.column1 = Table1.column1 Then we timed the relative speed of Query #1 as opposed to Query #2: Query #1: SELECT DISTINCT column1 FROM View1 Query #2: SELECT DISTINCT column1 FROM Table2 GAIN: 7/7 Not a single DBMS detected what we were doing and threw away the join clause. Each one performed the useless join and so it's trueYou should avoid views of joins if you only need columns from one side of the join. The Bottom Line: Views
A query of a view takes longer to prepare than a query of a table. It is faster to execute a prepared query on a materialized view twice, than to materialize the view twice. Don't query a view of a join or a union unless you intend to use all columns. |