简体   繁体   English

在查询查询中使用通配符运算符

[英]Using a wildcard operator in a query of query

I have the following code: 我有以下代码:

<cfquery name="somequery1" datasource="somedsn">
    SELECT somecolumn1, somecolumn2, somecolumn3 
    FROM sometable 
    WHERE someid = <cfqueryparam cfsqltype="cf_sql_integer" value="1">
</cfquery>

<cfquery name="somequery2" dbtype="query">
    SELECT *
    FROM somequery1
</cfquery>

My code manager says I need to change the Query of Query to: 我的代码经理说,我需要将“查询查询”更改为:

<cfquery name="somequery2" dbtype="query">
    SELECT somecolumn1, somecolumn2, somecolumn3 
    FROM somequery1
</cfquery>

Can someone explain why I would need to redefine the column references in the Query of Query? 有人可以解释为什么我需要在查询查询中重新定义列引用吗? Surely, the wildcard operator takes care of this. 当然,通配符运算符会照顾到这一点。

Is there any technical or performance gain to redefining the column references in the SELECT clause of a Coldfusion Query of Queries? 重新定义查询的Coldfusion查询的SELECT子句中的列引用是否有任何技术或性能上的好处? This assumes that the column references have already been explicitly set in the database query that is supplied to the Query of Queries. 假定已在提供给查询查询的数据库查询中明确设置了列引用。

I believe the use of the wildcard operator makes the code cleaner and easier to update, because any changes to the column references only need to be done once. 我相信使用通配符运算符可以使代码更整洁,更容易更新,因为对列引用的任何更改仅需要执行一次。

As you've discussed with Rahul: your "code manager" is offering good advice if this was a DB-based query, but I think it's a bit egregious in the context of a CFML query-on-query. 正如您与Rahul讨论的那样:如果这是基于DB的查询,则“代码管理器”会提供很好的建议,但是我认为在CFML逐个查询的上下文中这有点过分。

I suspect they have heard the guidance in the context of DB queries, and have not really thought it through sufficiently when giving guidance on in-memory query operations. 我怀疑他们在数据库查询的上下文中听到了指导,并且在提供有关内存中查询操作的指导时并没有真正考虑透彻。

In short: your code is more optimal as it stands than the change's they're advising. 简而言之:您的代码比建议的更改更理想。

EDIT: 编辑:

As discussed, yes it is correct that your current code will be more modular considering the fact that it would incorporate any changes(for example if you need to make the changes in the selected columns) in your query ie., it will take care of any columns which you might add in future. 如前所述,是的,考虑到它将在查询中合并任何更改(例如,如果您需要在选定的列中进行更改)的事实,即当前代码将更加模块化是正确的,即,它将负责您将来可能添加的任何列。 So your present query is efficient and good to proceed with. 因此,您当前的查询是有效的,并且可以继续进行。


The wildcard character surely takes care of it if you want to select all the column, however it is nowadays not recommended and usually not preferred to use wildcard character when selecting the columns. 如果要选择所有列,通配符肯定会处理,但是现在不建议这样做,通常不建议在选择列时使用通配符。 You can have a look at Aaron Bertrand Bad habits to kick : using SELECT * / omitting the column list : 您可以看看Aaron Bertrand的不良习惯:使用SELECT * /忽略列列表

But there are several reasons why you should avoid SELECT * in production code: 但是,出于以下原因,应在生产代码中避免使用SELECT *:

  1. You can be returning unnecessary data that will just be ignored, since you don't usually need every single column. 您可能会返回不必要的数据,这些数据将被忽略,因为您通常不需要每一列。 This is wasteful in I/O, since you will be reading all of that data off of the pages, when perhaps you only needed to read the data from the index pages. 这在I / O中非常浪费,因为当您可能只需要从索引页中读取数据时,您将从页面中读取所有数据。 It is also wasteful in network traffic and in many cases the memory required by the consuming application to hold the results. 这也浪费了网络流量,在许多情况下,消耗应用程序保存结果所需的内存也很浪费。
  2. When you use SELECT * in a join, you can introduce complications when multiple tables have columns with the same name (not only on the joined columns, such as OrderID, which are typically the same, but also peripheral columns like CreatedDate or Status). 当在联接中使用SELECT *时,如果多个表具有相同名称的列(不仅在联接的列(例如,OrderID,通常是相同的,而且在外围列,例如CreatedDate或Status)上)具有相同的名称,则可能带来麻烦。 On a straight query this might be okay, but when you try to order by one of these columns, or use the query in a CTE or derived table, you will need to make adjustments. 在直接查询中,这可能没问题,但是当您尝试按这些列之一进行排序,或者在CTE或派生表中使用查询时,您将需要进行调整。
  3. While applications should not be relying on ordinal position of columns in the resultset, using SELECT * will ensure that when you add columns or change column order in the table, the shape of the resultset should change. 虽然应用程序不应依赖于列的顺序位置在结果集中,使用SELECT *将确保当您添加列或更改表中的列的顺序,结果集的形状应该改变。 Ideally, this should only happen intentionally. 理想情况下,这仅应有意发生。

Here is another approach. 这是另一种方法。

<cfset selectFields = "somecolumn1, somecolumn2, somecolumn3">

<cfquery name="somequery1" datasource="somedsn">
select #selectFields#
etc
</cfquery>

<cfquery name="somequery2" dbtype="query">
select #selectFields#
from somequery1
</cfquery>

You get to use your time wisely and your code manager might actually like it. 您可以明智地利用时间,而代码管理器实际上可能会喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM