Sql通配符:性能开销?

发布于 2024-08-14 04:06:03 字数 275 浏览 12 评论 0原文

我在谷歌上搜索了这个问题,似乎找不到一致的观点,或者许多基于可靠数据的观点。我只是想知道在 SQL SELECT 语句中使用通配符是否会比单独调用每个项目产生额外的开销。我在几个不同的测试查询中比较了两者的执行计划,看起来估计值总是相同的。是否有可能在其他地方产生了一些开销,或者它们的处理方式确实相同?

我具体指的是:

SELECT *

vs。

SELECT item1, item2, etc.

I've Googled this question and can't seem to find a consistent opinion, or many opinions that are based on solid data. I simply would like to know if using the wildcard in a SQL SELECT statement incurs additional overhead than calling each item out individually. I have compared the execution plans of both in several different test queries, and it seems that the estimates always read the same. Is it possible that some overhead is incurred elsewhere, or are they truly handled identically?

What I am referring to specifically:

SELECT *

vs.

SELECT item1, item2, etc.

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

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

发布评论

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

评论(5

仅一夜美梦 2024-08-21 04:06:03
SELECT * FROM...

并将

SELECT every, column, list, ... FROM...

执行相同的操作,因为两者都是未优化的扫描。

不同之处在于:

  • 在 sys.columns 中进行额外查找以解决 *
  • 当表架构更改时合同/签名更改
  • 无法创建覆盖索引。事实上,根本没有调整选项,
  • 确实必须刷新所需的视图
  • 如果非 schemabound无法使用 * 索引或 schemabind 视图,则
  • ...以及其他内容

关于同一主题的其他 SO 问题...

SELECT * FROM...

and

SELECT every, column, list, ... FROM...

will perform the same because both are an unoptimised scan

The difference is:

  • the extra lookup in sys.columns to resolve *
  • the contract/signature change when the table schema changes
  • inability to create a covering index. In fact, no tuning options at all, really
  • have to refresh views needed if non schemabound
  • can not index or schemabind a view using *
  • ...and other stuff

Other SO questions on the same subject...

水波映月 2024-08-21 04:06:03

您的意思是 select * from ... 而不是 select col1, col2, col3 from ... 吗?

我认为命名列并检索最少量的信息总是更好,因为

  • 您的代码将独立于数据库中列的物理顺序工作。列顺序不应影响您的应用程序,但如果您使用 * 则会出现这种情况。在数据库迁移等情况下,这可能很危险。
  • 如果您命名列,DBMS 可以进一步优化执行。例如,如果有一个索引包含您感兴趣的所有数据,则根本不会访问该表。

如果您对“通配符”有其他意思,请忽略我的答案......

Do you mean select * from ... instead of select col1, col2, col3 from ...?

I think it's always better to name the column and retrieve the minimal amount of information, because

  • your code will work independently of the physical order of the columns in the db. The column order should not impact your application, but it will be the case if you use *. It can be dangerous in case of db migration, etc.
  • if you name the columns, the DBMS can optimize further the execution. For instance, if there is an index that contains all the data your are interested in, the table will not be accessed at all.

If you mean something else with "wildcard", just ignore my answer...

翻了热茶 2024-08-21 04:06:03

编辑:如果您正在谈论 Select * From ... 中的星号通配符,请参阅其他响应...

如果您正在谈论谓词子句中的通配符或使用 Like 运算符的其他查询表达式, (_ , % ) 如下所述,那么:

这与使用通配符是否影响 SQL 是否为“SARG-ABLE”有关。 SARGABLE(Search-ARGument-able)表示查询的搜索或排序参数是否可以用作现有索引的条目参数。如果将通配符添加到参数的开头

 Where Name Like '%ing'

,则无法遍历名称字段上的索引来查找以“ing”结尾的节点。

如果您将通配符附加到末尾,

Where Name like 'Donald%' 

那么优化器仍然可以在名称列上使用索引,并且查询仍然是 SARG 可用的

EDIT: If you are talking about the asterisk wild card as in Select * From ... then see other responses...

If you are talking about wildcards in predicate clauses, or other query expressions using Like operator, (_ , % ) as described below, then:

This has to do with whether using the Wildcard affects whether the SQL is "SARG-ABLE" or not. SARGABLE, (Search-ARGument-able)means whether or not the query's search or sort arguments can be used as entry parameters to an existing index. If you prepend the wild card to the beginning of an argument

 Where Name Like '%ing'

Then there is no way to traverse an index on the name field to find the nodes that end in 'ing'.

If otoh you append the wildcard to the end,

Where Name like 'Donald%' 

then the optimizer can still use an index on the name column, and the query is still SARG-able

困倦 2024-08-21 04:06:03

如果你所说的SQL野车是*。它本身并不意味着性能开销。但是,如果表扩展,您可能会发现自己检索了未搜索的字段。
一般来说,不具体指定您搜索或插入的字段是一个坏习惯。
考虑

insert into mytable values(1,2)

如果表扩展到三个字段会发生什么?

If that you call SQL wild car is *. It does not imply performance overhead by it self. However, if the table is extended you could find yourself retrieving fields you doesn't search.
In general not being specific in the fields you search or insert is a bad habit.
Consider

insert into mytable values(1,2)

What happen if the table is extended to three fields?

我的鱼塘能养鲲 2024-08-21 04:06:03

从执行计划的角度来看,这可能并不是更多的工作。但是,如果您要获取实际上并不需要的列,则数据库和应用程序之间会使用额外的网络带宽。此外,如果您使用高级客户端 API 对返回的数据执行一些工作(例如,Perl 的 selectall_hashref),那么这些额外的列将给客户端带来性能成本。多少?视情况而定。

It may not be more work from an execution plan standpoint. But if you're fetching columns you don't actually need, that's additional network bandwidth being used between the database and your application. Also if you're using a high-level client API that performs some work on the returned data (for example, Perl's selectall_hashref) then those extra columns will impose performance cost on the client side. How much? Depends.

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