I have a table called members. I am looking on advice how to improve it.
- id : This is user id (unique) (auto increment) (indexed)
- status : Can contain ‘activated’, ‘suspended’, ‘verify’, ‘delete’
- admin : This just contains either 0 or 1 (if person is admin or not)
- suspended_note : If a members account is suspended i can add a note so when they try and login they will see the note.
- failed_login_count : basically 1 digit from 0 to 4, counts failed logins
- last_visited : unix timestamp of when they last visited site; (updated on logout) (i do this via php with time() )
- username : can contain from 3 to 15 characters (unique and indexed)
- first_name : can contain letters only and from 3 to 40 chars in length
- last_name : can contain letters only and from 2 to 50 chars in length
- email : can contain an email address (i use php email filter to check if valid)
- password : can contain from 6 to 10 chars in length and is hashed and contains fixed length of 40 chars in database once hashed
- date_time : unix timestamp (i do this via php with time() ). When user logs in
- ip : members ip on registration/logins
- activationkey : i use md5 and a salt to create a unique activation key; length is always 32 chars
- gender : either blank or male/female and nothing else.
- websiteurl: can add they site url;
- msn : can contain msn email address (use regular expression to match this)
- aim : aim nickname (use regular expression to match this)
- yim : yim nickname (use regular expression to match this)
- twitter : twitter username (use regular expression to match this)
suspended_note; first_name; last_name; date_time; ip; gender; websiteurl; msn; aim; yim; twitter can be null because on registration only username, email and password is required so those fields will be null until filled in (they are basically optional and not required) apart from ip which is taken on signup/login.
Could anyone tell me based on the information I have given how I can improve and alter this table more efficently? I would say I could improve it as I tend to use varchar for most things and am looking to get the best performance out of it.
I tend to do quite a few selects and store the user data in sessions to avoid having to query database every time. Username is unique and indexed like id as most of my selects compare have username in it with LIMIT 1 on my queries.
UPDATE:
I wanted to ask if I changed to enum for example how would I do a select and compare query for example in php for enum? I did look online but cannot find any example queries with enum being used. Also if I changed date_time for example to timestamp do I still use time() in php to insert the unix timestamp into date_time column database?
The reason I ask is I was reading one tutorial online that says when the row is queried, selected, updated etc MySQL automatically updates the timestamp for that row; is this true as I rather insert the timestamp using php time() in timestamp field. I use php time() already for date_time but use currently use varchar not timestamp.
Plus server time is in US and in php.ini I set it to UK time but I guess mysql would store it in the time on the server which again is no good as I want them in UK time.
Some tips:
charinstead of varchar. There is a lot of discussion available on that, but whilevarchardoes help you cut down on the size, that is hardly a big issue most of the time.charcan be quicker. this is tricky point though.timestamp. There is a datatype for thatint(5)can hold too much. So if your failed count is max 4, you don’t need that big of a number! Atinyintcan hold upt o 127 signed, or 255 unsigned.A note from the comments:
I agree with this 🙂
Edit: some updates after your new questions.
You can just compare it to the value as if it was a string. The only difference is that with an insert or update, you can only use the give value. Just use
You can use
now()in mysql? (this is just a quick fromthetopofmyhead, could have a minor mistake, but:You can use the php time. The timestamp does not get updated automatically, see the manual (http://dev.mysql.com/doc/refman/5.0/en/timestamp.html): you would use something like this in the definition: