Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL String search and where clause condition order
    primarykey
    data
    text
    <p>I'm working with large number of rows in db(MySQL, innoDb engine, approx 20 milion rows) and I need to perform fuzzy like searching quite a lot. For some reasons I decided to use jaro_winkler algorithm and for performance issues I implemented it as a function in SQL. Application is written in Python and there is a weird situation I came across today:</p> <p>Comparing these two queries (called from mysql shell not via Orm or etc):</p> <pre><code>SELECT * FROM products WHERE jaro_winkler(code, '78-1747') &gt; 0.7 AND code LIKE '%78%'; </code></pre> <p>and</p> <pre><code>SELECT * FROM products WHERE code LIKE '%78%' AND jaro_winkler(code, '78-1747') &gt; 0.7; </code></pre> <p>I noticed that first one is at least 10 times slower than the second one. It seems logical at first, but as I checked the order of conditions in WHERE should not matter.</p> <p>So my question - is it the normal behaviour ?</p> <p>And has someone (from practical experience) can recommend a best algorithm or function to perform fuzzy searching ? I know about damerau-levenshtein metric, but it turns to be slower than my current solution.</p> <p>EDIT: After using explain:</p> <p>I created sample database really quickly and used both queries:</p> <p>for the first query:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE products ALL NULL NULL NULL NULL 4166 Using where </code></pre> <p>query time: ~ 2 seconds</p> <p>explain for the second query:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE products ALL NULL NULL NULL NULL 4332 Using where </code></pre> <p>query time: ~ 0.1 second</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload