简体   繁体   中英

MySql view is very slow. Why?

My normal query:

SELECT
    DISTINCT vt.id as id,
    vtt.name as n,
    vt.etxid as etx
FROM vt
LEFT JOIN vtt ON
    (vtt.locale = "etx"
    AND vtt.etxid = vt.etxid)

Execution time: 5ms

My view:

CREATE OR REPLACE VIEW myview AS
SELECT
    DISTINCT vt.id as id,
    vtt.name as n,
    vt.etxid as etx
FROM vt
LEFT JOIN vtt ON
    (vtt.locale = "etx"
    AND vtt.etxid = vt.etxid)

My view query:

SELECT * from myview;

Execution time: 600ms

As soon as you mention DISTINCT or aggregation functions in a view MySQL selects TEMPTABLE algorithm for this view, and it means it will create a temporary table for the view and then apply sorting, grouping, and aggregations to it. See more details here . Also, there are some recommendations here concerning view performance .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM