views:

60

answers:

3

PHP

$total_points = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM account WHERE id='$id'"),0)

Mysql account table

|user_id|

mysql points table

|id | user_id | points |

or

PHP

$total_points = mysql_query("SELECT points FROM account WHERE id='$id'");

Mysql account table

|user_id| points |

Mysql points table

|id | user_id | points |
+3  A: 

Storing the variable would probably be faster, but that also means constantly updating it, which can be less efficient / slower in the long run.

Using COUNT(id) will be much better than COUNT(*), however. So my vote would be to use COUNT(id) :)

Brad F Jacobs
im working on a players points system, where a user earns points through playing games, the points will be recorded on the points table. and im thinking of showing the users point next to their user name throughout the whole site/or at least at his/her profile page.I dont get it, why you're saying it will be slower/less efficient in long run? you think what method does popular portals such as NewGround/Kongregate uses to show the users points? do they use the storing as variable method, or the dynamic COUND(id)? if COUND(id) they should be facing alots of performance issues, dont they?
Exoot
`COUNT(id)` is not faster than `COUNT(*)`. Unless you're using some sort of aggregation, those two are semantically synonymous.
Daniel Egeberg
A: 

First off, for the first line you had, I believe it's faster to use FOUND_ROWS() than COUNT(*).

$total_points = mysql_num_rows(mysql_query("SELECT FOUND_ROWS() FROM account WHERE id='$id'"),0)

The second approach would be faster once that points table grows large, but you need to make sure you increment the account table properly so those values are in sync. It could become inaccurate if you forget to add the points in some places, or forget to delete them, etc.

Patrick
exactly, when the points table grow large FOUND_ROWS method will be slower. So is it preferred to do FOUND_ROWS every time when getting the total points earned by user? or calculate them and save it to the accounts table with additional 'points' column and load the points from there? and also create a php in the admin panel to update the accounts 'points' column often? which would be the best solution?
Exoot
A: 

The COUNT(*) or query version should be faster because you are not going futher down to mysql_num_rows. For counting you don't need all fields (*), you should simply do COUNT(fieldID) which is a lot faster.

Few Points To Note:

Note that you are getting only one row anyways because you are using where clause, in other words, the result will be either one row or no row if not found:

$total_points = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM account WHERE id='$id'"),0)

Normally you should count when you are expecting more than one record eg:

$total_points = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM account"),0)

Again, for optimized query, use a single field:

$total_points = mysql_result(mysql_query("SELECT COUNT(fieldID) as Num FROM account"),0)
Sarfraz
So which one would be the best choice? storing variable in users account table or dynamically fetching the points via COUND(id)?
Exoot