我有一个表来计算单词出现次数WORD_COUNT(WORD, TOTAL)
,它在另一个表的触发器中更新。这是在这样的层次结构查询中完成的:
创建单词和total = 1:
INSERT INTO WORD_COUNT(TOTAL, WORD)
SELECT 1, TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL;
这项工作非常完美,它将一个短语(推文)分成单词并将其插入WORD_COUNT。
现在的问题是,当我想在重复键上增加总数时(WORD是主键),我需要添加一个ON DUPLICATE KEY
子句,它看起来与CONNECT BY
子句不能相处得很好。
此查询无法编译:
INSERT INTO WORD_COUNT(TOTAL, WORD)
SELECT 1, TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL
ON DUPLICATE KEY UPDATE TOTAL=TOTAL+1;
而且这个都没有:
INSERT INTO WORD_COUNT(TOTAL, WORD)
WITH WORDS(WORD) AS
(SELECT DISTINCT 1,
TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL)
SELECT WORD FROM WORD_COUNT
ON DUPLICATE KEY UPDATE TOTAL = TOTAL + 1;
由于这是在高流量表的触发器内发生的,我想在单个查询中解决这个问题,但也许是时候考虑一个中间表了,是吗?
谢谢
答案 0 :(得分:2)
这应该可以使用合并:
MERGE INTO WORD_COUNT WC
USING
(
SELECT DISTINCT 1,
TRANSLATE(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level),
'áéíóúÁÉÍÓÚ',
'aeiouAEIOU')
FROM DUAL
WHERE LENGTH(regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
CONNECT BY regexp_substr(:NEW.TWEET, '[^[:punct:]|[:space:]]+', 1, level) IS NOT NULL
) NW
ON (WC.WORD = NW.WORD)
WHEN MATCHED THEN UPDATE SET WC.TOTAL = WC.TOTAL + 1
WHEN NOT MATCHED THEN INSERT(TOTAL, WORD) VALUES(NW.TOTAL, NW.WORD);
答案 1 :(得分:0)
据我所知oracle db不支持"重复密钥"条款。您应该尝试使用MERGE子句。
类似的问题