4 silly mistakes in use of MySQL indexes

CREATE TABLE table1 (Index on col1 is redundant as any search on col1 can use primary index. This just wastes disk space and might make some queries which change this table a bit slower.
col1 INT,
col2 INT,
PRIMARY (col1, col2),
KEY (col1)
);
MySQL cannot use an index if the columns do not form a leftmost prefix of the index.Example:
CREATE TABLE table2 (MySQL wont use any indexes for query like
id INT PRIMARY,
col1 INT,
col2 INT,
col3 INT,
KEY (col1, col2)
);
SELECT * FROM table2 WHERE col2=123EXPLAIN SELECT shows this instantly. If you want to run this query faster either change order of columns in the index or add another one.
CREATE TABLE table3 (Query like
id INT PRIMARY,
col1 INT,
col2 INT,
col3 INT,
KEY (col1)
);
SELECT * FROM table2 WHERE col1=123 AND col2=456would use the index on col1 to reduce number of rows to check but MySQL can do much better if you add multicolumn index which covers both col1 and col2. The effect of adding such index is very easy to see with EXPLAIN SELECT.
Syndicated 2007-08-16 12:22:00 (Updated 2007-08-23 21:02:00) from Ilya Martynov
FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.
Keep up with the latest Advogato features by reading the Advogato status blog.
If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!