MySQL 中 SELECT * 是否比列出 40 个列要快?
SELECT * 比列出 40 个列慢,使用 SELECT 查询时最好列出列名。我们来看一个简单的示例并创建一个表 -
mysql> create table DemoTable( Id int, Name varchar(20), Age int, ZipCode varchar(20), CountryName varchar(20) ); Query OK, 0 rows affected (0.82 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable values(101,'Chris',23,'0909332','US'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable values(102,'Bob',24,'8747443','AUS'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable values(103,'Adam',21,'9988833','UK'); Query OK, 1 row affected (0.11 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将生成以下输出 -
+------+-------+------+---------+-------------+ | Id | Name | Age | ZipCode | CountryName | +------+-------+------+---------+-------------+ | 101 | Chris | 23 | 0909332 | US | | 102 | Bob | 24 | 8747443 | AUS | | 103 | Adam | 21 | 9988833 | UK | +------+-------+------+---------+-------------+ 3 rows in set (0.00 sec)
现在,我们将在使用 MySQL SELECT 时列出每个列名,因为这被认为是一种更快的做法 -
mysql> select Id,Name,Age,ZipCode,CountryName from DemoTable;
这将生成以下输出 -
+------+-------+------+---------+-------------+ | Id | Name | Age | ZipCode | CountryName | +------+-------+------+---------+-------------+ | 101 | Chris | 23 | 0909332 | US | | 102 | Bob | 24 | 8747443 | AUS | | 103 | Adam | 21 | 9988833 | UK | +------+-------+------+---------+-------------+ 3 rows in set (0.00 sec)
广告