Which is faster — INSTR or LIKE?

前端 未结 4 1873
广开言路
广开言路 2020-11-29 23:04

If your goal is to test if a string exists in a MySQL column (of type \'varchar\', \'text\', \'blob\', etc) which of the following is faster / more efficient / better to use

4条回答
  •  时光说笑
    2020-11-29 23:48

    There is little to add to razzed's test. But apparently using regexp does incur a much heavier processing load, unlike what Seth points out in his comment.

    The following tests assume that you set query_caching to On in my.ini

    query_cache_type = 1
    query_cache_size = 64M
    

    Tests

    • The timings show the average performance, out of three measurements (with the cache cleared intermittently):

    • LIKE

      SELECT * FROM `domain_model_offers` WHERE `description` LIKE '%inform%' LIMIT 0 , 30
      

      Initial: 0.0035s
      Cached: 0.0005s

    • REGEXP

      SELECT * FROM `domain_model_offers` WHERE `description` REGEXP 'inform' LIMIT 0 , 30
      

      Initial: 0.01s
      Cached: 0.0004s

    Result

    LIKE or INSTR is definitely faster than REGEXP.

    Though minimal, the cache timing difference is probably sufficient to warrant further investigation.

    On a probably configured MySQL system, fulltext indexing should generally be always faster or at least on par with a nonindexed search. So use indexing, especially on long human language texts, regardless of intermittent markup code.

提交回复
热议问题