سؤال

If I have a table with the following columns as the primary key: Username, Title, Start Date, then the database will automatically create an index on it.

  1. If I want to select by username, and start date for a query.....will it use the index above OR do I need to specify an additional index?
  2. If title and start date identify uniquely but I also add username to the primary key, would that make it a superkey?
هل كانت مفيدة؟

المحلول

1) If I want to select by username, and start date for a query.....will it use the index above OR do I need to specify an additional index?

You have a complex condition, say, like this:

username = 'blah-blah-blah' AND startdate > '01.01.2010'

If your table's PK defined like:

PRIMARY KEY (Username, Title, StartDate)

Then index will be used for first part involving username field. startdate values will be evaluated in natural order.

If you want index to be used for both parts of complex condition, either create additional index on startdate or change order of fields in the PK:

PRIMARY KEY (Username,  StartDate, Title)

2) If title and start date identify uniquely but I also add username to the primary key, would that make it a superkey?

It is a good practice not to abuse an unique index with additional fields. In your case create PK on title and start date and then (if necessarily) create separate index on user name field.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top