shamoon Posted October 30, 2007 Share Posted October 30, 2007 Hello all, I have a query SELCT b.title, IF( b.total_rentals > 0, b.copies_owned / b.total_rentals, 100) AS usage_ratio FROM books b, WHERE b.status = 1 ORDER BY usage_ratio DESC Is there a lot of overhead by doing my query like that? I mean, including the calculation like that? Thanks, Shamoon Quote Link to comment https://forums.phpfreaks.com/topic/75408-doing-math-in-a-query/ Share on other sites More sharing options...
Barand Posted October 30, 2007 Share Posted October 30, 2007 I think the biggest overhead will come from the ORDER BY rather than the calculation itself. BTW, remove "," before the WHERE Quote Link to comment https://forums.phpfreaks.com/topic/75408-doing-math-in-a-query/#findComment-381464 Share on other sites More sharing options...
shamoon Posted October 30, 2007 Author Share Posted October 30, 2007 Hmmm... so the calculation isn't that bad? Or is it the ORDER by that USES the calculation that is bad? Quote Link to comment https://forums.phpfreaks.com/topic/75408-doing-math-in-a-query/#findComment-381466 Share on other sites More sharing options...
Barand Posted October 30, 2007 Share Posted October 30, 2007 If a query is slow because the table is large you can often speed it up by adding an index. In the case of a calculated value you can't do that. However, as you need the results in that order it's something you'll have to live with. If you only have a few thousand records I wouldn't worry. Try timing it with and without the ORDER BY to assess the impact. Quote Link to comment https://forums.phpfreaks.com/topic/75408-doing-math-in-a-query/#findComment-381468 Share on other sites More sharing options...
Barand Posted October 30, 2007 Share Posted October 30, 2007 I've just tried a similar query on an 11,000 record table and any differences in times between calculate/not calculate and sort/not sort are measured in hundredths of seconds. Quote Link to comment https://forums.phpfreaks.com/topic/75408-doing-math-in-a-query/#findComment-381552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.