【发布时间】:2018-02-09 00:34:36
【问题描述】:
我有一张带有数字过滤器(年龄、体重、薪水等)的表格。 由于我事先不知道过滤器,我必须使用键值对:
company
id, name
1, ACME
users
id, name, company_id
1, jon doe, 1
filters
id, user_id, filter, value
1, 1, 'age', 30
2, 1, 'weight', 82
3, 2, 'salary', 50000
我的查询检索属于特定公司并匹配一个或多个过滤条件的任意组合的用户:例如
SELECT COUNT(*) FROM users, filters as age, filters as weight
WHERE age.user_id = users.id
AND weight.user_id = users.id
AND age.filter = 'age'
AND age.value = 30
AND weight.filter = 'weight'
AND weight.value = 100
AND users.company_id = 1
该表包含数百万行,我已经尝试了过滤器、值、user_id 列的所有可能的索引组合。 查询需要几秒钟到几分钟的时间。
我的用例有更好的解决方案吗?
【问题讨论】:
-
编辑您的问题并提供示例数据和所需的结果。
-
@GordonLinoff 我在表/查询结构中上移了一层,以便更清楚地表达我的意思。如果您需要更多信息,请告诉我