
- 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 to optimize many SELECTs in a single table in MySQL?
To optimize many SELECTs, use it once and apply IN() to fetch multiple values. Let us first create a table −
mysql> create table DemoTable ( Id int, Name varchar(100), Age int ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1,'Chris',23); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values(2,'David',21); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable values(3,'Mike',24); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(4,'Robert',22); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable ;
This will produce the following output −
+------+--------+------+ | Id | Name | Age | +------+--------+------+ | 1 | Chris | 23 | | 2 | David | 21 | | 3 | Mike | 24 | | 4 | Robert | 22 | +------+--------+------+ 4 rows in set (0.00 sec)
Following is the query to optimize many SELECTs on a single table −
mysql> select Name from DemoTable where Age in(21,22,23);
This will produce the following output −
+--------+ | Name | +--------+ | Chris | | David | | Robert | +--------+ 3 rows in set (0.00 sec)
- Related Articles
- How to optimize a MySQL table after deleting some rows?
- How to take backup of a single table in a MySQL database?
- How to fetch only a single result from a table in Java-MySQL?
- Check how many rows are in a MySQL database table?
- Best way to update a single column in a MySQL table?
- How to check if any value is Null in a MySQL table single row?
- How to insert an array of values in a MySQL table with a single INSERT?
- How to create many Javascript variables in a single statement?
- Difference between two selects in MySQL?
- Delete records from a MySQL table with IN() in a single query
- How to insert only a single column into a MySQL table with Java?
- How to delete a single value from a MySQL table with duplicate records?
- How can we delete a single row from a MySQL table?
- What is the use of OPTIMIZE TABLE statement in maintaining the MySQL tables?
- Add a single year to all the date records in a MySQL table column

Advertisements