3

此查询单独运行:

SELECT 
    -- lots of columns 
FROM 
    table1 t1
    LEFT JOIN table2 t2
        ON t2.[userid] = t1.[userid]
    LEFT JOIN table3 t3
        ON t1.[orderid] = t3.[orderid]
    LEFT JOIN table4 t4
        ON t4.[orderitemlicenseid] = t3.[orderitemlicenseid]
    LEFT JOIN table5 t5
        ON t1.[orderid] = t5.[orderid]
    LEFT JOIN table6 t6
        ON t5.[transactionid] = t6.[transactionid]
    LEFT JOIN table7 t7
        ON t7.[transactionid] = t5.[transactionid]
    LEFT JOIN table8 t8
        ON t8.[voucherid] = t7.[voucherid]
    LEFT JOIN table9 t9
        ON t8.[voucherid] = t9.[voucherid]
    LEFT JOIN table10 t10
        ON t10.[vouchergroupid] = t9.[vouchergroupid]
        AND t10.[territoryid] = t2.[territoryid]
    LEFT JOIN table11 t11
        ON t11.[voucherid] = t8.[voucherid]
    LEFT JOIN table12 t12
        ON t12.[orderid] = t1.[orderid]
GROUP BY 
    t5.[transactionid]

大约需要 2.5 秒才能完成。当我将其保存到视图并运行为:

选择 * 从视图名称;

完成需要 7 秒。

是什么原因以及如何使视图更快?

4

1 回答 1

6

MySql 中处理视图的方式存在性能问题。在这里查看这个答案。

基本上有 2 种不同的算法用于 MySql 中的视图:Merge 和 Temptable。Merge 通常表现很好,但 Temptable 可能表现很差。当视图包含某些结构(包括 )时,不能使用合并GROUP BY,因此您的视图将使用 Temptable 算法。

于 2011-11-23T17:45:00.487 回答