Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将MySQL查询执行时间从90秒减少到5秒甚至1秒以下_Mysql_Performance_Join - Fatal编程技术网

如何将MySQL查询执行时间从90秒减少到5秒甚至1秒以下

如何将MySQL查询执行时间从90秒减少到5秒甚至1秒以下,mysql,performance,join,Mysql,Performance,Join,我正在使用MySQL数据库。当我执行一个主表(20000行)与其他5个表连接的查询时,需要90秒才能返回结果。然后,我将索引添加到每个标准中(where子句)。然后执行时间从90秒下降到45秒,然后我尝试只选择必要的字段,而不是全部(*),这需要25秒。我应该怎么做才能让它降到5秒甚至1秒以下 SELECT ap.propertyid, ap.type_market, ap.market_sale_price, ap.sale_from, ap.sale_until, ap.property_

我正在使用MySQL数据库。当我执行一个主表(20000行)与其他5个表连接的查询时,需要90秒才能返回结果。然后,我将索引添加到每个标准中(where子句)。然后执行时间从90秒下降到45秒,然后我尝试只选择必要的字段,而不是全部(*),这需要25秒。我应该怎么做才能让它降到5秒甚至1秒以下

SELECT
ap.propertyid, ap.type_market, 
ap.market_sale_price, ap.sale_from, ap.sale_until, ap.property_salepsf, 
ap.rent_from, ap.property_rent_until, ap.property_rentpsf, ap.title, 
ap.street, ap.suburbs, ap.state, ap.bedrooms, ap.bathrooms, ap.carport, 
ap.buildup_area, ap.builtup_area_from, ap.builtup_area_to, 
ap.land_area, ap.land_area_from, ap.land_area_to, 
ap.status AS prop_status, ap.datecreate AS uploadedOn, type_property.name, (
 SELECT name 
 FROM gallery 
 WHERE
 propertyid = ap.propertyid
 ORDER BY frontpage
 LIMIT 0, 1
) AS picture, 
uom_mst.uom_shortcode,
agent_profile.person, agent_profile.mobile, agent_profile.electronicmail, agent_profile.agent_pix, agent_profile.agent_pix_crop,
country.country_currency_html AS currency,
owner_profile.oname, owner_profile.omobile, owner_profile.oemail
FROM agentproperty ap
LEFT JOIN agent_profile ON (ap.agentid = agent_profile.agentid)
LEFT JOIN type_property ON (ap.type_property = type_property.rid)
LEFT JOIN uom_mst ON (ap.property_bua_uom = uom_mst.uom_id)
LEFT JOIN country ON (ap.property_ctry = country.country_id)
LEFT JOIN owner_profile ON (ap.propertyid = owner_profile.propertyid)
WHERE
ap.status = 'active' AND 
agent_profile.status = 'active' AND 
ap.property_type = '1' AND 
getCompanyID(ap.propertyid) = '001' AND 
ap.agentid = '100010001'
GROUP BY ap.propertyid
ORDER BY ap.datecreate DESC
LIMIT 200, 10

假设agentid和propertyid(代理属性、代理配置文件)具有高选择性,而状态、属性类型、国家id等的选择性较低[这意味着存在许多不同的代理和属性id,但很少有不同的状态、属性类型等]

影响最大的指数是:

  • agent\u属性(agentid)
    agent\u属性(agentid,属性类型,状态)
    -在where子句中使用
  • agent\u profile(agentid)
    -用于联接
  • owner\u profile(propertyid)

但是,您还需要考虑<代码> GETCOFYYID < /代码>的影响。


您尚未提供SQL,但它可能至少需要一个
属性(属性id)
索引或属性表中的任何内容。

假设agentid和propertyid(代理属性、代理配置文件)具有高选择性,而状态、属性类型、国家/地区id等的选择性较低)[这意味着有许多不同的代理和属性ID,但很少有不同的状态、属性类型等]

影响最大的指数是:

  • agent\u属性(agentid)
    agent\u属性(agentid,属性类型,状态)
    -在where子句中使用
  • agent\u profile(agentid)
    -用于联接
  • owner\u profile(propertyid)

但是,您还需要考虑<代码> GETCOFYYID < /代码>的影响。


您还没有给出SQL,但它可能至少需要一个
属性(property\u id)
索引或属性表中的任何内容。

在查询上运行解释计划,看看MySQL是怎么说的

确保联接中涉及的所有列都有与其关联的索引


连接中的六个表对于数据库来说是一项艰巨的工作。我建议查看重新排序连接是否有任何效果。如果将连接从限制性最大的表排序到限制性最小的表,则可能会减少所需的工作。

对查询运行EXPLAIN PLAN,并查看MySQL对此的说明

确保联接中涉及的所有列都有与其关联的索引


连接中的六个表对于数据库来说是一项艰巨的工作。我建议查看重新排序连接是否有任何效果。如果将连接从限制性最大的表排序到最小的表排序,则可能会减少所需的工作。

缓存我的加速。这不是一个很好的答案,但这是一个很大的问题,它会加快我的速度。这不是一个很好的答案,但是这是一个很大的疑问