SQL:如何找到未使用的主键
我有一张桌子,上面有 > 1'000'000 个条目;该表引用了大约 130 个其他表。我的问题是,很多 1-mio-entries 都是旧的且未使用的。
找到未被任何其他表引用的条目的最快方法是什么?我不喜欢做 a
select * from (
select * from table-a TA
minus
select * from table-a TA where TA.id in (
select "ID" from (
(select distinct FK-ID "ID" from table-b)
union all
(select distinct FK-ID "ID" from table-c)
...
有没有更简单、更通用的方法?
谢谢大家!
I've got a table with > 1'000'000 entries; this table is referenced from about 130 other tables. My problem is that a lot of those 1-mio-entries is old and unused.
What's the fastet way to find the entries not referenced by any of the other tables? I don't like to do a
select * from (
select * from table-a TA
minus
select * from table-a TA where TA.id in (
select "ID" from (
(select distinct FK-ID "ID" from table-b)
union all
(select distinct FK-ID "ID" from table-c)
...
Is there an easier, more general way?
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
You could do this:
try :
你也可以尝试:
这是更面向 SQL 的,并且不会像上面的解决方案那样永远花费时间。
try :
you might also try:
This is more SQL oriented and it will not take forever like the above solution.
不确定效率,但是:
如果您关心的是在进行内务管理时允许数据库继续正常操作,您可以将其分为多个阶段:
根据需要多次,然后:
当然,有时进行大量处理需要花费一些时间很多时间。
Not sure about efficiency but:
If your concern is allowing the database to continue normal operations while you do the house keeping you could split it into multiple stages:
as may times as you need and then:
Of course sometimes doing a lot of processing takes a lot of time.
我喜欢上面 @Patrick 的回答,但我想补充一下。
您可以通过扫描 sysObjects、查找关键关系并生成 INSERT 语句来构建这些 INSERT 语句,而不是手动构建 130 步查询。
这不仅可以节省您的时间,还可以帮助您确定是否已覆盖所有表格 - 可能有 131 个,也可能只有 129 个。
I like @Patrick's answer above, but I would like to add to that.
Rather than building the 130-step query by hand, you could build these INSERT statements by scanning sysObjects, finding key relations and generating your INSERT statements.
That would not only save you time, but should also help you to know for sure whether you've covered all the tables - maybe there are 131, or only 129.
我倾向于马塞洛·坎托斯的回答(并已投赞成票),但这里有一个替代方案,试图规避外键上没有索引的问题......
我想做的就是建议一个为了让甲骨文尽量减少其努力。不幸的是,Oracle 会将其编译为与 Marcelo Cantos 的答案非常相似的内容,并且它的性能可能没有任何不同。
I'm inclined to Marcelo Cantos' answer (and have upvoted it), but here is an alternative in an attempt to circumvent the problem of not having indexes on the foreign keys...
All I'm trying to do is to suggest an order to Oracle to minimise its efforts. Unfortunately Oracle will compile this to comething very similar to Marcelo Cantos' answer and it may not performa any differently.