
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I enhance my select query to make it faster in MySQL?
For quicker querying, use MySQL IN() because it uses indexing internally. Let us first create a table −
mysql> create table DemoTable1618 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20), -> ClientEmailId varchar(30) -> ); Query OK, 0 rows affected (1.53 sec)
Insert some records in the table using insert command:
mysql> insert into DemoTable1618(ClientName,ClientEmailId) values('Chris Brown','Brown323@gmail.com'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1618(ClientName,ClientEmailId) values('David Miller','MillerDavid@gmail.com'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1618(ClientName,ClientEmailId) values('John Doe','998John_Doe@gmail.com'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable1618(ClientName,ClientEmailId) values('John Smith','999John_Smith@gmail.com'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1618(ClientName,ClientEmailId) values('Adam Smith','Adam_Smith@gmail.com'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1618;
This will produce the following output −
+----------+--------------+-------------------------+ | ClientId | ClientName | ClientEmailId | +----------+--------------+-------------------------+ | 1 | Chris Brown | Brown323@gmail.com | | 2 | David Miller | MillerDavid@gmail.com | | 3 | John Doe | 998John_Doe@gmail.com | | 4 | John Smith | 999John_Smith@gmail.com | | 5 | Adam Smith | Adam_Smith@gmail.com | +----------+--------------+-------------------------+ 5 rows in set (0.00 sec)
Here is the query to use IN() for faster querying −
mysql> select * from DemoTable1618 where ClientEmailId IN('998John_Doe@gmail.com','999John_Smith@gmail.com','MillerDavid@gmail.com');
This will produce the following output −
+----------+--------------+-------------------------+ | ClientId | ClientName | ClientEmailId | +----------+--------------+-------------------------+ | 2 | David Miller | MillerDavid@gmail.com | | 3 | John Doe | 998John_Doe@gmail.com | | 4 | John Smith | 999John_Smith@gmail.com | +----------+--------------+-------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How can I enhance my skin tone to look beautiful?
- How can I make my custom objects Parcelable?
- How can I make my custom objects Serializable?
- How can I make my layout scroll vertically?
- How can I organize my Python code to make it easier to change the base class?
- How can I make my layout scroll vertically in Android?
- Indexing large text field to make query faster in MongoDB
- Can I write my own MySQL functions to use in MySQL queries?
- How can I make the balance between my best friend and lover?
- How can I make Matplotlib.pyplot stop forcing the style of my markers?
- How can I replace & with an ampersand in my MySQL database?
- Does it make sense to use “LIMIT 1” in a query “SELECT 1 …”?
- How to can I get the names of my MySQL table columns?
- Is it true that if I dye my hair they will grow white even faster?
- How to do recursive SELECT query in MySQL?

Advertisements