Question are,
1.How can I improve the performance of SELECT queries in mysql utilizing REGEXP?
The table looks like
create table `tweets`(
`id` bigint auto_increment,
`tweet` varchar(140),
`time` datetime,
primary key(`id`)
);
Here the following query takes about 0.35 seconds.
select tweet from tweets where tweet regexp '^[abcdef]{1,4}$';
- Will indexing
tweetmake it faster? If so, what type of index should I use? - My table engine is
InnoDB, Is there any other table engine that will become beneficial?
Your best bet is to reduce the result set to evaluate against the regular expression before evaluating. Regular expressions are, for all intents and purposes, impossible to index for.
If I had to come up with a way for this, I would examine patterns that are commonly searched against, and mark them in some indexible way at insert time. For example if you use the
^[abcdef]{1,4}$expression to search against a lot, I’d make a boolean columnfirst4AThruFand on an insert/ update trigger, update the column to true or false based on whether or not it matched the regular expression. If I indexed thefirst4AThruFcolumn, and the column had enough selectivity, I could write the query:and this should be pretty zippy.
Other possibilities to consider are full-text queries or LIKE clauses, although in the case mentioned above I don’t expect them to work well.