MYSQL在select语句中的哪个字段呢?

时间:2011-07-21 18:14:57

标签: mysql database

我想从id = 4的数据库表中返回字段name。哪个查询更容易让MYSQL处理。

SELECT name, id FROM table WHERE id = 4

SELECT name FROM table WHERE id = 4

我想我问的是,将WHERE语句中的字段放在SELECT中是否也是好的做法,即使我不会在php中返回它。

6 个答案:

答案 0 :(得分:3)

SELECT name, id FROM table WHERE id = 4

在mysql的结果中也会有id,它会对数据产生很小的影响,因为它只是一个数字,但如果你这样做,你选择其他的东西,如SELECT name, id FROM table WHERE name = "ron"会绝对是一种不好的做法,因为更多的数据正在从数据库服务器传输,因为您已经知道名称“ron”,因为它不是唯一的,它可能有多行。

mysql> explain select id,name from admin_users where id = 1;
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql> explain select name from admin_users where id = 1;
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

相比
mysql> explain select id,name from admin_users where name = 'ron';
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
123 row in set (0.11 sec)

mysql> explain select name from admin_users where name = 'ron';
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
123 row in set (0.05 sec)

答案 1 :(得分:1)

如果您不需要返回的ID,请不要包含它。如果您需要,请加入它。

答案 2 :(得分:1)

不,这不是好习惯。 SELECT正是您需要的那些领域。

答案 3 :(得分:1)

我怀疑第二种选择是在大型数据库上更快(许多记录> 1M),因为从数据库中取出数据所需的时间更少。


这是在一个小桌子上完成的分析(1187条记录),id是主键:

mysql> select name,id from websites where id = 1;
+------+----+
| name | id |
+------+----+
| Shef |  1 |
+------+----+
1 row in set (0.00 sec)

mysql> select name from websites where id = 1;
+------+
| name |
+------+
| Shef |
+------+
1 row in set (0.00 sec)

对于第一个查询:

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000045 |
| checking permissions | 0.000006 |
| Opening tables       | 0.000016 |
| System lock          | 0.000007 |
| init                 | 0.000016 |
| optimizing           | 0.000006 |
| statistics           | 0.000063 |
| preparing            | 0.000008 |
| executing            | 0.000004 |
| Sending data         | 0.000009 |
| end                  | 0.000003 |
| query end            | 0.000002 |
| closing tables       | 0.000005 |
| freeing items        | 0.000046 |
| logging slow query   | 0.000002 |
| cleaning up          | 0.000002 |
+----------------------+----------+

对于第二个查询:

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000050 |
| checking permissions | 0.000006 |
| Opening tables       | 0.000015 |
| System lock          | 0.000008 |
| init                 | 0.000017 |
| optimizing           | 0.000008 |
| statistics           | 0.000044 |
| preparing            | 0.000008 |
| executing            | 0.000002 |
| Sending data         | 0.000009 |
| end                  | 0.000003 |
| query end            | 0.000002 |
| closing tables       | 0.000005 |
| freeing items        | 0.000046 |
| logging slow query   | 0.000002 |
| cleaning up          | 0.000002 |
+----------------------+----------+

答案 4 :(得分:1)

  

我想我要问的是,把这个领域放在哪里是好的做法   SELECT中的WHERE语句,即使我不会在php中返回它。

不。这将增加网络开销,因为数据在客户端服务器之间传输。如果您不打算使用这些数据,请不要选择它。

答案 5 :(得分:1)

除非您在结果中需要“id”列,否则没有理由要求它。 MySQL将对查询进行相同的处理。要求它可能会减慢速度,因为MySQL必须向您返回额外的数据,并且您的程序需要解析它。怀疑它对于'id'列很重要,但如果它可能是几个巨大的文本字段。

以下是MySQL对我的一个简单表上的两个查询的看法(id字段上有一个索引)。

mysql> explain select id,name from admin_users where id = 1;
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql> explain select name from admin_users where id = 1;
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | admin_users | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)