Select rows where column LIKE dictionary word

Posted by Gerve on Stack Overflow See other posts from Stack Overflow or by Gerve
Published on 2012-09-15T14:43:58Z Indexed on 2012/09/15 15:37 UTC
Read the original article Hit count: 163

Filed under:
|

I have 2 tables:

Dictionary - Contains roughly 36,000 words

CREATE TABLE IF NOT EXISTS `dictionary` (
  `word` varchar(255) NOT NULL,
  PRIMARY KEY (`word`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Datas - Contains roughly 100,000 rows

CREATE TABLE IF NOT EXISTS `datas` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `hash` varchar(32) NOT NULL,
  `data` varchar(255) NOT NULL,
  `length` int(11) NOT NULL,
  `time` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `hash` (`hash`),
  KEY `data` (`data`),
  KEY `length` (`length`),
  KEY `time` (`time`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=105316 ;

I would like to somehow select all the rows from datas where the column data contains 1 or more words.

I understand this is a big ask, it would need to match all of these rows together in every combination possible, so it needs the best optimization.

I have tried the below query, but it just hangs for ages:

SELECT      `datas`.*, `dictionary`.`word`
FROM        `datas`, `dictionary`
WHERE       `datas`.`data` LIKE CONCAT('%', `dictionary`.`word`, '%')
AND         LENGTH(`dictionary`.`word`) > 3
ORDER BY    `length` ASC
LIMIT       15

I have also tried something similar to the above with a left join, and on clause that specified the like statement.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql