0

I have a table like this

CREATE TABLE `tbl` (
    `id` int(10) unsigned NOT NULL,
    `pid` int(10) unsigned NOT NULL,
    `cid` int(10) unsigned NOT NULL,
    `name` int(11) DEFAULT NULL,
    `desc` varchar(500),
    `deleted` tinyint(1) unsigned NOT NULL DEFAULT 0,
    `createdAt` datetime DEFAULT NULL,
    PRIMARY KEY `pid_cid` (`pid`,`cid`)
    ) ENGINE=InnoDB 

WHERE clause of select queries are those:

where pid=pid [and deleted = 1];
where cid=cid [and deleted = 1];
where pid=pid and cid=cid [and deleted = 1];
  1. Should I index pid and cid separately?
  2. Should I index deleted?
Radim Bača
  • 10,526
  • 1
  • 18
  • 32
Sato
  • 7,432
  • 15
  • 54
  • 101

1 Answers1

1

The optimal set of indexes would be:

  • pid, deleted
  • cid, deleted
  • pid, cid, deleted

Whether you need all three depends on your data. How many different deleted values are there for each pid/cid. The first two may be sufficient.

Indexing deleted by itself is probably not useful. Presumably, it only takes on two values, which is generally not a good candidate for an index.

Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709