views:

28

answers:

1
+3  Q: 

MySQL Optimization

Hello .. In my database of 5 million records .. Structure of the table:

CREATE TABLE IF NOT EXISTS `music` (
  `id` int(50) NOT NULL auto_increment,
  `artistname` varchar(50) NOT NULL,
  `songname` varchar(50) NOT NULL,
  `duration` varchar(6) NOT NULL,
  `url` varchar(255) NOT NULL,
  `server` int(5) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `artistname` (`artistname`),
  KEY `songname` (`songname`),
  KEY `url` (`url`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

How can I optimize the table and then doing a search through the fields "artistname " and "songname"?

Sorry for bad english. From Russia with love :-D

+5  A: 

Create a FULLTEXT index:

CREATE FULLTEXT INDEX fx_music_artist_song ON music (artistname, songname)

and issue a query like this:

SELECT  *
FROM    mytable
WHERE   MATCH(artistname, songname) AGAINST ('+queen +bohemian' IN BOOLEAN MODE)

This will match across both artistname and songname, even if the words are not leading.

To match only the artist, you can create an additional index on artistname only and use it in a query:

SELECT  *
FROM    mytable
WHERE   MATCH(artistname) AGAINST ('+queen' IN BOOLEAN MODE)

The column set in the MATCH clause should be exactly the same as in the index definition for the corresponding index to be used.

Note that this clause will work even without the index, but in this case it will be much more slow.

Quassnoi