"I'm concerned about performance, especially with large databases" Does performance matter? This should not be part of any normal process. How often do you plan to run this, and to what purpose? It suggests a failure of data integrity. Would adding
check
constraints help?