0

I have multiple tables through my site:

Is it best to store the foreign key from the lookup table or the varchar itself.

Two of my site tables are

job_profile
    id
    title
    salary_range_seeking
    cv_link
    datetime_created
    job_type

job_type
    id
    type

job type- type values are Full Time, Part Time

So for example is it best to store the id from job_type in the column job_type and maintain referential integrity or have it so on the front end the user picks the value and the text value is stored in the column job_type. I am looking for the best performance option and recommended practice.

2 Answers 2

0

You'll get better performance using a foreign key instead of storing the text value in a field, because it'll reduce the amount of memory used.

This is the recommended practice when the value of the field (in this case job_type) is going to be repeated with a high rate.

8
  • if the value is not always required would i need to allow null on the field as i have read this is not best practice Aug 7, 2012 at 21:52
  • will i get a better performance if i need to do joins though. Aug 7, 2012 at 21:56
  • bennadel.com/blog/… and on other sites online. Aug 7, 2012 at 22:22
  • 1
    In my opinion in this the performance will be better doing a join instead of storing the texts "Full Time" and "Part Time" repeatedly (using a Tinyint instead of a varchar). It will reduce disk usage, memory consumption, and queries will execute faster. On the other side if the tables are not big enough you'll not get great benefits.
    – mvillaress
    Aug 8, 2012 at 6:28
  • 1
    I recommend you the book "High Performance MySQL" by Baron Schwartz, Peter Zaitsev and Vadim Tkachenko. It explains how MySQL do these tasks and why in this case the JOIN solution would be better.
    – mvillaress
    Aug 8, 2012 at 6:35
0

Performance for reads or writes? or running reports? How you store data depends a lot on what you intend to do with it, and how you intend to do it.

Now, it is generally accepted to store the id, not the varchar, there are several reasons for this, but the primary one is that it's the generally accepted way of doing it. This will make support for your work easier in the future for other people, and probably for you too. With the basic layout you are providing there should be almost no impact to performance regardless of what you are using this data for.

If the primary purpose of the job_profile table is to run reports off of it, you get better performance by storing the varchar and not needing to do a join. This type of de-normalizing you would normally see in a data warehouse type of application where you make trade offs on using more memory and storage for faster retrieval and presentation of data.

2
  • if the field is optional is it best to store the varchar- i.e. if not every job has a type. Or is it best to have a foreign key constraint and allow null. Aug 7, 2012 at 21:55
  • I would have an FK constraint and allow null. Just remember, you would want to do left joins to the job_type table if you allow nulls, as inner joins would only return results that have non-null values. Aug 7, 2012 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.