Performance - order by in MySQL or in PHP -
i know has been asked before @ least in thread: is php sort better mysql "order by"?
however, i'm still not sure right option here since performance on doing sorting on php side 40 times faster. mysql query runs in 350-400ms
select keywords id, sum(impressions) impressions, sum(clicks) clicks, sum(conversions) conversions, sum(not_ctr) not_ctr, sum(revenue) revenue, sum(cost) cost visits campaign_id = 104 group keywords(it's integer) desc keywords , campaign_id columns indexed.
using 150k rows , returns around 1500 rows in total. results recalculated (we calculate click through rates, conversion rates, roi etc, totals whole result set). calculations done in php.
now idea store results php apc quick retrieval, need able order these results columns calculated values, therefore if wanted order click-through rate i'd have use (sum(clicks) / (sum(impressions) - sum(not_ctr)) within query makes around 40ms slower , initial 400ms long time already.
in addition paginate these results, adding limit 0,200 doesn't affect performance.
while testing apc approach executed query, did additional calculations , stored array in memory executed once during initial request , worked charm. fetching , sorting array memory took around 10ms, script memory usage 25mb. maybe it's worth loading results memory table , querying table directly?
this done on local machine(i7, 8gb ram) has default mysql install , production server 512mb box on rackspace on haven't tested yet, if possible ignore server setup.
so real question is: worth using memory tables or should use php sorting , ignore ram usage since can upgrade ram? other options consider in optimizing performance?
in general, want sorting on database server , not in application. 1 reason database should implementing parallel sorts , has access indexes. general rule may not applicable in circumstances.
i'm wondering if indexes helping you. recommend try query:
- with no indexes
- with index on
campaign_id - with both indexes
indexes not useful. 1 particularly important factor called "selectivity". if have 2 campaigns in table, better off doing full-table scan rather indirectly searching through index. because particularly important when table not fit memory (resulting in condition every row requires load page cache).
finally, if going application expands beyond single server, careful. optimal on single machine may not optimal in different environment.
Comments
Post a Comment