MySQL is more efficient than a like statement locate position InStr Find_in_set

Source: Internet
Author: User

If you have been looking for a more efficient method than the MySQL like statement, let me introduce you to a few.

Like statement
SELECT ' column ' from ' table ' where ' condition ' like '%keyword% '

In fact, you can useLocate (position) and InStrThese two functions take the place of

One, locate statements
SELECT ' column ' from ' Table ' where locate (' keyword ', ' condition ') >0

second, or locate's name position
Position statements
SELECT ' column ' from ' table ' where position (' keyword ' in ' condition ')

Third, InStr statement

SELECT ' column ' from ' Table ' where InStr (' condition ', ' keyword ') >0

The difference between locate, position, and InStr is just the same as the parameter location, and the parameter of locate more than one starting position.
mysql> SELECT LOCATE (' Bar ', ' Foobarbar ', 5);

7

The speed of these three is a little bit faster than using like.



~~~~~~~~~~~~~~~~~~~~~~~ Gorgeous split-line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Four, we also want to introduce a new member, that is Find_in_set

 find_in_set (STR1,STR2) function: Returns the position index where str2 is in str1, where str2 must be split with ",". 

Table:
mysql> SELECT * from region;
+----+-------------+
| id | name        |
+----+-------------+
| 1  | name1,nam2  |
| 2  | name1       |
| 3  | Name3       |
| 4  | Name2,name4 |
| 5  | Name3,name5 |
+----+-------------+
5 rows in Set (0.00 sec)

Find_in_set statement
Mysql> SELECT * FROM Test where find_in_set (' name1 ', name);
+----+------------+
| id | name       |
+----+------------+
| 1  | name1,nam2 |
| 2  | name1      |
+----+------------+
2 rows in Set (0.02 sec)

v. Of course, there is also a full-text index of MySQL

Full-text index: http://dev.mysql.com/doc/ refman/5.6/en/fulltext-search.html

MySQL is more efficient than a like statement locate position InStr Find_in_set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.