一次对多个字段进行分组和计数
是否可以合并这些查询,以便搜索条件仅执行一次?
SELECT category, count(*)
FROM ads
WHERE [search criteria]
GROUP BY category
SELECT state, count(*)
FROM ads
WHERE [search criteria]
GROUP BY state
SELECT price_range, count(*)
FROM ads
WHERE [search criteria]
GROUP BY price_range
...
Plus about 10 more COUNTS
我的目标是创建一个类似于此网站<中使用的过滤器/a>.
我正在考虑使用某种搜索引擎(例如 Lucene)而不是关系数据库。欢迎新想法。
Is it possible to merge these queries so the search criteria is executed only once?
SELECT category, count(*)
FROM ads
WHERE [search criteria]
GROUP BY category
SELECT state, count(*)
FROM ads
WHERE [search criteria]
GROUP BY state
SELECT price_range, count(*)
FROM ads
WHERE [search criteria]
GROUP BY price_range
...
Plus about 10 more COUNTS
My aim is to create a filter similar to that is used in this website.
I'm thinking about using some kind of search engine (eg. Lucene) instead of relational database. New ideas are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
什么是数据库管理系统?在 Oracle 中,您可以使用分析函数来做到这一点:
What's the DBMS? In Oracle you can do that with analytic functions:
爱德华多,
您可以使用存储过程吗?如果是这样,您可以先执行公共部分。
然后您可以在较小的表上执行多个查询。您还可以存储结果并将整个结果作为一个包含三列的表返回:
Eduardo,
Are you allowed to use a stored procedure? If so, you can execute the common part first.
Then you can do your multiple queries on the smaller table. You can also store the results and return the whole thing as one table with three columns:
如果您的用例除了列出的查询之外还包括全文搜索,Solr 可能值得一看在。结合了对结构化数据和文本数据的分面搜索,并且可以相当有效地进行您想要的各种计数。但这在一定程度上取决于您拥有多少数据以及您的用例。
If your use case includes full-text search in addition to the queries you listed, Solr might be worth looking at. In incorporates faceted search over structured and text data, and can do the kinds of counts you want reasonably efficiently. But it depends in part on how much data you have, and what your use cases are.
您可以通过使用嵌套查询和一些技巧来做到这一点;)
you can do by using nested query and some tricks ;)