如何解决SQL插入新记录如果不存在,否则更新现有记录
我有2个表Staging&Main Table都有SKU和Price列。记录从那里插入到暂存表中,具体取决于记录,该记录决定是要插入新记录还是更新现有记录。我已经编写了多个查询,这些查询工作正常,但所需时间比平时长。我正在做的是:
Staging Table where data initially inserted into
+----------+--------------+-----------+-------+---------------------------------------------------------------------------------------------------------------------------+
| indexid | Persisted | sku | price | Logic |
+----------+--------------+-----------+-------+---------------------------------------------------------------------------------------------------------------------------+
| 1 | No | 4567456 | 10.99 | This row doesn't exists in main table so need to be inserted and update the persisted to NewProduct |
| 2 | No | 5463454 | 5.99 | This row exists and price matching then this simply need to be updated the persisted to PriceMatched |
| 3 | No | 5645654 | 4.75 | This row exists but price not matching,so new row to be inserted,updated existing record in both tables to PriceUpdated |
| 4 | PriceUpdated | 2222222 | 3.00 | |
| 5 | NewProduct | 5555555 | 4.00 | |
| 6 | PriceMatched | 7685765 | 5.00 | |
| 7 | PriceUpdated | 6574567 | 3.99 | |
+----------+--------------+-----------+-------+---------------------------------------------------------------------------------------------------------------------------+
+---------+--------------+---------+-------+
| indexid | status | sku | price |
+---------+--------------+---------+-------+
| 1 | Active | 5635674 | 10.99 |
| 2 | Active | 5463454 | 5.99 |
| 3 | Active | 5645654 | 2.75 |
| 4 | PriceUpdated | 2222222 | 3.00 |
| 5 | PriceUpdated | 5555555 | 4.00 |
| 6 | PriceMatched | 7685765 | 5.00 |
| 7 | PriceUpdated | 6574567 | 3.99 |
+---------+--------------+---------+-------+
如果主表中不存在产品,则插入新记录并使用持久化='NewProduct'更新登台表
我从登台表中选择所有记录,其中持久化='否' 然后插入新记录
UPDATE stagingTable SET persisted = 'NewProduct' WHERE indexid = '${indexID}';
否则,检查是否存在现有产品价格匹配,然后持续更新='PriceMatched'
UPDATE stagingTable SET persisted = 'PriceMatched' WHERE indexid = '${indexID}' ;
如果价格不匹配,则将新记录插入主表并更新现有记录状态='PriceUpdated',并且暂存表='PriceUpdated'
UPDATE mainTable SET productstatus = 'PriceUpdated' WHERE indexid = '${indexID}';
UPDATE stagingTable SET persisted = 'PriceUpdated' WHERE indexid = '${indexID}';
插入工作正常,但更新花费了很长的10000条记录,大约需要15-20分钟。我正在通过批量插入和更新进行插入和更新。
这是我的逻辑
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。