Here is my current table;
CREATE TABLE `linkler` (
`link` varchar(256) NOT NULL,
UNIQUE KEY `link` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I will only use these 2 queries on the table, SELECT EXISTS (SELECT 1 FROM linkler WHERE link = ?) and INSERT INTO linkler (link) VALUES (?)
I don’t know much about indexing databases. Since I won’t be adding same thing twice, I thought marking it unique would be a good idea. Is there anything I can do to increase performance? For example, can I do something that rows are always sorted so that mysql can do binary search or something similiar?
Adding a unique index is perfect. Also, since you have a unique index, you don’t need to check for existence before you do an insert. You can simply use
INSERT IGNOREto insert the row if it doesn’t exist (or ignore the error if it does):Whether that will be faster than doing a SELECT/INSERT combination depends on how often you expect to have duplicates.
ETA: If that is the only column in this table, you might want to make it a
PRIMARY KEYinstead of just aUNIQUE KEYalthough I don’t think it really matters much other than for clarity.