[MySql] Which is more efficient?

Started by G_G, August 03, 2011, 04:17:22 am

Previous topic - Next topic

G_G

Which way do you think is more efficient? I have my table setup like this.



user_idawardspoints
11,2,350

Then when querying to get the awards, I simple explode the string into an array. It works perfectly. But would it be more efficient to setup the table like this?




user_idaward_idaward_points
1110
1230


stripe103

If you want to have different points for the different awards, then the lower one is required. Unless I think something wrong. And unless you have the points for the different awards somewhere else.

Blizzard

The first way is kind of more efficient if you don't think of sorting it by award ID or if you want to filter out all users that have a specific set of awards (e.g. you want to know who has award 1). But the second way is good practice and database engines are built to optimize queries on tables in the second format. Access of numbers is faster than access of strings. Also, if you want every award to have a different number of points, I suggest you have a separate table for award point and (if you want) use a "view" with a field that uses a formula to calculate the sum of the points.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

We do have a separate table for achievements.
award_id | award_name | award_description | award_points | award_image

In the first one, when a user gains an achievement it takes points from the admin table and adds it to their current. I think I may switch to the 2nd format though as it'll fit a more accurate points calculation.

Blizzard

As I said, I suggest that you make a view and use that one instead of the user table.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zeriab

I highly suggest you go for the second design for recording the awards each user have.
Ask yourself if the award_points column really is necessary. Let's say that you change the amount of points an award gives in your Awards table. Should this change be reflected in the UserAward table?
If it should consider removing the column in UserAward since it just contains duplicate information.
To get the sum you can do as Blizzard suggests ^^

G_G

Alright. Took your guys' advice. I appreciate it. :3 The achievements system over at Decisive Media is much more efficient now. And switching the way I did it removed a lot of queries and unneeded code.