【问题标题】:How to improve performance or Query Execution Time in MySQL?如何提高 MySQL 的性能或查询执行时间?
【发布时间】:2015-10-18 17:39:56
【问题描述】:

这是我的查询,在远程 MySQL 服务器上执行至少需要 1 分钟。

select 
    a.z_companyid_pk 'z_companyid_pk',
    a.company_name 'Client',
    a.display_name 'Display_Name',
    a.z_parentcompanyid_fk 'Parent_Company',
    a.z_resellerid_fk 'Reseller',
    (select 
            company_name
        from
            company_mst
        where
            z_companyid_pk = a.z_resellerid_fk) 'Reseller_name',
    (select 
            if(count(*) >= 1, 'YES', 'NO')
        from
            webprofile
        where
            z_boxid_fk in (select 
                    z_boxid_pk
                from
                    box_mst
                where
                    z_companyid_fk = a.z_companyid_pk)) 'V1_Website',
    (select 
            if(count(*) >= 1, 'YES', 'NO')
        from
            webapp_mst
        where
            z_companyid_fk = a.z_companyid_pk) 'V2_Website',
    (select 
            if(count(*) > 0, 'YES', 'NO')
        from
            widget_mst
        where
            widget_status = 1
                and type in ('templated-menu' , 'menus')
                and z_companyid_fk = a.z_companyid_pk) 'Widgets_with_my_menu',
    (select 
            if(count(imagehtml) > 0, 'YES', 'NO')
        from
            image_mst
        where
            length(imagehtml) > 0
                and z_companyid_fk = a.z_companyid_pk) 'HTML_menus',
    (select 
            max(login_datetime)
        from
            loginlog_mst a
                LEFT OUTER JOIN
            contact_mst b ON a.z_contactid_fk = b.z_contactid_pk
        where
            b.z_companyid_fk = a.z_companyid_pk) 'LCI',
    k.z_boxid_pk 'DMB_Box_ID',
    k.box_name,
    (select 
            box_lastcheck
        from
            box_online
        where
            z_boxid_fk = k.z_boxid_pk) 'LCI_last_Checked_in_Date',
    (select 
            websitelink
        from
            company_profile
        where
            z_companyid_fk = a.z_companyid_pk) 'Website',
    (select 
            company_email
        from
            company_profile
        where
            z_companyid_fk = a.z_companyid_pk) 'Primary_Contact_Email',
    concat(a.company_address_line1,
            a.company_address_line2) 'address',
    (select 
            city_name
        from
            city_mst
        WHERE
            z_cityid_pk = a.z_cityid_fk) 'city',
    (select 
            state_name
        from
            state_mst
        WHERE
            z_stateid_pk = a.z_stateid_fk) 'Province',
    (select 
            country_name
        FROM
            country_mst
        where
            z_countryid_pk = z_countryid_fk) 'country_name',
    a.company_postalcode 'zip_postal_code',
    (select 
            telephone
        from
            company_profile
        where
            z_companyid_fk = a.z_companyid_pk) 'company_telephone',
    a.z_timezoneid_fk,
    b.timezone 'time_Zone',
    (select 
            facebooklink
        from
            company_profile
        where
            z_companyid_fk = a.z_companyid_pk) 'facebook',
    (select 
            twitterlink
        from
            company_profile
        where
            z_companyid_fk = a.z_companyid_pk) 'twitter',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 1
                and am.z_companyid_fk = a.z_companyid_pk) 'Yelp',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 2
                and am.z_companyid_fk = a.z_companyid_pk) 'Urbanspoon',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 3
                and am.z_companyid_fk = a.z_companyid_pk) 'Trip_Advisor',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 4
                and am.z_companyid_fk = a.z_companyid_pk) 'Citysearch',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 5
                and am.z_companyid_fk = a.z_companyid_pk) 'Open_Table',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 6
                and am.z_companyid_fk = a.z_companyid_pk) 'ZAGAT',
    (SELECT 
            bizurl
        FROM
            user_settings us
                INNER JOIN
            asset_mst am ON (us.userid = am.userid)
        where
            us.isiteid = 32
                and am.z_companyid_fk = a.z_companyid_pk) 'Zomato',
    d.z_contactid_fk 'Email_contact_ID',
    d.username 'Email_User_Name',
    d.contact_firstname,
    d.contact_lastname,
    d.status,
    (select 
            max(login_datetime)
        from
            loginlog_mst
        where
            z_contactid_fk = d.z_contactid_fk) 'last_login_date',
    (select 
            GROUP_CONCAT(groupname)
        from
            contact_group_mst
        where
            z_companyid_fk = a.z_companyid_pk) 'group_name'
from
    company_mst a
        LEFT OUTER JOIN
    timezone_mst b ON a.z_timezoneid_fk = b.z_timezoneid_pk
        LEFT OUTER JOIN
    company_profile c ON a.z_companyid_pk = c.z_companyid_fk
        LEFT OUTER JOIN
    (select 
        v.z_contactid_fk,
            v.z_companyid_fk,
            w.username,
            w.contact_firstname,
            w.contact_lastname,
            if(contact_wtaccess = 1, 'Veryfied', 'Not Veryfied') 'status'
    from
        priviledge_mst v
    LEFT OUTER JOIN contact_mst w ON v.z_contactid_fk = w.z_contactid_pk
    where
        w.type <> 'Staff') d ON a.z_companyid_pk = d.z_companyid_fk
        LEFT OUTER JOIN
    (select 
        z_boxid_pk, box_name, asset_mst.z_companyid_fk
    from
        box_mst
    INNER JOIN asset_mst ON (box_mst.userid_fk = asset_mst.userid)
    where
        asset_mst.status = 0
            and type = 'menubox') k ON a.z_companyid_pk = k.z_companyid_fk
where
    a.status <> 0
        and a.z_companyid_pk in (101089 , 104001, 103863)
ORDER BY a.company_name

请通过建议一些技术或修改或任何提示来帮助我,我可以应用于此查询以提高性能。

【问题讨论】:

  • 表结构,哪些列的索引呢?查询的执行计划?

标签: mysql performance query-optimization


【解决方案1】:

结构

JOIN ( SELECT ... )
JOIN ( SELECT ... )

性能不佳,因为这些子查询的 tmp 表上没有索引。如果可能,将它们转换为表的 JOIN。如果没有,则为它们创建临时表并索引 tmp 表。

a 需要 INDEX(z_companyid_pk),除非该列已经是 PRIMARY KEY

输出中有多少行?我想了解聚合需要做多少工作。

不要使用LEFT,除非您期望并希望“正确”表中缺少数据。

and type = 'menubox'

type 在哪个表中?

请为 priviledge_mst、contact_mst、company_mst、asset_mst、box_mst 提供SHOW CREATE TABLE。我们需要检查索引。

【讨论】:

    【解决方案2】:

    即使没有实际数据,也很难确定实际问题。但您可以使用以下 2 种方法进行检查-

    1. 您在左连接中使用子查询的地方,在子查询中传递 z_companyid_pk 以减少子查询中的数据集。

    2. 删除子查询,简单地使用左连接。

    假设您已经检查并且所有必需的索引都在那里。

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2013-03-21
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多