在 postgres 中调整子查询
我在数据库中发现了一些可疑数据。我试图确定某个字段(姓氏)是否正确。我在 postgres 中提出了以下查询:
SELECT members."memberID",
members.lastname
FROM members
WHERE members."memberID" NOT IN (SELECT members."memberID"
FROM members
WHERE members.lastname ~* '[a-zA-z]+([-][a-zA-Z]+)*');
子查询当前与普通名称和带有连字符的名称匹配。父查询应显示与该模式不匹配的成员。目前,该查询需要花费大量时间来运行(我从未见过它完成)。我不确定为什么需要这么长时间或如何改进它。
I have discovered some suspect data in a database. I am attempting to determine if a certain field, lastname, is correct. I have come up with the following query in postgres:
SELECT members."memberID",
members.lastname
FROM members
WHERE members."memberID" NOT IN (SELECT members."memberID"
FROM members
WHERE members.lastname ~* '[a-zA-z]+([-][a-zA-Z]+)*');
The subquery currently matches against normal names and names with a hypen. The parent query should display the members who don't match that pattern. Currently the query takes an incredible amount of time to run (i've never seen it complete). I am not sure why it takes so long or how to improve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不存在
左连接/为空
摘要
引用:
附录
但正如 Andrew Lazarus 指出的那样,如果
MEMBERS
表中没有重复的 memberid,则查询只需:NOT EXISTS
LEFT JOIN / IS NULL
Summary
Quote:
Addendum
But as Andrew Lazarus points out, if there are no duplicates of memberid in the
MEMBERS
table, the query only needs to be:我喜欢 OMG Ponies 的答案,但是如果
memberID
是唯一的(即 PK),您可以完全删除子查询。(我删除了不区分大小写的运算符,因为正则表达式涵盖了这两种情况。)
I like OMG Ponies answer, but if
memberID
is unique (i.e., PK), you can just drop the subquery altogether.(I deleted the case-insensitive operator since the regexp covers both cases.)