Problem Solution Discussion Adding Indexes

RENAME TABLE log TO log_2001_05, log_temp TO log; To rot at e log t ables t o keep a set of daily t ables covering t he last week, you could run t he following st at em ent s daily: CREATE TABLE log_temp ...; DROP TABLE IF exists log_7; RENAME TABLE log_6 TO log_7, log_5 TO log_6, log_4 TO log_5, log_3 TO log_4, log_2 TO log_3, log_1 TO log_2, log TO log_1, log_tmp TO log;

8.8 Adding or Dropping Indexes

8.8.1 Problem

Table lookups are slow. Or INSERTS and UPDATES are slow.

8.8.2 Solution

ALTER TABLE can not only drop or add colum ns, it can drop or add indexes on t hose colum ns. These operat ions oft en are useful for im proving t he perform ance of a dat abase. Typically, indexing a colum n t hat you query frequent ly helps SELECT st at em ent s run fast er because t he index allows MySQL t o avoid full t able scans. Dropping indexes can som et im es be useful as well. Whenever a row is m odified, MySQL m ust updat e any indexes t hat include t he m odified colum ns. I f you dont act ually use a part icular index very m uch, it s possible t hat your t able is overindexed and t hat dropping t he index will speed up perform ance of t able updat es.

8.8.3 Discussion

For t he discussion in t his sect ion, it s useful t o begin again wit h a new copy of t he mytbl exam ple t able. Use DROP TABLE and CREATE TABLE t o rem ove t he exist ing version and recreat e it in it s original form : DROP TABLE mytbl; CREATE TABLE mytbl i INT, c CHAR1 ; I n earlier sect ions of t his chapt er, SHOW COLUMNS was used t o see t he effect of t able m odificat ions. For index- changing operat ions, use SHOW INDEX rat her t han SHOW COLUMNS . Current ly, t he t able has no indexes, because none were specified in t he CREATE TABLE st at em ent : mysql SHOW INDEX FROM mytbl; Empty set 0.00 sec

8.8.4 Adding Indexes

There are four t ypes of st at em ent s for adding indexes t o a t able: ALTER TABLE tbl_name ADD PRIMARY KEY column_list ; ALTER TABLE tbl_name ADD UNIQUE index_name column_list ; ALTER TABLE tbl_name ADD INDEX index_name column_list ; ALTER TABLE tbl_name ADD FULLTEXT index_name column_list ; The first st at em ent adds a PRIMARY KEY , which m eans t hat indexed values m ust be unique and cannot be NULL . The second creat es an index for which values m ust be unique wit h t he except ion of NULL values, which m ay appear m ult iple t im es . The t hird adds an ordinary index in which any value m ay appear m ore t han once. The fourt h creat es a special FULLTEXT index t hat is used for t ext - searching purposes. FULLTEXT searches are discussed in m ore det ail in Recipe 4.12 . For t hose st at em ent s t hat include index_name in t he synt ax, t he index nam e is opt ional. I f you dont specify a nam e, MySQL assigns one aut om at ically. column_list indicat es which colum ns t o index; it should be a list of one or m ore colum n nam es separat ed by com m as. As sim ple exam ples, t he first of t he following st at em ent s creat es a single-colum n index on c , and t he second creat es a m ult iple-colum n index t hat includes bot h c and i : ALTER TABLE mytbl ADD INDEX c; ALTER TABLE mytbl ADD INDEX c,i; I n m any cases, indexed colum ns m ust be declared NOT NULL . For exam ple, if you creat e mytbl as an I SAM t able t ype, t he preceding ADD INDEX st at em ent s will fail because I SAM t ables do not allow NULL in any kind of index. And no m at t er what t he t able t ype, colum ns in a PRIMARY KEY cannot cont ain NULL values. I f you t ry t o add an index and MySQL issues a NULL - relat ed com plaint , use ALTER TABLE t o change t he relevant colum n or colum ns t o NOT NULL and t hen t ry adding t he index again. For exam ple, if you t ry t o m ake colum n i a PRIMARY KEY as follow s, an error result s: mysql ALTER TABLE mytbl ADD PRIMARY KEY i; ERROR 1171 at line 5: All parts of a PRIMARY KEY must be NOT NULL; If you need NULL in a key, use UNIQUE instead To deal wit h t his, redefine i t o be NOT NULL , t hen t ry again: mysql ALTER TABLE mytbl MODIFY i INT NOT NULL; mysql ALTER TABLE mytbl ADD PRIMARY KEY i; Alt ernat ively, as t he error m essage indicat es, you can creat e a UNIQUE index rat her t han a PRIMARY KEY should you w ish t o allow NULL values in t he index.

8.8.5 Dropping Indexes