If I have a column, set as primary index, and set as INT.
If I don’t set it as auto increment and just insert random integers which are unique into it, does that slow down future queries compared to autincrementing?
Does it speed things up if I run OPTIMIZE on a table with its primary and only index as INT? (assuming only 2 columns, and second column is just some INT value)
(the main worry is the upper limit on the autoincrement as theres lots of adds and deletes in my table)
In
MyISAMit will in fact speed it (marginally).In
InnoDB, this may slow theINSERToperations down due to page splits.This of course implies that your numbers are really unique.
AUTO_INCREMENTandINTmay be used together.OPTIMIZE TABLEwill compact you table and indexes, freeing the space left from the deleted rows and page splits. If you had lots ofDELETEoperations on the table orINSERTout of order (like in your solution with random numbers), this will help.It will also bring the logical and physical order of the index pages into consistency with each other which will speed up full scans or ranged queries on
PK(PK BETWEEN val1 AND val2), but will hardly matter for random seeks.BIGINT UNSIGNED(which can also be used withAUTO_INCREMENT) may hold up values up to18446744073709551615.