mysql - Is it better to JOIN on id or to save string value -
i have table mvuser
wih attributes user_id (int) (primary_key), username (varchar) (key), email (varchar)
,..., table job
attributes job_id (int) (primary_key), job_name (varchar) (key)
, table user_job.
is better table user_job
have attributes user_id (int), job_id (int)
, or have attributes username (varchar), job_name (varchar)
, why? 1 faster?
table user_job
queried username or job_name, example:
select job_name user_job user_job.username='username'
or
select job_name user_job join mvuser on mvuser.user_id = user_job.user_id join job on job.job_name = user_job.job_name username='username'
and similar when query users job_name.
i know solution user_id (int), job_id (int)
better if username , job_name can changed, in case can't, permanent.
you should prefere joining on user_id
, job_id
primary key , indexed default (always try join on indexed columns) integer comparison faster string comparison.
if usernames unique optimize where
clause using user_id
instead of username (as user_id indexed , username not). otherwise speed query if create index on username
.
but general practice is:
if don't have performance problem, don't on optimize , ceep readable.
if have performance problem, need measure before , after optimization. if can't measure improvement theoretical speedup not your real problem.
Comments
Post a Comment