简体   繁体   English

PHP MySQL优化

[英]php mysql optimisation

I would like to optimize my php scripts, actually i'm implementing the memcached(it reduce the time from: 30 secs to 5 secs) for php. 我想优化我的php脚本,实际上我正在为php实现memcached(将时间从30秒减少到5秒)。 first think that you must see is the script at app.promls.net, it takes like 3 seconds on build it(view source at the end and you'll se comment box with the time execution). 首先认为您必须看到app.promls.net上的脚本,构建该脚本大约需要3秒钟(最后查看源代码,随着时间的执行,您将看到注释框)。

next thinks i optimize my select statement using explain: 接下来认为我使用解释优化了我的选择语句:

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

the explain : 解释:

> 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'

as you can see it returns me the 142 rows from table properties and a temporary table, thats because the where has the next condition: 如您所见,它从表属性和一个临时表中返回了142行,那是因为where具有下一个条件:

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

so to fix that i implement an index on p.status. 因此,要修复我在p.status上实现了索引。 but the mismaching is when i use a where with only one value as: 但是误配是当我使用只有一个值的where时:

where p.status in('active');

it only returns me 77 rows and in extra: "using where; Using temporary; using filesort" on explain; 它只给我返回77行,另外还包括:“在哪里使用;使用临时文件;使用文件排序”; if only change the where condition, but if i take off the order condition it makes the same 77 rows and in extra: "using where; " so i would like to know how could i optimize mysql using explain and when the selection contains "order" or "group by" statements. 如果只更改where条件,但是如果我取消了order条件,它将产生相同的77行,并另外加上:“ using where;”,所以我想知道如何使用explain和何时包含“ order”来优化mysql。 ”或“分组依据”语句。

by the way i have hear that using store procedures are more faster that plain select from php, but loooking in to the source of wordpress dont use procedure so i assume that use procedures is not the solution. 顺便说一句,我听说使用存储过程比从php中选择普通存储过程要快,但是寻找wordpress的来源不要使用过程,所以我认为使用过程不是解决方案。

how ever i was thinking to use the hiphop for php maybe it could help. 我曾经想过如何将hiphop用于php,也许可以帮上忙。

i'm using a dedicated server. 我正在使用专用服务器。 the configuration for php must be fine, cause i have another sub domain that loads most faster than this app, the script are the same but the db is firebird and i'm implementing the memcached and the store procedures. php的配置必须很好,因为我有另一个子域,其加载速度比该应用程序快,脚本是相同的,但数据库是firebird,并且我正在实现memcached和存储过程。

what do you think, do i need to get back to firebird or mysql could manage this? 您如何看待,我需要重新使用Firebird还是mysql可以解决这个问题?

I really appreciate your help, if you see my app takes like 30 seconds to execute my script so i really need to optimize it; 非常感谢您的帮助,如果您看到我的应用需要30秒钟才能执行我的脚本,那么我真的需要对其进行优化;

tell me if you need more information, i'll be absolutely available for gave it! 告诉我,如果您需要更多信息,我将为您提供一切! lets chat by msn or skype if you wish. 如果愿意,可以通过msn或skype聊天。

thanks in advance 提前致谢

tell me if you need more information 告诉我是否需要更多信息

First I think you should provide us with your schema(domain). 首先,我认为您应该向我们提供您的架构(域)。 I think you should provide us with an image by using a tool like for example sql2diagram (googled for this). 我认为您应该使用sql2diagram之类的工具为我们提供图像。 Maybe you should first optimize your schema 也许您应该先优化您的架构

actually i'm implementing the memcached(it reduce the time from: 30 secs to 5 secs) for php 实际上我正在为PHP实现memcached(将时间从30秒减少到5秒)

First I would like to know how it can take that long to get data from memcached(MEMORY). 首先,我想知道从memcached(MEMORY)获取数据要花这么长时间。 It should not take any time at all to get data from memcached. 从memcached获取数据完全不需要花费任何时间。 I am also wondering if you need to invalidate cache a lot or if this is a one time query. 我也想知道您是否需要使缓存无效很多,或者这是一次查询。 Because in that case you need to do another MySQL to put the new data in the cache. 因为在这种情况下,您需要执行另一个MySQL才能将新数据放入缓存中。 If possible I think you should do that offline in advance using a message queue like for example redis (I would also use redis as my datastore) or beanstalkd . 如果可能的话,我认为您应该事先使用消息队列(例如redis (我也将redis用作我的数据存储))或beantalkd离线进行此操作 That is going to make your site fast again. 这将使您的网站再次快速。

how ever i was thinking to use the hiphop for php maybe it could help. 我曾经想过如何将hiphop用于php,也许可以帮上忙。

It can help you reduce CPU usage which can safe you money. 它可以帮助您减少CPU使用率,从而可以省钱。 But if your query is BAD like it is now I don't think even hiphop will cut it 但是,如果您的查询现在像现在一样糟糕,我想即使是嘻哈也不会削减它

do i need to get back to firebird or mysql could manage this? 我需要回到firebird还是mysql可以解决这个问题?

MySQL should be able to manage it, but I find your query really complicated. MySQL应该可以管理它,但是我发现您的查询确实很复杂。 I think you are doing too much joins(You should also reformat query because it is hard to read). 我认为您执行的联接过多(您也应该重新格式化查询,因为它很难阅读)。 You should optimize it like you are saying. 您应该像说的那样优化它。 In the past you needed to do database normalization . 过去,您需要进行数据库规范化 Now I think you should use more memory, disc space instead to avoid JOINING( expensive!! ) tables . 现在,我认为您应该使用更多的内存和磁盘空间来避免JOINING( 昂贵! )表 After that you should construct SQL by calcutation in set and predicate notation . 之后,您应该通过setcut和predicate表示法计算SQL The paper I got from my university, I found a pain in the ass in the past, but I did get good results with it. 我从大学获得的论文过去曾让我感到痛苦,但是我的确取得了很好的成绩。

But to be honest I have not used SQL in a long time, because now I am really hooked to redis which is lightning fast . 但老实说,我已经很长时间没有使用SQL了,因为现在我真的迷上了Redis ,它快如闪电 I think it would be a lot easier to implement this on top of redis. 我认为在redis之上实施此操作会容易得多。

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

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