SQL Server 通配符 [英] SQL Server wildcard

查看:26
本文介绍了SQL Server 通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Select * from

在动态 T-SQL 语句中使用与在存储过程中使用通配符解释时是否具有相同的性能影响,假设存储的过程是编译后的代码单元吗?

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?

推荐答案

要回答你的问题,需要拆开一点:

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

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

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

SELECT * 有 3 种主要类型的缺点:

SELECT * has 3 main types of disadvantages:

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

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

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

存储过程并不是真正的代码编译单元":存储过程的查询计划通常会在第一次运行后被缓存,但在许多/大多数情况下,ad-hoc SQL 语句实际上也是如此情况.

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.

现在,要实际回答您的问题:,在临时 SQL 中使用 SELECT * 的任何缺点也同样适用于存储过程中的 SQL.

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.

这篇关于SQL Server 通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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