- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下查询正在大约 400 万行上运行。前两个 CTE 语句在大约一个小时内执行。然而,最后一个有望持续超过 15 年。
WITH parsed AS (
SELECT name, array(...) description FROM import
), counts AS (
SELECT unnest(description) token, count(*) FROM parsed GROUP BY 1
)
INSERT INTO table (name, description)
SELECT name, ARRAY(
SELECT ROW(token, count)::a
FROM (
SELECT token, (
SELECT count
FROM counts
WHERE a.token=counts.token
)
FROM UNNEST(description) a(token)
) _
)::a[] description
FROM parsed;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------
Insert on table (cost=55100824.40..162597717038.41 rows=3611956 width=96)
CTE parsed
-> Seq Scan on import (cost=0.00..51425557.67 rows=3611956 width=787)
Filter: ((name IS NOT NULL) AND (description IS NOT NULL))
SubPlan 1
-> HashAggregate (cost=11.59..12.60 rows=101 width=55)
-> Append (cost=0.00..11.34 rows=101 width=55)
-> Result (cost=0.00..0.01 rows=1 width=0)
-> Index Scan using import_aliases_mid_idx on import_aliases (cost=0.00..10.32 rows=100 width=56)
Index Cond: (mid = "substring"(import.mid, 5))
SubPlan 2
-> HashAggregate (cost=0.78..1.30 rows=100 width=0)
-> Result (cost=0.00..0.53 rows=100 width=0)
CTE counts
-> HashAggregate (cost=3675165.23..3675266.73 rows=20000 width=32)
-> CTE Scan on parsed (cost=0.00..1869187.23 rows=361195600 width=32)
-> CTE Scan on parsed (cost=0.00..162542616214.01 rows=3611956 width=96)
SubPlan 6
-> Function Scan on unnest a (cost=0.00..45001.25 rows=100 width=32)
SubPlan 5
-> CTE Scan on counts (cost=0.00..450.00 rows=100 width=8)
Filter: (a.token = token)
parsed
和 counts
中大约有 400 万行。查询当前正在运行,最终语句大约每 2 分钟插入一行。它几乎不接触磁盘,但疯狂地吃 CPU,我很困惑。
查询有什么问题?
最后的语句应该在 counts
中查找 description
的每个元素,将像这样的 [a,b,c]
转换为像这样的 [(a,9),(b,4),(c,0)]
并插入它。
编辑
将 parsed 和 counts 作为表,并将 token
in counts 索引,这是计划:
explain INSERT INTO table (name, mid, description) SELECT name, mid, ARRAY(SELECT ROW(token, count)::a FROM (SELECT token, (SELECT count FROM counts WHERE a.token=counts.token) FROM UNNEST(description) a(token)) _)::a[] description FROM parsed;
QUERY PLAN
------------------------------------------------------------------------------------------------------
Insert on table (cost=0.00..5761751808.75 rows=4002061 width=721)
-> Seq Scan on parsed (cost=0.00..5761751808.75 rows=4002061 width=721)
SubPlan 2
-> Function Scan on unnest a (cost=0.00..1439.59 rows=100 width=32)
SubPlan 1
-> Index Scan using counts_token_idx on counts (cost=0.00..14.39 rows=1 width=4)
Index Cond: (a.token = token)
哪个更合理。这些数组平均有 57 个元素,所以我猜这只是对可能相当低效的 CTE 表的大量查找导致性能下降。它现在以每秒 300 行的速度运行,对此我很满意。
最佳答案
正如我在对问题的编辑中所述,将解析和计数作为表,并将计数中的 token 编入索引,速度要快得多。我假设 CTE 连接比它们更聪明。
关于sql - CTE 扫描比应有的速度慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848711/
是否可以在 CTE 内编写 CTE? 我希望它遵循这个逻辑,但解释器不喜欢这段代码。 with outertest as( with test as ( select
我有一个 with 子句,按时间间隔和天气描述对一些天气数据进行分组: With temp_table (counter, hour, current_Weather_description) as
我想要一个普通的 CTE,然后是一个递归的 CTE 如何将这两者结合起来? 我知道我可以做多个纯正常的 CTE WITH CTE1 AS( ), CTE2 AS(),... 所以我试过了 WITH C
在 SQL 中是否可以在同一查询中的另一个 C.T.E 内的公共(public)表表达式内使用引用?这里有一个例子: WITH CT1 AS (SELECT * FROM T), CT2 A
请不要将此问题标记为 CTE within a CTE 的重复项..我检查了那个问题和答案……但那个答案不能满足我的需要。 我想像这样运行嵌套 CTE 查询 Drop Table #Temp Crea
在 jOOQ 中,我在以后的 CTE 中重复使用 CTE。我正在尝试按年份和学校汇总学生的完成记录。我正在使用 jOOQ 3.11.2 和 postgres 9.4。 我有有效的 SQL 代码。但是在
我需要根据最新的时间戳更新 CTE o/p 列值之一(前 1 条记录),然后返回。 查询 WITH cte AS ( select dt_zone.zone_name, dt_mate
我的 CTE 出现错误。我在 Amazon RDS 上托管数据库。我的计算机上有另一个示例数据库主机 (localhost)。 CTE 在我的本地计算机上运行良好。我认为 Amazon RDS 有问题
我正在使用 SQL Server 进行分页,我想通过计算结果总数作为我的部分结果集的一部分来避免重复,而不是获取该结果集然后执行单独的查询以获取计数。然而,麻烦的是,它似乎增加了执行时间。例如,如果我
我想(使用 cte)以这种方式计算表中的 child 数量,以在 parent 级别所有 child 的数量包括他们的 child 。有 sample 吗? 最佳答案 CREATE TABLE t_p
鉴于以下递归 CTE(在我的示例中简化了一点): WITH myCTE (sort, parentid, myid, level, somedata) AS ( -- Anchor membe
我有一张带有自连接的表。您可以将结构视为表示组织层次结构的标准表。例如表:- MemberId MemberName RelatedMemberId 该表由 50000 条样本记录组成。我写了 CTE
我正在尝试将公用表表达式加入现有表 (table1),如下所示。 select column1, column2 from table1 left outer join ;with cte as
我可以在标量函数中使用公用表表达式(CTE) 吗? 我试图用它来获取单个浮点值,但始终为空 这是我计算每位员工总工作时间的函数代码: ALTER FUNCTION GetTotalWorkingHou
我可以在分层数据模型中找到给定记录的所有子项(请参阅下面的代码),但我不确定如何使用给定的子 ID 遍历备份父/子链。谁能指出我正确的方向来弄清楚如何做到这一点?这在 Linq to SQL 中也可能
我在这里遇到了一点困难。我的主要目标是能够在 C# 和 Entity Framework 中使用它,而我们的高层指令是远离存储过程。 我有 2 个表:一个外部参照和一个 (Celko) 树表。 /**
尝试运行此 cte 时出现以下错误 Invalid Object Name 'cte' 使用以下 CTE 语句时出错 WITH cte (LOC_ID, [Description], LOC_TYPE
我正在阅读“Murach 的 SQL Server 2016 for Developers”一书中的示例。该示例说明了如何在 SQL 中编写递归 CTS 代码。我非常了解递归函数(在 C# 中),但我
我有一个相当复杂的查询,其中包含多个 CTE,但有 1 个主 CTE,其他所有 CTE 都从中提取,这是否会导致该主 CTE 被执行多次? 最佳答案 您可以这样使用 CROSS JOIN: SELEC
我正在尝试编写一个递归 CTE,它引用另一个在它之前编写的 CTE。 第一个 cte nodes 在编写递归 Hierarchy cte 之前我用分号关闭了: WITH nodes(node, nod
我是一名优秀的程序员,十分优秀!