【问题标题】:union all in TDengine在 TDengine 中联合所有
【发布时间】:2021-10-25 10:18:17
【问题描述】:

最近在TDengine中尝试union all,发现有问题:

taos> select count(*) as count, loc from st where ts between 1600000000000 and 1600000000010 group by loc;
         count         |              loc               |              loc               |
==========================================================================================
                    10 | nchar0                         | nchar0                         |
                    10 | nchar1                         | nchar1                         |
                    10 | nchar2                         | nchar2                         |
                    10 | nchar3                         | nchar3                         |
                    10 | nchar4                         | nchar4                         |
                    10 | nchar5                         | nchar5                         |
Query OK, 6 row(s) in set (0.003831s)

taos> select count(*) as count, loc from st where ts between 1600000000020 and 1600000000030 group by loc;
Query OK, 0 row(s) in set (0.002620s)

taos> select count(*) as count, loc from st where ts between 1600000000000 and 1600000000010 group by loc
   -> union all
   -> select count(*) as count, loc from st where ts between 1600000000020 and 1600000000030 group by loc;
         count         |              loc               |              loc               |
==========================================================================================
                    10 | nchar0                         | nchar0                         |
                    10 | nchar1                         | nchar1                         |
                    10 | nchar2                         | nchar2                         |
                    10 | nchar3                         | nchar3                         |
                    10 | nchar4                         | nchar4                         |
                    10 | nchar5                         | nchar5                         |
Query OK, 6 row(s) in set (0.004686s)

taos> select count(*) as count, loc from st where ts between 1600000000020 and 1600000000030 group by loc
   -> union all
   -> select count(*) as count, loc from st where ts between 1600000000000 and 1600000000010 group by loc;
         count         |              loc               |              loc               |
==========================================================================================

Query OK, 0 row(s) in set (0.004371s)

从上面的查询中,为什么查询 3 和查询 4 ​​的结果不同?这让我很困惑。

【问题讨论】:

    标签: tdengine


    【解决方案1】:

    因为TDengine的SQL是一种类SQL语言,我猜它的SQL实现和SQL基本一样。在mySQL中,UNIONALL的实现原理是根据前面的表结构而不是下面的表来生成结果表。例如表1的结构是:

    CREATE TABLE `table_1` (
     `col1` int(255) DEFAULT NULL,
     `col2` int(255) DEFAULT NULL,
     `col3` int(255) DEFAULT NULL,
     `col4` int(255) DEFAULT NULL
    ) ENGINE = InnoDB CHARSET = latin1;
    

    表2的结构是:

    CREATE TABLE `table_1` (
     `col4` int(255) DEFAULT NULL,
     `col3` int(255) DEFAULT NULL,
     `col2` int(255) DEFAULT NULL,
     `col1` int(255) DEFAULT NULL
    ) ENGINE = InnoDB CHARSET = latin1;
    

    表1Unionall和表2的结果与表1的结构相同,表2Unionall的结果与表2的结构相同。

    所以对于这个问题,在第二种情况下,前面的表是空的,TDengine可能无法识别它的表结构,所以结果表结构也是空的,导致结果不一致。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2018-02-04
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多