In Symfony2 doc ‘How to use PdoSessionStorage to store Sessions in the Database’, which show the session table like this:
CREATE TABLE `session` (
`session_id` varchar(255) NOT NULL,
`session_value` text NOT NULL,
`session_time` int(11) NOT NULL,
PRIMARY KEY (`session_id`),
UNIQUE KEY `session_id_idx` (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Why unique key constraint is needed for the primary key?
A primary key is by definition a unique key. In this example, it’s just a duplicated unecessary key.
In other cases, where the primary key could potentially be composite (consisting of two or more fields), such secondary indexes would allow the individual components of the primary key to be addressed seperately.
For instance:
where you do a query with
WHERE b=something, would not use the primary key index, as B’s index entries are tied to ‘a’, and you’re not using ‘a’ in the query. Adding a secondary seperate index dedicated to just B allows index use for this where clause.