我的数据库有多个表,其中有一些匹配的列,但名称和顺序不同。
例如
Table1: FullName Grade Foo
Table2: Bar Rank WholeName
什么会更快:
1) 1个视图,对每个表重复相同的查询
CREATE View Test AS
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(FullName, ' ', 1), ' ', -1) AS "First Name",
If (Grade<50, "Bad", "Good") AS "Type"
FROM table1
UNION
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(WholeName, ' ', 1), ' ', -1) AS "First Name",
If (Rank<50, "Bad", "Good") AS "Type"
FROM table2
或
2) 两个视图,一个用于纯合并,另一个用于操纵
CREATE View Test1 AS
SELECT FullName AS "First Name", Grade AS "Type"
FROM table1
UNION
SELECT WholeName AS "First Name", Rank AS "Type"
FROM table2
CREATE View Test2 AS
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(`First Name`, ' ', 1), ' ', -1) AS "First Name",
If (Type<50, "Bad", "Good") AS "Type"
FROM Test1
显然1)更复杂,因为你必须重复每一个操作。如果你有10张table,那就意味着重复同样的操作10次。如果你有10个操作,那就意味着写100个操作而不是10个。但如果快一点,我就买。
http://www.sqlfiddle.com/#!9/48d531/1表示大致相同的时间,尽管在某些运行中它支持2)。
请记住,这里涉及到大量的数据,而实际的sql非常复杂,并且涉及左连接。
1条答案
按热度按时间7qhs6swi1#
一
VIEW
是语法上的糖分--试试等价的SELECT
.“sugar”论点认为q2在解决了
VIEWs
: