How to Speed Up Pattern Matching Queries

pattern matching queriesFrom time to time I see pattern matching queries with conditions that look like this: “where fieldname like ‘%something%’ “. MySQL cannot use indexes for these kinds of queries, which means it has to do a table scan every single time.

(That’s really only half true — there are the FullText indexes. In another blog post I will cover FullText indexes as well.)

I recently was trying to find a solution, and my friend Charles Nagy reminded me of Trigrams. Let me show you the Trigram of the name Daniel:

But how is this useful?

Let me show you an example. You have the following email schema:

With data like these:

And we are looking for email addresses like ‘%n.pierre%’:

There are 11 email addresses, but it has to scan the whole index (318458 rows). That’s not good! Let’s try and make it better.

Trigram table

I created a table like this:

As we can see, there is an index called “trigram“.

The plan is to create a trigram for every single email addresses. I wrote the following trigger:

When there is an insert, it creates and inserts the trigrams into the email_trigram table. Trigrams for anderson.pierre:

With the following query, we can find all the email addresses with n.pierre:

It does not have to read the whole table, but still needs to read a lot of rows and even using filesort. I did not want to create trigrams manually, so I wrote the following procedure:

Since with trigrams we are looking for parts of the words (like err or ier), there can be many matches. If we are using a longer condition like derson.pierre, the procedure needed to read 65722 rows. This is also a lot.

Let’s have a look at the selectivity a bit:

There are parts that give back many rows. As I mentioned, more parts mean more rows.

I was hoping for a bigger improvement, so I wondered what else we could do. MySQL cannot use an index because of the leading %. How can we avoid that? Let’s save all the possible versions of the email address that we could be looking for.

(I don’t know if there is any official name of this method — if someone knows it, please write a comment.)

Shorting method

Hmm.. could this work? Let’s test it. I created the following table and trigger:

Let’s find the email addresses that contain n.pierre:

Wow, that is much better than the previous one! It is more than 100 times faster! Now you can have a beer because you deserve it. 🙂


There are parts that result in many readings as well, but it helps a lot now that we are using a longer pattern:

Using more than six characters gives us a much better selectivity.

Table statistics

In this test, I used 318458 random email addresses, and both methods created 2749000 additional rows.

Size on the disk:

As we expected they will use more space than the original table.


  • Both solutions require an extra table
  • That table contains millions of short rows, and it could use a few gigs of space
  • Requires three triggers (insert, update and delete, which could affect the write performance on the table) or the application has to keep that table up-to-date


  • Finding an email address is going to be much faster and require fewer reads.
  • Users will be much more satisfied.


If there is no built in solution or index in MySQL that can help or solve your problem, do not give up. Many times, with small modifications, you can create your own index table or use other tricks.

In this specific case, if you are willing to sacrifice some additional disk space you can speed up your queries a lot with the right method. Trigram was not the best option, but I can see use cases where it could be better.

This is just one possible solution, there could be an even better one. If you know one, please let me know.

Share this post

Comments (12)

  • Scott Klasing

    With triggers you wont be able to alter the table live using pt-online-schema-change and most alters can not be done ALTER ONLINE due to the need to copy data.

    In super high volume shops the triggers would cause performance issues, and due to the need to alter these tables while business is running we disallow them.

    Even with no triggers, once PTOSC is running , it adds its own triggers and the resultant trigger REPLACE statements also often cause our “super high volume” databases to jam, ie, lock up where threads go through the roof.

    March 19, 2018 at 7:23 pm
  • Jannes

    I’ve been doing exactly this for the last few years to get fast search over many tens of millions of rows, over many fields. Few extra tricks:

    1) You can limit the length of your index to for example 6 characters if you alter your search query like so:

    FROM email AS e
    RIGHT JOIN email_tib AS t ON
    WHERE t.email_parts LIKE “n.pier%” — Note that this patter has been cut to max 6 chars as well
    AND LIKE ‘%n.pierre%’; — Note full user specified pattern here

    So you do the fast match on only a few characters and then double check the original field against the original pattern to get rid of false positives. This cuts back immensely on the size of your index table, without significantly affecting the selectivity (if not too short). Fixed width char fields also save a bit of overhead and are slightly faster.

    2) The above also allows you to get rid of the delete trigger and speed up the update trigger. A few stale rows in the index table are no problem, you can just garbage collect them later.

    3) Suppose you have more fields than just an email address, like firstname, lastname etc. then you can throw all those permutations into the same 6 char index table and use a query like this:

    SELECT, e.firstname, e.lastname
    FROM email AS e
    RIGHT JOIN email_tib AS t ON
    WHERE t.email_parts LIKE “pierre%”
    AND ( LIKE ‘%pierre%’ OR e.firstname LIKE ‘%pierre%’ OR e.lastname LIKE ‘%pierre%’);

    4) Bonus points for avoiding duplicates in the index table pointing to the same record: covering UNIQUE key and use INSERT IGNORE.

    5) Realistically, if the email is “anderson.pierre”, who is ever going to do a search for ‘%n.pierre%’ ? You could limit your index to only contain the first 6 characters after each word boundary. So only do ‘anders’, ‘.pierr’, ‘pierre’. Other domain specific knowledge might lead you to exclude ‘.com’ and ‘.net’ or even whole domains ‘’ from your index. They’d give so many matches anyway that it’s pointless; might as well not include them at all (in the index table).

    6) You could allow the user to enter 2 search patterns, then simply match both from the index (t.email_parts LIKE “pierre%” OR t.email_parts LIKE “exampl%”) and then again match both against all actual fields. Even if you cut out domain names from the index, it will still only return and not

    7) I don’t actually know if it’s faster or not, but there is a way to make one multirow insert in the trigger instead of looping multiple single row inserts.

    Hope this helps someone.

    March 20, 2018 at 6:47 pm
    • Tibor Korocz

      Hi Jannes,

      These are very good comments.
      As you can see in my triggers I am not using the domain part at all because that would give us much more matches.

      I was also thinking to remove the duplicates completely. Every part would be have an ID like anders has id 5 and .pierr has ID 102 etc..

      So a table like this:


      And I I would have an other table , a kind of connection table which would contain all the email address IDs and the these parts IDs, so I could easily select which email address has these parts..

      That table would look like something like this:

      email_id, parts_id

      I did not test this yet.
      But this could save us a lot of space and it would might be faster as well.. I am planning to test it but first I wanted to see if there are any comments or other IDs as well.

      Thank you for your comments.

      March 20, 2018 at 7:06 pm
  • Jannes

    Thanks. I noticed you already did some of the things I mentioned. Just wanted to point them out again, I guess.

    I’d be happy to be proven wrong, but I doubt your extra table is going to help you. I vaguely remember exploring that direction a little, but giving up. Size wise 6 characters is basically the same size as an integer. So all the extra integers referencing each other will only make the total (much) larger and require more random access lookups when joining. I’m even using only 4 in my application, which in my case is still fine for selectivity. In my experience, even if the initial index matches way too many rows, the other AND clause will quickly filter the false positives out.

    In my case I’m also adding partitions and a Priority field, for roughly ordering the results by relevance as well as excluding some partitions in some cases.

    CREATE TABLE textindex (
    ID int(10) unsigned NOT NULL,
    Subtext char(4) NOT NULL COMMENT ‘Note: case insensitive!’,
    Prio tinyint(3) unsigned NOT NULL,
    PRIMARY KEY (Subtext,ID,Prio) # Covering index, so no lookup for actual data required
    ) ENGINE=InnoDB COMMENT=’Contains all text fragments from different fields and tables’
    (PARTITION p05 VALUES IN (5) COMMENT = ‘One letter words’,
    PARTITION p06 VALUES IN (6) COMMENT = ‘Two letter words’,
    PARTITION p07 VALUES IN (7) COMMENT = ‘Recently used items (of all types) (needs a scheduled update)’,
    PARTITION p10 VALUES IN (10) COMMENT = ‘Standard prio’,
    PARTITION p90 VALUES IN (90) COMMENT = ‘Old expired stuff’

    I have something like 8 fields where I pull subtexts from. In my case a subtext always starts at a word boundary in the original string. So ‘bla123bla.test’ becomes: { ‘bla1’, ‘123b’, ‘bla.’, ‘.tes’, ‘test’ }. For wildcard matching I require the user to type at least 3 characters. If he types a 1 or 2 character word, I make sure it’s an exact match in only the corresponding partition (excluding others for speedup). It will depend on your application whether this makes sense for you.

    Another small set of duplicates you can get rid of (at least in my case) is where one Subtext is the prefix of another. If you have ‘bla’ and ‘blah’ you can get rid of the first one. Probably not very effective for a list of only email addresses, but a nice boost when you’re getting texts from various, partially similar fields.

    Another idea I’m playing with which might actually be useful in your case (but harder for me) is making things work even if the user searches for ‘npierre’ (without dot). Besides your normal inserts, similarly also insert all Subtexts of the string with all non alphanumeric characters filtered out. Then use a search query like this:

    SELECT DISTINCT,, e.firstname, e.lastname
    FROM email AS e
    RIGHT JOIN email_tib AS t ON
    WHERE t.email_parts LIKE “npierr%”
    AND ( LIKE ‘%npierre%’ OR e.firstname LIKE ‘%npierre%’ OR e.lastname LIKE ‘%npierre%’
    OR REPLACE(,’.’,”) LIKE ‘%npierre%’ OR REPLACE(e.firstname,’.’,”) LIKE ‘%npierre%’ OR REPLACE(e.lastname,’.’,”) LIKE ‘%npierre%’

    Note that I’m only filtering the dot here, actually filtering all alphanumeric characters efficiently is surprisingly hard in MySQL. Anyway, I found the idea of simply stuffing imaginary data (even typos) into your index table can actually be useful, a bit funny.

    Speaking of hard, here’s how I decide where word boundaries are (letting pos go from 1 to the length of the string) :

    AND (pos = 1
    OR (LOCATE(LOWER(SUBSTRING(i.Description, pos-1, 1)), ‘\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0abcdefghijklmnopqrstuvwxyz012345678901234567890123456 ‘) + 25) DIV 26
    (LOCATE(LOWER(SUBSTRING(i.Description, pos , 1)), ‘\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0abcdefghijklmnopqrstuvwxyz012345678901234567890123456 ‘) + 25) DIV 26

    Quite horrible and unfortunately not very friendly to more exotic languages.

    One final little trick I do, as I have company names in one of the fields, is removing words like ‘Ltd.’ and ‘Inc.’. ‘Incorporated’ and other words that are too common. This is sort of the reverse of the previous trick: removing some useless things from the index. If the user types ‘google inc’ it will find google quickly through the index without wasting time on a million false matches had ‘inc’ been in there. It will still require matches on both ‘%google%’ and ‘%inc%’ in the final pattern.

    One small correction in item 3 of my previous post: that query needs a DISTINCT, as it can actually get multiple matches from the index pointing to the same email row.

    Interesting discussion, thanks.

    March 20, 2018 at 9:08 pm
  • Jannes

    Re-reading your initial trigram query, can you try the following on your dataset?

    FROM email_trigram AS tr
    INNER JOIN email AS e ON
    WHERE tr.trigram = “n.p” — only first 3 chars of search term
    AND LIKE ‘%n.pierr%” — whole search term

    Avoiding GROUP BY has the added benefit of allowing you to use a LIMIT (that fills quickly) and then telling the user to improve her search terms.

    Another fun one I just came up with while typing this:

    FROM email AS e
    INNER JOIN email_trigram AS tr1 ON AND tr1.trigram=”n.p”
    INNER JOIN email_trigram AS tr2 ON AND tr2.trigram=”.pi”
    INNER JOIN email_trigram AS tr3 ON AND tr3.trigram=”pie”
    INNER JOIN email_trigram AS tr4 ON AND tr4.trigram=”ier”
    INNER JOIN email_trigram AS tr5 ON AND tr5.trigram=”err”
    INNER JOIN email_trigram AS tr6 ON AND tr6.trigram=”rre”

    For this to be fast you should probably do:

    ADD UNIQUE KEY idx_trigram_email_id (trigram, email_id)

    (or probably better: drop the id field and make the above the PRIMARY KEY)

    If that works even remotely decently, you might want to consider also trying 4 and 5 character quadgrams (?) and pentagrams (?)

    I haven’t tested that one at all, but I wouldn’t be surprised if that actually has very good performance. I might need to experiment with that for my own data set. Please let me know what you find.

    Further idea: combine the two above queries only doing 2 or 3 INNER JOINs (the first and the last for best selectivity) and filtering the rest like in the first query. (no DISTINCT required if you have the UNIQUE KEY)

    March 20, 2018 at 9:53 pm
  • Masood Alam

    This particular email scenario would make more sense in Redis to me instead of maintaining triggers and/or application work around.

    March 21, 2018 at 9:58 am
  • Alexey Kopytov


    Actually, there is a built-in solution in MySQL to do things like that, and it’s called the ngram plugin:

    It is, in my opinion, one of the least known and most underrated features in MySQL. Probably because the fine manual leaves the impression that ngram is only applicable to the CJK languages. It is not, you should certainly give it a try.

    March 25, 2018 at 3:40 am
    • Tibor Korocz


      I already tested and wrote a blog post about that, it is in the queue, coming soon 😉

      March 26, 2018 at 4:20 am
  • Bill Karwin

    I did a presentation some years ago comparing fulltext search methods. I tried trigrams, but in my results, it was about 100 times slower than using a real FULLTEXT index, and about 1000 times slower than Sphinx Search.

    Trigrams are better than a table-scan of course, but I would not recommend trigrams.

    April 8, 2018 at 11:27 am
    • Tibor Korocz


      Yes, you are right Sphinx and similar solutions are going to be faster than trigrams for sure. No doubts there. 🙂

      This solution can work in scenarios where you do not want to add another layer another complexity to you infrastructure or you have limited resources etc.. Honestly there are many businesses with only 1-2 MySQL servers.. But they still want reasonable response time, in those cases it might can be useful.

      But yes I would not recommended it in every scenario as well, but I still can see some cases where it can work.

      April 9, 2018 at 6:37 am
  • handri


    other than Sphinx search, is there any other search engine solution that support pattern matching search?

    Can elastic search / Solr support this?

    Thank you.

    December 15, 2018 at 1:47 am

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.