重命名列对内部选择更有效吗?

时间:2015-11-17 10:32:46

标签: sql sql-server

重命名列让我变得更有效率并避免冲突或以下 SQL 语句之间没有区别:

请记住,我在外部选择和内部选择上使用相同的字段名称。

A - 内部选择重命名列:

delete from aspnet_UsersInRoles
 where
  UserId=(select u.UserId from aspnet_Users u where  u.UserName='Me')
  and 
  RoleId in (select r.RoleId from aspnet_Roles r where r.RoleName in(select name from DSC_Role where isCustomer=0) )

B - 内部选择时没有重命名列:

delete from aspnet_UsersInRoles
 where
  UserId=(select UserId from aspnet_Users where UserName='Me')
  and 
  RoleId in (select RoleId from aspnet_Roles where RoleName in(select name from DSC_Role where isCustomer=0) )

哪一个最好?

2 个答案:

答案 0 :(得分:4)

查询的bith版本的查询计划之间应该没有区别。你不应该期望一个人比另一个人更有效率或更快。

对表格进行别名化只会有助于消除歧义并使您的查询更易于阅读。它不应该影响底层DBMS中的实际执行计划。

答案 1 :(得分:1)

您没有重命名列,而是在引用 SELECT 列表中的列时使用表别名

在性能方面没有区别。比较解释计划(下面的演示是针对 Oracle ):

使用表别名

SQL> EXPLAIN PLAN FOR
  2  SELECT empno FROM emp;

Explained.

SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |    56 |     4   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| EMP  |    14 |    56 |     4   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

没有表别名

SQL> EXPLAIN PLAN FOR
  2  SELECT e.empno FROM emp e;

Explained.

SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |    56 |     4   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| EMP  |    14 |    56 |     4   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

您还可以使用 SQL * Plus 中的set autot on来比较统计信息

使用别名

Statistics
----------------------------------------------------------
          8  recursive calls
          0  db block gets
         10  consistent gets
          0  physical reads
          0  redo size
        702  bytes sent via SQL*Net to client
        544  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed

没有别名

Statistics
----------------------------------------------------------
          8  recursive calls
          0  db block gets
         10  consistent gets
          0  physical reads
          0  redo size
        702  bytes sent via SQL*Net to client
        544  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed

完全没有区别。