In the below code what does the
# INDEX youtube_band_id (youtube_band_id),
# INDEX youtube_video_time (youtube_video_time),
# INDEX youtube_video_category (youtube_video_category(15))
actually do? Why is it used for only 3 of the 5 fields?
Code
# // Create our You Tube videos table
# $tbl = "CREATE TABLE {$jamroom_db['jrYouTubeVideos']} (
# youtube_id INT(7) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
# youtube_band_id INT(7) UNSIGNED NOT NULL,
# youtube_video_id VARCHAR(25) NOT NULL DEFAULT '',
# youtube_video_time INT(11) UNSIGNED NOT NULL DEFAULT '0',
# youtube_video_category VARCHAR(30) NOT NULL DEFAULT '',
# INDEX youtube_band_id (youtube_band_id),
# INDEX youtube_video_time (youtube_video_time),
# INDEX youtube_video_category (youtube_video_category(15))
# )";
# dbVerifyTable($jamroom_db['jrYouTubeVideos'],$tbl);
Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA
Posted: 09/01/09 11:28
The "general" rule of thumb is that:
- if the column is used in a WHERE condition
- if the column is used in a JOIN condition
you should index it. However, there is an INSERT/UPDATE/DELETE penalty when you have column indexes, since when data is inserted, updated or deleted in the database, the respective INDEX's must also be updated to stay in sync with the new data, so this is why you don't just simply index every column.