I am new to SQL and I have a basic question about performance.
I want to create a users database which will store information about my users:
- Id
- Log in name
- Password
- Real name
Later I want to perform a SELECT query on: Id, Log in name and Real name.
What would be the best design for this database, what tables and what keys should I create?
If it’s only about those 4 fields it looks like just one table. Primary key on
ID, unique index onLoginName. You may not want to store the password, but only a hash.Depending on your queries, create different indexes. Furthermore, you may not need the
IDfield at all.UPDATE:
Creating an index on certain column(s) enables the database to optimize its SQL statements. Given your user table:
The databases I know will automatically create an index on the primary key, which in fact means that the database maintains an optimized lookup table, see WikiPedia for further details.
Now say, you want to query users by
LOGIN_IDwhich is a fairly common use case, I guess, you can create another index like:The above index will optimize the
select * from USER where LOGIN_ID='foo'. Furthermore, you can create a unique index instead, assuming that you do not want duplicateLOGIN_IDs:That’s the whole story, so if you want to optimize a query for the users real name (
NAME), you just create another index: