Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Optimization

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

like image 205
Isis Avatar asked Jun 17 '26 08:06

Isis


1 Answers

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.

like image 138
Quassnoi Avatar answered Jun 18 '26 22:06

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!