morphboy23 Posted July 7, 2007 Share Posted July 7, 2007 Ok, currently I have this query: "SELECT (SELECT COUNT(*) FROM inquiries WHERE iduser = '$id') AS icount, (SELECT COUNT(*) FROM responses WHERE iduser = '$id') AS rcount, (SELECT IFNULL( SUM( total_value ) / SUM( total_votes ) , 0 ) FROM ratings, responses WHERE responses.iduser = '$id' AND responses.idresponse = ratings.id AND ratings.total_votes > 0) AS avgscore" With 3 rows in 'users', 35 in both 'ratings' and 'responses', and 29 in 'inquiries', this query took about 1/1000th of a second on my machine. These values are used in an equation in php, and must are fetched every time a user's profile page is loaded. With many more rows in these tables, would this query be very slow? I'm wondering whether or not to add a field to one of the tables to hold the calculated score when an inquiry/response row is added, or when a rating row is updated. Or should I just keep things like this? It's the age old storage space & feasibility vs. speed Quote Link to comment Share on other sites More sharing options...
bubblegum.anarchy Posted July 8, 2007 Share Posted July 8, 2007 Does the following produce the same results faster? SELECT sum(inquiries.iduser IS NOT NULL) AS icount, sum(response.iduser IS NOT NULL) AS rcount, ifnull(sum(ratings.total_value) / sum(ratings.total_votes), 0) AS avgscore FROM users LEFT JOIN inquires ON users.id = inquries.iduser LEFT JOIN responses ON users.id = responses.iduser LEFT JOIN ratings ON responses.idresponse = ratings.id WHERE users.id = '{$id}' Speed is probably more important when dealing with the dynamics of the Internet. Storage size is less dynamic and easier to control than bandwidth traffic. Quote Link to comment 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.