bz_post
id,uid,title
bz_post_content
id,content
delete from bz_post_content WHERE ID IN (
SELECT a.id from bz_post a
left join bz_post_content c on a.id = c.id
where
a.id <>
(
select min(b.id) from bz_post b
left join bz_post_content d on b.id = d.id
where a.uid = b.uid and a.title = b.title and c.content = d.content
)
);
我寫的很麻煩 其實就是 洗掉同一個uid下相同的title和content 但是保留一條
目前這個sql慢出天際 不太懂sql
uj5u.com熱心網友回復:
用 EXPLAIN 輸出執行計劃,看一下。uj5u.com熱心網友回復:
我理解你的第一條就是指id最小的
delete
from
bz_post_content t1
where exists(
select 1 from bz_post_content t2 where t1.uid=t2.uid and t1.title=t2.title and t1.content=t2.content and t2.id<t1.id
)
uj5u.com熱心網友回復:
順說一句,處理SQL時候最重要的保持面向集合的思想。不是一條條找出符合xx條件的那一行,而已一次性找出符合條件那個集合。如果是從程式要轉DBA要花好久克服原有的固定思維方式。uj5u.com熱心網友回復:
你的意思應該就是行去重吧,可以參考下:https://blog.csdn.net/weixin_39540651/article/details/106387049
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/16718.html
標籤:PostgreSQL
