【发布时间】:2021-04-05 15:42:51
【问题描述】:
我试图让那些将符合我的过滤条件的成员,但我在圆括号内传递了多个 OR 条件,并且圆括号内的每个条件都是,并且带有另一个圆括号;但是,查询不起作用,它返回一个空表,但是每当我使用 INTERSECT 语句运行查询时,它都会返回成员。尽管如此,100 万条记录已经花费了 3 秒,而且在服务器端这是一个非常昂贵的操作,如果记录增加,它会增加。有人可以帮我为什么我的第一个查询不起作用,或者如果我使用第二个查询,有什么办法可以缩短时间吗?
第一个查询,返回空表
SELECT custom_attribute_values.attributable_id as Member_id FROM custom_attribute_values
WHERE (("custom_attribute_id" = '12' AND "value_string" = 'Female') OR ("custom_attribute_id" = '12' AND "value_string" = 'Male'))
AND (("custom_attribute_id" = '17' AND "value_string" = 'Widowed') OR
("custom_attribute_id" = '17' AND "value_string" = 'Divorced') OR ("custom_attribute_id" = '17' AND "value_string" = 'Never married') OR
("custom_attribute_id" = '17' AND "value_string" = 'Married') OR ("custom_attribute_id" = '17' AND "value_string" = 'Separated'))
第二个查询,返回结果,我对此很感兴趣,但花费了太多时间
SELECT custom_attribute_values.attributable_id FROM custom_attribute_values
WHERE (("custom_attribute_id" = '12' AND "value_string" = 'Female') OR ("custom_attribute_id" = '12' AND "value_string" = 'Male'))
INTERSECT
SELECT custom_attribute_values.attributable_id FROM custom_attribute_values WHERE (("custom_attribute_id" = '17' AND "value_string" = 'Widowed') OR
("custom_attribute_id" = '17' AND "value_string" = 'Divorced') OR ("custom_attribute_id" = '17' AND "value_string" = 'Never married') OR
("custom_attribute_id" = '17' AND "value_string" = 'Married') OR ("custom_attribute_id" = '17' AND "value_string" = 'Separated'))
【问题讨论】:
标签: sql postgresql query-optimization