2015-07-28 39 views
-3

這是我的查詢,它至少需要1分鐘在遠程MySQL服務器上執行。如何提高MySQL中的性能或查詢執行時間?

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 

請幫助我建議一些技術或修改或任何提示,我可以應用此查詢以提高性能。

+0

表結構,列上的索引呢?查詢的執行計劃? – Olli

回答

0

即使沒有實際數據,也很難確定實際問題。但是你可以通過下面2 approach-

  1. 檢查要使用左連接的子查詢,傳遞z_companyid_pk子查詢,以減少數據在子查詢中設置。

  2. 刪除子查詢並簡單地使用左連接。

假設您已檢查並且所有必需的索引都將存在。

0

將構建

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

PERFORMA很差,因爲有從那些子查詢tmp的表沒有索引。如果可能的話,將它們變成JOIN。如果不是,則爲它們創建TEMPORARY TABLE併爲tmp表建立索引。

a需要INDEX(z_companyid_pk)除非該列已經是PRIMARY KEY

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

請勿使用LEFT,除非您希望並希望缺少「正確」表中的數據。

和類型= 'menubox'

哪個表是type在?

Pleas爲priviledge_mst,contact_mst,company_mst,asset_mst,box_mst提供SHOW CREATE TABLE。我們需要檢查索引。