As far as indexing goes, is it proper to index all fields that will be searched (within a WHERE) clause to speed up SELECTS? For example my database contains a profiles table which stores user information such as name, intrestCode,zip, description, and email. The profile record is identified by a PRIMARY id column which uniquely corresponds with the userid. I made zip and intrestCode an index since profiles will be searched by zip and possibly intrestCode (SELECT `blah`,`blah`... FROM profile WHERE zip=?, SELECT `blah`,`blah`... FROM profile WHERE zip=? && intrestCode=?). Am I doing it right?
As far as indexing goes, is it proper to index all fields that will
Share
Sounds basically correct. You should be aware that you can’t use two different indices on the same table in the same query, so if you ran
then the query
can only look up one table from the index. The secret here is that you can create a single index on two tables, like so:
MySQL can use this for queries that use either
zipalone in the WHERE clause, or use bothzipandintrestCode, but not for queries that use onlyintrestCodein the WHERE clause.(This is because each index covers the whole table. If MySQL were to try and look up
zipandintrestCodefrom different tables, then it would be retrieving lots of irrelevant rows from the second index. Therefore, it only looks at one index. If you want it to use the index on both columns, you need to have one index that includes both columns.)