I have two tables table1 and table2. Table1 is generated by a parser and automatically updated every few days. Table2 is the user edited version of table1. If a record exist in table2, that should overlay the record in table1 in the view.
Any edited version in table2 has the original content from table1 for Column2 and Column3 in OldColumn2 and OldColumn3 respectively. When a user deletes a record, the deleted column has value 1 and if the user wants to add the record again it has value 0 to display in the view. By default the value is NULL if it’s never been deleted.
If an new record in inserted into table2, OldColumn2 and OldColumn3 has the value new string to distinguish that the record doesn’t exist in table1.
Here is the design of my table.
Table1
+------+------+------+-------------+
| Col1 | Col2 | Col3 | OtherColumns|
+------+------+------+-------------+
| a1 | b1 | c1 | Data |
+------+------+------+-------------+
| a2 | b2 | c2 | Data |
+------+------+------+-------------+
| a3 | b3 | c3 | Data |
+------+------+------+-------------+
| a4 | b4 | c4 | Data |
+------+------+------+-------------+
Table2
+------+------+------+-------------+----------+----------+---------+
| Col1 | Col2 | Col3 | OtherColumns| OldCol2 | OldCol3 | Deleted |
+------+------+------+-------------+----------+----------+---------+
| a1 | e1 | f1 | Data | b1 | c1 | NULL |
+------+------+------+-------------+----------+----------+---------+
| a2 | k2 | m2 | Data | b2 | c2 | 0 |
+------+------+------+-------------+----------+----------+---------+
| a3 | k3 | m3 | Data | b3 | c3 | 1 |
+------+------+------+-------------+----------+----------+---------+
| z1 | kk | jj | Data | new | new | 1 |
+------+------+------+-------------+----------+----------+---------+
| z2 | kj | uu | Data | new | new | 0 |
+------+------+------+-------------+----------+----------+---------+
View
+------+------+------+-------------+----------+----------+---------+
| Col1 | Col2 | Col3 | OtherColumns| OldCol2 | OldCol3 | Deleted |
+------+------+------+-------------+----------+----------+---------+
| a1 | e1 | f1 | Data | b1 | c1 | NULL |
+------+------+------+-------------+----------+----------+---------+
| a2 | k2 | m2 | Data | b2 | c2 | 0 |//Deleted then added
+------+------+------+-------------+----------+----------+---------+
| a4 | k4 | j4 | Data | NULL | NULL | 0 |
+------+------+------+-------------+----------+----------+---------+
| z2 | kj | uu | Data | new | new | 0 |
+------+------+------+-------------+----------+----------+---------+
Here is my view statement.
CREATE VIEW NEWVIEW
AS
SELECT t2.* FROM table1 t1
LEFT JOIN table2 t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.oldColumn2 AND t1.column3 = t2.oldColumn3
WHERE t2.column1 IS NOT NULL AND t2.Deleted = 0
UNION
SELECT t1.*, null, null, null FROM table1 t1
LEFT JOIN table2 t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.oldColumn2 AND t1.column3 = t2.oldColumn3
WHERE t2.column1 IS NULL
UNION
SELECT * FROM table2 WHERE oldColumn2 = 'new' AND oldColumn3 = 'new' AND Deleted = 0
The view is a bit slow right now. How can I optimize this view?
Try to change first two SELECT statements in your view with this one –
Note, than views may have bad performance.