I have a query like this:
Select E.ID, E.NAME,
date_format( CONVERT_TZ( CONVERT_TZ( E.ST, '+00:00', '+5:00' ),
'+00:00', if( ET.GT LIKE '%.5', REPLACE( ET.GT, '.5', ':30' ) , CONCAT( ET.GT, ':00')
) ), '%Y-%m-%e %r'), concat( date_format( CONVERT_TZ( CONVERT_TZ( E.ET, '+00:00',
'+5:00' ) , '+00:00', if( ET.GT LIKE '%.5', REPLACE( ET.GT, '.5', ':30' ) , CONCAT(
ET.GT, ':00' ) ) ), '%Y-%m-%e %r') , ' ', if ( TC is NULL, '', concat(TC, 'T'))),
EE.CL, EC.EC, CONCAT( '##PATH##', E.ID, '/')
and these fields are pulled from 4 different tables. The conditions, formatting stuff, etc is to convert the Date from Eastern to specific timezone per settings from one of the tables.
Certainly not a nice looking query :-). Here are the reason(s), I am trying to do all these wacky thing in on query.
- These results are returned as array and then encoded as JSON and sent back to the caller.
- I don’t have to loop through the results in the PHP. I can just pass the results as is.
- I don’t have to worry about extra columns that I don’t need. I can just pull only columns needed.
This is just working great for me!
But, from the performance and other design perspective(s):
Questions: Is it good to leave as is or I need think about doing the processing on PHP?
I will have to do two things when I remove the complexity in the query (If, Convert_Tz,etc)
- Loop through the returned array and call a function to calculate the Timezone.
- Remove the unnecessary array elements.
Your thoughts?
EDIT:
Here are the joins:
LEFT JOIN eeemps EER ON e.id = EER.eid
LEFT JOIN eemp Ee ON eer.empid = ee.empid
LEFT JOIN EclassS EC ON E.EVN = EC.E_ID
LEFT JOIN E_tz NE ET ON E.TZID = ET.ID
where date between x and y, and e_id = 123
Thats all “where” cond is. Ignore the table and field names. I changed them.
PHP part of your server probably already has some nice work, so doing the most possible in MySQL is the right way of dealing with a problem. Moreover, MySQL way is 99 times from 100 faster than PHP. And do not worry about large queries. If possible do some generation of the code to (for example joins) and also keep in mind that formatting is very important to make difficult things way easier.