如何增强我的选择查询以使其在 MySQL 中运行得更快?

mysqlmysqli database

为了更快地进行查询,请使用 MySQL IN(),因为它在内部使用索引。让我们首先创建一个表 −

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 在表中插入一些记录 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)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1618;

这将产生以下输出 −

+----------+--------------+-------------------------+
| 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)

以下是使用 IN() 进行更快查询的查询 −

mysql> select * from DemoTable1618 where ClientEmailId IN('998John_Doe@gmail.com','999John_Smith@gmail.com','MillerDavid@gmail.com');

这将产生以下输出 −

+----------+--------------+-------------------------+
| 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)

相关文章