SQL Server 通配符

发布于 2024-11-29 01:21:26 字数 23 浏览 0 评论 0原文

考虑到存储过程是编译的代码单元?

Does Select * from <table/view> have the same performance implication regarding wildcard interpretation when used in a dynamic T-SQL statement as against using it in a stored procedure, given that the stored procedure is a compiled unit of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-12-06 01:21:26

要回答你的问题,需要稍微拆开一下:

Select * from 是否具有相同的性能含义
关于通配符解释

SELECT * 有 3 种主要缺点:

  • 代码维护:使用 SELECT * 会降低复杂表/查询的易读性,并且当客户端应用程序期望从查询中得到特定结果时,可能会导致问题,但是表发生变化
  • 网络性能:将结果返回给客户端应用程序时使用 SELECT * 意味着所有列都将返回给客户端;如果客户端仅使用其中的某些列,则会浪费带宽并且应用程序运行速度会比其应有的速度慢。
  • 索引/查询计划性能:在某些情况下,如果您的查询实际上只需要返回参与索引的列,但您将它们全部返回,那么您可能会得到引擎创建的更糟糕的查询计划。

我不确定你所说的“关于通配符解释的含义”是什么意思,但我怀疑你误解了为什么 SELECT * 是一个坏主意 - SQL 引擎无论如何都会验证提供的列; “扩展”通配符的成本本质上是0。

假设存储过程是代码的编译单元

存储过程实际上并不是“代码的编译单元”:存储过程的查询计划通常会在首次运行后被缓存,但实际上也是如此在许多/大多数情况下也可以使用 hoc SQL 语句。

现在,真正回答您的问题:,在即席 SQL 中使用 SELECT * 的任何缺点也同样适用于存储过程内的 SQL。

To answer your question, it needs to be picked apart a little:

Does Select * from have the same performance implication
regarding wildcard interpretation

SELECT * has 3 main types of disadvantages:

  • Code maintenance: using SELECT * reduces legibility for complex tables/queries, and can cause problems when a client application expects a certain result from a query, but the table changes
  • Network performance: using SELECT * when returning results to a client application means that all columns will be returned to the client; if only some of those columns are used by the client, then bandwidth is wasted and the application runs slower than it could.
  • Indexing / Query plan performance: Under some circumstances, if you have a query that really only needs to return the columns that participate in an index, but you return them all instead, then you could get much worse query plans created by the engine.

I'm not sure what you mean by "implication regarding wildcard interpretation", but I suspect you're misunderstanding why SELECT * is a bad idea - the SQL engine validates provided columns anyway; the cost of "expanding" the wildcard is essentially 0.

given that the stored procedure is a compiled unit of code

A stored procedure is not really a "compiled unit of code": the query plan for a stored procedure will usually be cached after it first runs, but the same is actually true of ad-hoc SQL statements also in many/most circumstances.

Now, to actually answer your question: Yes, any disadvantages of using SELECT * in ad-hoc SQL also apply, equally, to SQL inside of a stored procedure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文