mysql - Best way to index a table with a unique multi-column? -


i creating table store around 100million rows in mysql 5.6 using innodb storage engine. table have foreign key link table around 5 million rows.

current table structure:

`pid`: [foreign key table]  `price`: [decimal(9,2)]  `date`: [date field] 

and every pid should have 1 record date

what best way create indexes on table?

option #1: create primary index on 2 fields pid , date

option #2: add column id auto_increment , primary index , create unique index on column pid , date

or other option?

only select query using on table is:

select pid,price,date table pid = 123 

based on said (100m; query is...; innodb; etc):

primary key(pid, date); 

and no other indexes

some notes:

  • since innodb, rest of fields "clustered" pk, lookup pid acts if price part of pk. where pid=123 order date efficient.
  • no need index(pid, date, price)
  • adding auto_increment gains nothing (except hint of ordering). if needed ordering, index starting date might best.
  • extra indexes slow down inserts. unique ones.

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -