Should I create unique index if a column contains unique constraint and I want to fast search by this column?
For example I have a table users
with column login
that should be unique. I need fast search user by the login
column.
Which is the best way to do it:
- create a unique constraint (it creates internal unique index - is it used in select queries with
WHERE login = 'something'
?) - create a unique index
- create a unique index and unique constraint (index duplicates internal index?)
Second case is unique login
on not locked users (column locked = false
). Postgres does not support partial conditions. Should I create a unique conditional and a partial index or is only a partial index enough?
And one more question: should I create new index for a column with a foreign key? For example: users.employee_id
relates to employees.id
, should I create an index on employee
column for optimized query SELECT * FROM users WHERE employee_id = ....
? When are internal indexes used by the optimization engine and when not?