Based on the incomplete specification, I would do this:
CREATE UNIQUE INDEX stock_UX1 ON stock (storeid,seedid,stk)
This index would satisfy the requirement for an index with storeid
as the leading column. (And we know will have that requirement if this is InnoDB and storeid
is a foreign key.)
With such a short table row, I'd go ahead and make it a covering index, and include all of the columns. Then queries can be satisfied directly from the index pages without lookups to data pages in the underlying table.
Since we know that (seedid,storeid)
is unique (given as the PRIMARY KEY), we know (storeid,seedid)
is also unique, so we might as well declare the index to be UNIQUE.
There are other choices; we don't have to create that index above. We could just do this instead:
CREATE INDEX stock_IX2 ON stock (storeid)
But that will use nearly the same amount of space, and not be as beneficial to as many possible queries.
The secondary index will contain the primary key of the table; so that second index will include the seedid
column, given the PRIMARY KEY of the table. That is, the index is equivalent to this:
CREATE INDEX stock_IX3 ON stock (storeid,seedid)
And we know the combination of those two columns is unique, so we can include the UNIQUE keyword
CREATE UNIQUE INDEX stock_UX4 ON stock (storeid,seedid)
If we do an EXPLAIN on a query of the form
EXPLAIN
SELECT t.storeid
, t.seedid
, t.stk
FROM stock t
WHERE t.storeid = 'foo'
we are likely to see a range scan operation on the secondary index; but retrieving the value of stk
column will require lookup to the data pages in the underlying table. Including the stk
column in the secondary index will make the index a covering index for the query. With the index first recommended in the answer, we expect the EXPLAIN
output to show "Using index".