繁体   English   中英

PHP MySQL优化

[英]php mysql optimisation

我想优化我的php脚本,实际上我正在为php实现memcached(将时间从30秒减少到5秒)。 首先认为您必须看到app.promls.net上的脚本,构建该脚本大约需要3秒钟(最后查看源代码,随着时间的执行,您将看到注释框)。

接下来认为我使用解释优化了我的选择语句:

SELECT SQL_CALC_FOUND_ROWS p.id , p.type, p.bathrooms, p.bedrooms, p.for_sale, p.for_rent, p.rent_price, p.sale_price, p.min_price, p.mid_price, p.hig_price, p.units, p.negotiation, p.status, p.square_meters, p.commission, p.address, p.currency_type, p.creation_date, p.modified_date, p.parent, p.property_name, p.area_id, p.amenities, p.unit_number, p.levels, p.for_vacational, p.construction_year, p.construction_density, p.plot_meters, p.community_fees, p.garbage_tax, p.mortage, p.accompanied_visit, p.sale_sign, (select up.path from uploads up where up.property_id = p.id order by position asc,id asc limit 0,1) as image, p.ref_catastral, p.vacational_term, p.property_keys, p.owner_id, p.property_type , pt.name_es as category, ci.description as city, ci.id as city_id, es.description as estate, es.parent as estate_id, co.description as country, co.parent as country_id, u.id as brokerid, u.fullname as brokername, u.phone brokerphone, u.cellphone brokermobile , u.username as brokeremail, c.address as companyaddress, c.phone as companyphone, c.name as companyname, c.website companyweb, c.email companyemail, c.id as companyid FROM properties p inner join property_types pt on pt.id = p.property_type inner join areas ci on ci.id = p.area_id inner join areas es on es.id = ci.parent inner join areas co on co.id = es.parent inner join users u on u.id = p.created_by inner join company c on c.id = p.company_id where p.status in('active','active-rented','active-sold') order by p.min_price asc, p.mid_price asc, p.hig_price asc, p.rent_price asc, p.sale_price asc limit 0, 10

解释:

> 1, 'PRIMARY', 'p', 'ALL',
> 'property_area,property_status', '',
> '', '', 142, 'Using where; Using
> temporary; Using filesort' 1,
> 'PRIMARY', 'c', 'ref', 'PRIMARY',
> 'PRIMARY', '4',
> 'inmobili.p.company_id', 1, 'Using
> where' 1, 'PRIMARY', 'pt', 'eq_ref',
> 'PRIMARY', 'PRIMARY', '4',
> 'inmobili.p.property_type', 1, 'Using
> where' 1, 'PRIMARY', 'u', 'ALL',
> 'PRIMARY', '', '', '', 4, 'Using
> where' 1, 'PRIMARY', 'ci', 'eq_ref',
> 'PRIMARY', 'PRIMARY', '4',
> 'inmobili.p.area_id', 1, '' 1,
> 'PRIMARY', 'es', 'eq_ref', 'PRIMARY',
> 'PRIMARY', '4', 'inmobili.ci.parent',
> 1, '' 1, 'PRIMARY', 'co', 'eq_ref',
> 'PRIMARY', 'PRIMARY', '4',
> 'inmobili.es.parent', 1, '' 2,
> 'DEPENDENT SUBQUERY', 'up', 'ref',
> 'property_uploads',
> 'property_uploads', '4',
> 'inmobili.p.id', 5, 'Using where;
> Using filesort'

如您所见,它从表属性和一个临时表中返回了142行,那是因为where具有下一个条件:

where p.status in('active','active-sold','active-rented');

因此,要修复我在p.status上实现了索引。 但是误配是当我使用只有一个值的where时:

where p.status in('active');

它只给我返回77行,另外还包括:“在哪里使用;使用临时文件;使用文件排序”; 如果只更改where条件,但是如果我取消了order条件,它将产生相同的77行,并另外加上:“ using where;”,所以我想知道如何使用explain和何时包含“ order”来优化mysql。 ”或“分组依据”语句。

顺便说一句,我听说使用存储过程比从php中选择普通存储过程要快,但是寻找wordpress的来源不要使用过程,所以我认为使用过程不是解决方案。

我曾经想过如何将hiphop用于php,也许可以帮上忙。

我正在使用专用服务器。 php的配置必须很好,因为我有另一个子域,其加载速度比该应用程序快,脚本是相同的,但数据库是firebird,并且我正在实现memcached和存储过程。

您如何看待,我需要重新使用Firebird还是mysql可以解决这个问题?

非常感谢您的帮助,如果您看到我的应用需要30秒钟才能执行我的脚本,那么我真的需要对其进行优化;

告诉我,如果您需要更多信息,我将为您提供一切! 如果愿意,可以通过msn或skype聊天。

提前致谢

告诉我是否需要更多信息

首先,我认为您应该向我们提供您的架构(域)。 我认为您应该使用sql2diagram之类的工具为我们提供图像。 也许您应该先优化您的架构

实际上我正在为PHP实现memcached(将时间从30秒减少到5秒)

首先,我想知道从memcached(MEMORY)获取数据要花这么长时间。 从memcached获取数据完全不需要花费任何时间。 我也想知道您是否需要使缓存无效很多,或者这是一次查询。 因为在这种情况下,您需要执行另一个MySQL才能将新数据放入缓存中。 如果可能的话,我认为您应该事先使用消息队列(例如redis (我也将redis用作我的数据存储))或beantalkd离线进行此操作 这将使您的网站再次快速。

我曾经想过如何将hiphop用于php,也许可以帮上忙。

它可以帮助您减少CPU使用率,从而可以省钱。 但是,如果您的查询现在像现在一样糟糕,我想即使是嘻哈也不会削减它

我需要回到firebird还是mysql可以解决这个问题?

MySQL应该可以管理它,但是我发现您的查询确实很复杂。 我认为您执行的联接过多(您也应该重新格式化查询,因为它很难阅读)。 您应该像说的那样优化它。 过去,您需要进行数据库规范化 现在,我认为您应该使用更多的内存和磁盘空间来避免JOINING( 昂贵! )表 之后,您应该通过setcut和predicate表示法计算SQL 我从大学获得的论文过去曾让我感到痛苦,但是我的确取得了很好的成绩。

但老实说,我已经很长时间没有使用SQL了,因为现在我真的迷上了Redis ,它快如闪电 我认为在redis之上实施此操作会容易得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM