Quickly search for a string in MySQL database?


Use FULLTEXT search to quickly search for a string. Let us first create a table −

mysql> create table DemoTable1554
   -> (
   -> Title text
   -> );
Query OK, 0 rows affected (0.63 sec)

Here is the query to create full text search −

mysql> create fulltext index faster_title on DemoTable1554(Title);
Query OK, 0 rows affected, 1 warning (7.09 sec)
Records: 0  Duplicates: 0  Warnings: 1

Insert some records in the table using insert command −

mysql> insert into DemoTable1554 values('John is working on MySQL database');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable1554 values('Adam Smith is working on Java language');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1554 values('John smith is working on Python Language');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1554;

This will produce the following output −

+------------------------------------------+
| Title                                    |
+------------------------------------------+
| John is working on MySQL database        |
| Adam Smith is working on Java language   |
| John smith is working on Python Language |
+------------------------------------------+
3 rows in set (0.00 sec)

Following is the query to quickly search for a string in a MySQL database. Here, we are searching for string “language” −

mysql> select * from DemoTable1554 where match(Title) against('language' in boolean mode);

This will produce the following output −

+------------------------------------------+
| Title                                    |
+------------------------------------------+
| Adam Smith is working on Java language   |
| John smith is working on Python Language |
+------------------------------------------+
2 rows in set (0.00 sec)

Updated on: 12-Dec-2019

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements