简体   繁体   English

sql查询计数两个“未知”列的总和

[英]sql query count for sum of two “unknown” columns

I need to query for the COUNT of rows that fulfill multiple filter criteria. 我需要查询满足多个过滤条件的COUNT行。 However, I do not know which filters will be combined, so I cannot create appropriate indexes. 但是,我不知道将合并哪些过滤器,因此无法创建适当的索引。

SELECT COUNT(id) FROM tbl WHERE filterA > 1000 AND filterD < 500

This is very slow since it has to do a full table scan. 这非常慢,因为它必须进行全表扫描。 Is there any way to have a perfomant query in my situation? 在我的情况下,有什么方法可以进行性能查询?

 id, filterA, filterB, filterC, filterD, filterE
    1, 2394, 23240, 8543, 3241, 234, 23

The issue here is that there are certain limitations in how you can index data on multiple criteria. 这里的问题是,如何在多个条件下对数据建立索引存在某些限制。 These are standard, fundamental issues and to the extent that ElasticSearch is able to get away from the problems it is just brute force parallelism and indexes on everything you may want to filter by. 这些是标准的基本问题,在某种程度上,ElasticSearch可以摆脱这些问题,而这仅仅是蛮力并行性,并且可以对要过滤的所有内容建立索引。

Usually some filters will be more commonly used and more selective, so usually one would start by looking at actual examples of queries and build indexes around the queries which have performed slowly in the past. 通常,一些过滤器将更常用且更具选择性,因此通常一个过滤器是从查看查询的实际示例开始,并围绕过去执行缓慢的查询建立索引。

This means you start with slow query logging and then focus on the most important queries first until you get everything where it is tolerable. 这意味着您将从缓慢的查询日志记录开始,然后首先关注最重要的查询,直到获得可以容忍的所有内容为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM