I have created a materialized view:
CREATE MATERIALIZED VIEW test_view AS SELECT state, city, zip, loc, pop FROM citylist WHERE state IS NOT NULL AND city IS NOT NULL AND zip IS NOT NULL AND pop IS NOT NULL
PRIMARY KEY (state,city,pop,zip) WITH CLUSTERING ORDER BY (state ASC).
and I get the results from the query "SELECT * FROM test_view Where state='NY'LIMIT 15;" as:
state | city | pop | zip | loc
-------+--------------+-------+-------+-------------------------
NY | ACCORD | 2695 | 12404 | [-74.235336, 41.808308]
NY | ACRA | 525 | 12405 | [-74.085723, 42.330367]
NY | ADAMS BASIN | 989 | 14410 | [-77.853905, 43.190644]
NY | ADAMS CENTER | 2649 | 13606 | [-76.00415, 43.863106]
NY | ADDISON | 4967 | 14801 | [-77.266027, 42.09825]
NY | ADIRONDACK | 105 | 12808 | [-73.782486, 43.716479]
NY | AFTON | 2801 | 13730 | [-75.536604, 42.241737]
NY | AKRON | 7924 | 14001 | [-78.508365, 43.024944]
NY | ALABAMA | 68 | 14003 | [-78.385231, 43.071888]
NY | ALBANY | 2709 | 12207 | [-73.752327, 42.658133]
NY | ALBANY | 6927 | 12204 | [-73.735364, 42.684667]
NY | ALBANY | 9374 | 12210 | [-73.76052, 42.65677]
NY | ALBANY | 10008 | 12209 | [-73.785385, 42.641665]
NY | ALBANY | 11097 | 12202 | [-73.764071, 42.641314]
NY | ALBANY | 17230 | 12206 | [-73.774406, 42.668326]
Now, when I add another condition filtering statement to make the query:
SELECT * FROM test_view Where state='NY' AND pop > 2000 ORDER BY city ASC LIMIT 15;
I get the error:
InvalidRequest: Error from server: code=2200 [Invalid query] message="PRIMARY KEY column "pop" cannot be restricted as preceding column "city" is not restricted"
I want to be able to keep the ordering of the entries in the table/results above, but also allow filtering using "pop". How will I edit my materialized view statement to do so?
答案 0 :(得分:0)
CREATE MATERIALIZED VIEW test_view AS SELECT state, city, zip, loc, pop FROM citylist WHERE state IS NOT NULL AND city IS NOT NULL AND zip IS NOT NULL AND pop IS NOT NULL
PRIMARY KEY (state,pop,city,zip) WITH CLUSTERING ORDER BY (state ASC).
键的创建顺序很重要-您不能在不限制复合键列表中前一列的情况下限制一列(不允许进行过滤,这反而是一个好主意)
答案 1 :(得分:0)
如果您不想同时使用州和城市进行查询,则可以将视图更改为以下所示。
创建材料视图test_view AS SELECT从城市列表中选择状态,城市,邮政编码,位置和弹出状态,其中状态不为空,城市不为空,邮政编码不为空,而弹出不为空 主键(状态,流行音乐,城市,邮政编码),带有聚类排序依据(流行ASC,城市ASC)
然后您可以像这样查询 SELECT * FROM test_view where state ='NY'AND pop> 2000 ORDER BY pop,城市ASC LIMIT 15;
在上面的视图中,它将不会保留州的城市顺序。这将是城市人口的顺序,而不是给定州的城市名称。如果要在州内保留城市名称的排序,那么在按人口进行过滤时,必须指定城市以及州。其他按人口筛选,但牺牲按城市名称排序。
出现错误的原因是,在指定分区键之后,您只能按群集列的顺序进行查询。按聚类列过滤时,不能忽略聚类键顺序中的前一列。