如何在MySQL中提高性能或查询执行时间?

时间:2015-07-28 08:57:44

标签: mysql performance query-optimization

这是我的查询,在远程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

请通过推荐一些技巧或修改或我可以应用于此查询以提高性能的任何提示来帮助我。

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

构造

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

执行效果不佳,因为这些子查询的tmp表中没有索引。如果可能,将它们转换为表格的JOIN。如果没有,则为它们创建TEMATEARY TABLE并索引tmp表。

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

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

请勿使用LEFT,除非您希望并希望“右”表中缺少数据。

  

和type ='menubox'

哪个表格是type

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