代码之家  ›  专栏  ›  技术社区  ›  naumcho

每行调用1=2吗?

  •  5
  • naumcho  · 技术社区  · 15 年前

    select orderId
    into #sometmptbl
    from orders
    where 1=2
    

    然而,当你在这上面做一个展示计划的时候,它实际上似乎要进入表/索引:

    语句1的查询计划(第行) 1).

    STEP 1
        The type of query is CREATE TABLE.
    
    STEP 2
        The type of query is INSERT.
        The update mode is direct.
    
        FROM TABLE
            orders
        Nested iteration.
        Index : orders_idx1
        Forward scan.
        Positioning at index start.
        Index contains all needed columns. Base table will not be read.
        Using I/O Size 2 Kbytes for index leaf pages.
        With LRU Buffer Replacement Strategy for index leaf pages.
        TO TABLE
            #sometmptbl
        Using I/O Size 2 Kbytes for data pages.
    

    报表的总估计I/O成本 1(第1行):632082。

    这是否意味着索引中的每个条目都要计算1=2?有没有办法在固定的时间内做到这一点?

    更新 :

    以下是执行后的实际I/O开销,因此看起来实际读取数确实为0,因此不会影响性能:

    Table: orders scan count 0, logical reads: (regular=0 apf=0 total=0), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
    Table: #sometmptbl_____00002860018595346 scan count 0, logical reads: (regular=1 apf=0 total=1), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
    Total actual I/O cost for this command: 2.
    Total writes for this command: 3
    0 row(s) affected.
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   Rawheiser    15 年前

    如果将statistics io设置为on,则应该看到零逻辑和物理读取。 它可能会创建一个扫描索引的计划,但似乎并没有实际使用它。

    我建议不要在大批量生产环境中以这种方式创建临时表。有系统表锁定问题,以及轻微的性能影响(您的里程数可能会有所不同)(同时,列的identity属性也被带入temp表中)。

    如果它是varchar,那么就没有任何理由不能标准化max值上的列长度,并在任何地方使用它。

        2
  •  0
  •   gbn    15 年前

    select orderId from orders where 1=2 给你什么?

    它可能选择了索引来读取数据类型等,而一个非常琐碎的查询(没有INTO)应该在根本没有表/索引访问的情况下进行优化。