UNION ALL查看运行速度非常慢

时间:2017-12-12 18:26:36

标签: mysql sql tableau database-performance union-all

我正在为Tableau中的vizualiaztion准备数据。目前,我们有大约十几个表,有一些共享值,我通过一个巨大的UNION ALL脚本组合成一个大视图(附件只显示2个表)

我在Tableau中的表现非常慢,而且我确信有一种更有效的方法来构建数据。目前,我必须每15分钟更新一次画面提取,因此数据不是真实的"直播"

任何帮助将不胜感激! 感谢

DROP VIEW XXXXX_alerts;
CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = root@% 
    SQL SECURITY DEFINER
VIEW XXXXX_alerts AS
    SELECT 
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.ID AS ID,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.inspector AS Inspector,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.CREATED_DATE AS Created_Date,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.CREATED_LOCATION AS Created_Location,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.MODIFIED_DATE AS Modified_Date,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.MODIFIED_LOCATION AS Modified_Location,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.date1 AS date1,
        NULL AS test_date_8200B,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.division AS division,
        '8900' AS Form,
        NULL AS Device_Type,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.coating_condition AS coating_condition,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.next_calibration_date AS next_calibration_date,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.pipe_barcode AS pipe_barcode,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.nitrogen_pressure_when_left_psig AS nitrogen_pressure_when_left_psig,
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form.is_nitrogen_left_value_correct AS is_nitrogen_left_value_correct,
        NULL AS Location,
        NULL AS Location_Conditional_Value,
        NULL AS Town,
        NULL AS readily_detectable_reading,
        NULL AS threshold_level_gas,
        NULL AS hefpa_shutoff,
        NULL AS customers_signature,
        NULL AS street,
        NULL AS serial_number,
        NULL AS tubing_filter_replaced_date,
        NULL AS calibration_date,
        NULL AS holiday_equipment_type,
        NULL AS date_tag_issued,
        NULL AS class,
        NULL AS condition1,
        NULL AS action_taken,
        NULL AS reinspect_complete,
        NULL AS person_designation,
        NULL AS apartment_complex,
        NULL AS result_of_contact_1,
        NULL AS result_of_contact_2,
        NULL AS date_letter_sent,
        NULL AS date_clerk_sent_letter_to_customer_advocate,
        NULL AS excessive_co_reinspect,
        NULL AS tag_cleared,
        NULL AS condition,
        NULL AS date_of_contact_to_customer_service_for_locked_off_meter,
        NULL AS date_tag_cleared,
        NULL AS type_of_fitting,
        NULL AS re_inspected_by_company_employee,
        NULL AS inspection_Result,
        NULL AS wo_sap_number,
        NULL AS material_studied,
        NULL AS time_studied,
        NULL AS program,
        NULL AS cgi_or_fi,
        NULL AS component_picture,
        NULL AS Test_Pressure,
        NULL AS Test_Duration,
        NULL AS Pipe_Footage,
        NULL AS Pipe_Size,
        NULL AS Barcode,
        NULL AS Material
    FROM
        _data435329_XXXXX_12_pre_tested_pipe_inspection_form 
    UNION ALL SELECT 
        _data435329_XXXXX_10_2_quantitative_odorant.ID AS ID,
        _data435329_XXXXX_10_2_quantitative_odorant.inspector AS Inspector,
        _data435329_XXXXX_10_2_quantitative_odorant.CREATED_DATE AS Created_Date,
        _data435329_XXXXX_10_2_quantitative_odorant.CREATED_LOCATION AS Created_Location,
        _data435329_XXXXX_10_2_quantitative_odorant.MODIFIED_DATE AS Modified_Date,
        _data435329_XXXXX_10_2_quantitative_odorant.MODIFIED_LOCATION AS Modified_Location,
        NULL AS date1,
        _data435329_XXXXX_10_2_quantitative_odorant.date1 AS test_date_8200B,
        _data435329_XXXXX_10_2_quantitative_odorant.division AS division,
        '8200B' AS Form,
        NULL AS Device_Type,
        NULL AS coating_condition,
        NULL AS next_calibration_date,
        NULL AS pipe_barcode,
        NULL AS nitrogen_pressure_when_left_psig,
        NULL AS is_nitrogen_left_value_correct,
        _data435329_XXXXX_10_2_quantitative_odorant.location1 AS Location,
        zcoptions.CONDITION_VALUE AS Location_Conditional_Value,
        _data435329_XXXXX_10_2_quantitative_odorant.town AS Town,
        _data435329_XXXXX_10_2_quantitative_odorant.odorometer_read_quantitative_readily_detectable___ AS readily_detectable_reading,
        _data435329_XXXXX_10_2_quantitative_odorant.odorometer_read_quantitative_threshold_level___gas AS threshold_level_gas,
        NULL AS hefpa_shutoff,
        NULL AS customers_signature,
        NULL AS street,
        NULL AS serial_number,
        NULL AS tubing_filter_replaced_date,
        NULL AS calibration_date,
        NULL AS holiday_equipment_type,
        NULL AS date_tag_issued,
        NULL AS class,
        NULL AS condition1,
        NULL AS action_taken,
        NULL AS reinspect_complete,
        NULL AS person_designation,
        NULL AS apartment_complex,
        NULL AS result_of_contact_1,
        NULL AS result_of_contact_2,
        NULL AS date_letter_sent,
        NULL AS date_clerk_sent_letter_to_customer_advocate,
        NULL AS excessive_co_reinspect,
        NULL AS tag_cleared,
        NULL AS condition,
        NULL AS date_of_contact_to_customer_service_for_locked_off_meter,
        NULL AS date_tag_cleared,
        NULL AS type_of_fitting,
        NULL AS re_inspected_by_company_employee,
        NULL AS inspection_Result,
        NULL AS wo_sap_number,
        NULL AS material_studied,
        NULL AS time_studied,
        NULL AS program,
        NULL AS cgi_or_fi,
        NULL AS component_picture,
        NULL AS Test_Pressure,
        NULL AS Test_Duration,
        NULL AS Pipe_Footage,
        NULL AS Pipe_Size,
        NULL AS Barcode,
        NULL AS Material
    FROM
        (_data435329_XXXXX_10_2_quantitative_odorant
        LEFT JOIN zcoptions ON ((zcoptions.KEY_VALUE = _data435329_XXXXX_10_2_quantitative_odorant.location1)))
    WHERE
        (zcoptions.OPTION_LIST_ID = 4553158)
    GROUP BY _data435329_XXXXX_10_2_quantitative_odorant.ID 
    UNION ALL SELECT 

1 个答案:

答案 0 :(得分:2)

根据您提供的内容,我们只能解决

    FROM  (_data435329_XXXXX_10_2_quantitative_odorant
    LEFT JOIN  zcoptions
          ON ((zcoptions.KEY_VALUE = 
              _data435329_XXXXX_10_2_quantitative_odorant.location1))
          )
    WHERE  (zcoptions.OPTION_LIST_ID = 4553158)
    GROUP BY  _data435329_XXXXX_10_2_quantitative_odorant.ID

WHERE (WHERE (zcoptions.OPTION_LIST_ID = 4553158) = 4553158)有效地将LEFT JOIN变为JOIN。所以,我想知道你为什么不说JOIN

指数:

zcoptions:  INDEX(OPTION_LIST_ID)

或者,为了使它"覆盖",因此有点快:

zcoptions:  INDEX(OPTION_LIST_ID, KEY_VALUE, CONDITION_VALUE)

这闻起来像EAV架构。如果是,请按照建议here设置更好的PRIMARY KEY等,同样适用于任何其他键值表。

下一件气味就是这种模式:

FROM a JOIN b ...
GROUP BY a.id

我称之为"爆炸 - 内爆"因为JOIN首先将行数相乘,然后GROUP BY将其返回到其中一个表中的每行一行。

如果JOINb中的a实际上没有GROUP BY中的多行,那么SELECT ..., ( SELECT CONDITION_VALUE FROM zcoptions WHERE KEY_VALUE = _data435329_XXXXX_10_2_quantitative_odorant.location1 AND OPTION_LIST_ID = 4553158 ) AS Location_Conditional_Value, ... 就不必要了。 (在不知道数据的情况下,我无法预测这一点。)在这种情况下,另一种表达查询的方式是

JOIN

并删除GROUP BYGROUP BY

如果zcoptions中有多行,则 ( SELECT min(CONDITION_VALUE) ... 形式不正确。查询可以自由混合非分组列。一个可能的解决方法是选择最小的:

    ( SELECT CONDITION_VALUE ... LIMIT 1 ) as ...,

或选择'首先'

skipIfEmpty