Skip to content

Commit bbb7119

Browse files
committed
fix: Remove CTE favor temp table
https://jira.railsc.ru/browse/PC4-17052
1 parent bf8cde3 commit bbb7119

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/redis_counters/dumpers/destination.rb

+12-10
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,37 @@ def initialize(engine)
7474

7575
def merge
7676
target_fields = fields.join(', ')
77+
temp_source = "_source_#{source_table}"
7778

7879
sql = <<-SQL
80+
CREATE TEMP TABLE #{temp_source} ON COMMIT DROP AS
81+
SELECT #{selected_fields_expression}
82+
FROM #{source_table}
83+
#{source_conditions_expression}
84+
#{group_by_expression};
85+
7986
WITH
80-
source AS
81-
(
82-
SELECT #{selected_fields_expression}
83-
FROM #{source_table}
84-
#{source_conditions_expression}
85-
#{group_by_expression}
86-
),
8787
updated AS
8888
(
8989
UPDATE #{target_table} target
9090
SET
9191
#{updating_expression}
92-
FROM source
92+
FROM #{temp_source} AS source
9393
WHERE #{matching_expression}
9494
#{extra_conditions}
9595
RETURNING target.*
9696
)
9797
INSERT INTO #{target_table} (#{target_fields})
9898
SELECT #{target_fields}
99-
FROM source
99+
FROM #{temp_source} as source
100100
WHERE NOT EXISTS (
101101
SELECT 1
102102
FROM updated target
103103
WHERE #{matching_expression}
104104
#{extra_conditions}
105-
)
105+
);
106+
107+
DROP TABLE #{temp_source};
106108
SQL
107109

108110
sql = model.send(:sanitize_sql, [sql, engine.common_params])

0 commit comments

Comments
 (0)