I have only one table in my mySql database for a very basic website and am using php/mysql calls to add user records to the single table I have called ‘RegisteredUsers’ — each record is:
(name, member number, name of their subfolder on the server).
A new requirement for me — each of my users can enter from zero to 5000 items and for each item they type a text description.
Each of the user’s ‘items’ is the (name, description) of a piece of ‘early man’ antiquity — a clay pot from prehistoric times, an artifact buried with one of the pharoahs, etc. — and the (name, description) tuples will be limited-size text, ie. (name: text, length=50; description: text, length=512) — the site forces the user to be very succinct in the name and description.
Now that I have to allow each user to add an arbitrary number (from 0 to 5000) of their antiques, although I am a database newbie, I am inclined to create a 2nd table and use the “member number” from the 1st table to index the 2nd table.
Each record of this 2nd table will have the following fields:
(member number, name-of-the-antique, description).
I’m concerned that if a user wants to delete an item with the name “Babylonia clay pot” from this 2nd table, with up to 5000 antiques for the user — performance will take a hit as I search the 2nd database for
member number=NN and name=One-Of-5000-Antiques.
Is this the correct approach?
The answer to performance concerns like this is pretty much always an index. Usually you would add one more column to your antiques table, which is a numeric, auto-incrementing primary key. So instead of deleting the item “Babylonia clay pot”, you delete the item with the id 42. This primary key will be indexed and access to any row by id will be virtually instantaneous, no matter how large your table grows (within limits, obviously, but you’ll probably never come close to those limits). You could also create an index on the
name-of-the-antiquecolumn, but that’ll be less efficient (still way faster than no index though).Read about indexes here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html