简体   繁体   中英

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. 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).

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:

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

so to fix that i implement an index on p.status. but the mismaching is when i use a where with only one value as:

where p.status in('active');

it only returns me 77 rows and in extra: "using where; Using temporary; using filesort" on explain; 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.

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.

how ever i was thinking to use the hiphop for php maybe it could help.

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.

what do you think, do i need to get back to firebird or mysql could manage this?

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;

tell me if you need more information, i'll be absolutely available for gave it! lets chat by msn or skype if you wish.

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). 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

First I would like to know how it can take that long to get data from memcached(MEMORY). It should not take any time at all to get data from 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. 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 . That is going to make your site fast again.

how ever i was thinking to use the hiphop for php maybe it could help.

It can help you reduce CPU usage which can safe you money. 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?

MySQL should be able to manage it, but I find your query really complicated. 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 . After that you should construct SQL by calcutation in set and predicate notation . 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 . I think it would be a lot easier to implement this on top of redis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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