Working with MySQL database.
I have a store table. The store table has at least 80,000 records. A user can search the store table by selecting a suburb from a drop down list. Would I be better off creating a suburb table (see below) and searching the store table for the suburb id or keeping it as a text match on the store table?
Store
- id
- name
- suburb_id
Suburb
- id
- name
I am guessing that using the store_id foreign key, I can create and index?
You are likely better off to create the
suburbtable as you have suggested. Database normalization best practice here would be to store thesuburb_idwith thestorerecord. If it is an integer id, you are likely to get much better performance out of it than relying on a text match even if theCHAR()/VARCHAR()column containing is indexed.Normalizing your
suburbtable will give you some added benefits when querying as well. If you ever added additional columns (besides justid, name) to thesuburbtable, you could, by way of aJOINquery for stores having different suburb attributes or aggregates. (Not that your end users would need this necessarily, but you could use it)For example, how many stores per suburb? (you could do this with a text field as well)
Or if you added the population to your Suburb table, which stores are in suburbs with popultaion > 40000. You could not do this with a text field alone in your
Storetable.Normalization will also help if you wish to store a user’s suburb preference for later use. Hypothetically, in your
Userstable, you would add a column as a foreign key reference toSuburb.id. Making use of theON DELETE SET NULLforeign key actions, if a suburb is removed from the database that change could be immediately reflected in your user’s preference.