如何优化此VIEW声明?

时间:2011-07-28 13:32:18

标签: mysql sql view query-optimization

我有两个表table1和table2。 Table1由解析器生成,每隔几天自动更新一次。 Table2是table1的用户编辑版本。如果table2中存在记录,则该记录应覆盖视图中table1中的记录。

table2中的任何已编辑版本都分别​​具有来自table1的原始内容,分别用于OldColumn2和OldColumn3中的Column2和Column3。当用户删除记录时,已删除的列的值为1,如果用户想要再次添加记录,则其值为0以显示在视图中。默认情况下,如果它从未被删除,则值为NULL

如果插入到table2中的新记录,OldColumn2和OldColumn3具有值new字符串,以区分该记录在table1中不存在。

这是我桌子的设计。

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     |
+------+------+------+-------------+----------+----------+---------+

这是我的观点陈述。

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

现在视图有点慢。如何优化此视图?

1 个答案:

答案 0 :(得分:0)

尝试使用此方法更改视图中的前两个SELECT语句 -

SELECT
  t1.column1,
  t1.column2,
  t1.column3,
  IF(t2.column1 IS NULL, t1.OtherColumns, t2.OtherColumns) OtherColumns,
  IF(t2.column1 IS NULL, NULL, t2.OldCol2) OldCol2,
  IF(t2.column1 IS NULL, NULL, t2.OldCol3) OldCol3,
  IF(t2.column1 IS NULL, NULL, t2.Deleted) Deleted
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 OR t2.column1 IS NOT NULL AND t2.Deleted = 0

注意,视图可能会有不良表现。