SQL 查询 - Select * from view 或 Select col1, col2, ... colN from view [英] SQL query - Select * from view or Select col1, col2, ... colN from view

查看:38
本文介绍了SQL 查询 - Select * from view 或 Select col1, col2, ... colN from view的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是 SQL Server 2005,但这个问题适用于任何RDBMS.>

从视图中选择所有列时,以下哪个更有效?

从视图中选择*

从视图中选择 col1, col2, ..., colN

解决方案

NEVER, EVER USE "SELECT *"!!!!

这是查询设计的基本规则!

这有多种原因.其中之一是,如果您的表上只有三个字段并且您在调用查询的代码中使用了所有三个字段,那么随着应用程序的增长,您很可能会向该表添加更多字段,如果您的 select * 查询只是为了返回调用代码的那 3 个字段,然后您从数据库中提取的数据比您需要的要多得多.

另一个原因是性能.在查询设计中,不要像这句口头禅那样考虑可重用性:

吃多少就吃多少,但吃多少就吃多少.

We are using SQL Server 2005, but this question can be for any RDBMS.

Which of the following is more efficient, when selecting all columns from a view?

Select * from view 

or

Select col1, col2, ..., colN from view

解决方案

NEVER, EVER USE "SELECT *"!!!!

This is the cardinal rule of query design!

There are multiple reasons for this. One of which is, that if your table only has three fields on it and you use all three fields in the code that calls the query, there's a great possibility that you will be adding more fields to that table as the application grows, and if your select * query was only meant to return those 3 fields for the calling code, then you're pulling much more data from the database than you need.

Another reason is performance. In query design, don't think about reusability as much as this mantra:

TAKE ALL YOU CAN EAT, BUT EAT ALL YOU TAKE.

这篇关于SQL 查询 - Select * from view 或 Select col1, col2, ... colN from view的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆