简体   繁体   English

如何提高COUNT(DISTINCT field1)...... GROUP BY field2的性能?

[英]How to improve performance of COUNT(DISTINCT field1)… GROUP BY field2?

I have the following query 我有以下查询

EXPLAIN SELECT COUNT(DISTINCT ip_address) as ip_address, exec_date
    FROM requests
    GROUP BY exec_date;

id  select_type table       type        possible_keys   key         key_len ref      rows   Extra
1   SIMPLE      requests    range       NULL            daily_ips   263     NULL    488213  Using index for group-by (scanning)

With a covering index daily_ips 覆盖索引daily_ips

Table       Non_unique  Key_name    Seq_in_index    Column_name Collation   Cardinality Sub_part    Packed  Null    Index_type  Comment Index_comment
requests    1           daily_ips   1               exec_date   A           16          NULL        NULL    YES BTREE       
requests    1           daily_ips   2               ip_address  A           483492      NULL        NULL    YES BTREE       

Is there any way I can further optimize this query? 有什么办法可以进一步优化这个查询吗?

What exactly does Using index for group-by (scanning) mean? 究竟Using index for group-by (scanning)意味着什么? Does it mean that the entire GROUP BY clause is done entirely from an index while the COUNT(DISTINCT ip_address) part of the statement is not? 这是否意味着整个GROUP BY子句完全是从索引完成的,而语句的COUNT(DISTINCT ip_address)部分不是?

Based on the data you've provided, I don't see any way you can further optimize the query. 根据您提供的数据,我认为您无法进一步优化查询。

As to your follow-up question, MySQL's manual page describing explain output for Using index for group-by says: 至于你的后续问题,MySQL的手册页描述了使用group-by索引的解释输出:

Similar to the Using index table access method, Using index for group-by indicates that MySQL found an index that can be used to retrieve all columns of a GROUP BY or DISTINCT query without any extra disk access to the actual table. 与Using index table访问方法类似,Using group for group-by表示MySQL找到了一个索引,可用于检索GROUP BY或DISTINCT查询的所有列,而无需对实际表进行任何额外的磁盘访问。 Additionally, the index is used in the most efficient way so that for each group, only a few index entries are read. 此外,索引以最有效的方式使用,因此对于每个组,只读取少数索引条目。 For details, see Section 8.13.10, “GROUP BY Optimization” . 有关详细信息,请参见第8.13.10节“GROUP BY优化”

Your index is particularly well-suited to speeding up your query. 您的索引特别适合加快查询速度。 Because only indexed fields are being selected (each column in your query also appears in the index), MySQL may not even need to hit the table at all, as all the relevant data appears in the index. 因为只选择了索引字段(查询中的每一列也出现在索引中),所以MySQL甚至可能根本不需要访问该表,因为所有相关数据都出现在索引中。

If executing a query were like performing a search on google , imagine not having to click through to any of the linked sites, because you found the information you were looking for directly in the search results - that's sort of like what not needing to scan the table data is like. 如果执行查询就像在谷歌上执行搜索一样,想象不必点击进入任何链接网站,因为您在搜索结果中直接找到了您要查找的信息 - 这有点像不需要扫描表数据就像。 Here is some more information on how MySQL uses indexes : 以下是有关MySQL如何使用索引的更多信息:

In some cases, a query can be optimized to retrieve values without consulting the data rows. 在某些情况下,可以优化查询以在不咨询数据行的情况下检索值。 (An index that provides all the necessary results for a query is called a covering index .) If a query uses only columns from a table that are numeric and that form a leftmost prefix for some key, the selected values can be retrieved from the index tree for greater speed: (为查询提供所有必要结果的索引称为覆盖索引 。)如果查询仅使用表中的数字列并且形成某个键的最左前缀,则可以从索引中检索所选值树速度更快:

SELECT key_part3 FROM tbl_name WHERE key_part1 =1 SELECT key_part3 FROM tbl_name WHERE key_part1 = 1

You can you the Objectify : Objectify你可以:

Objectify ofy = ObjectifyService.begin(); Objectify ofy = ObjectifyService.begin(); Query query = ofy.query(here is the class name.class).filter("column name in the table", value for which to query).list(); 查询query = ofy.query(这里是类name.class).filter(“表中的列名”,要查询的值).list();

Before that you might need to add the jar for Objectify. 在此之前,您可能需要为Objectify添加jar。

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

相关问题 如何插入到TABLE1(field1,field2&#39;value&#39;,&#39;myval&#39;,&#39;currentdate&#39;)从TABLE2选择(field1,field2) - How to insert into TABLE1 (field1,field2 'value','myval','currentdate') select(field1,field2) from TABLE2 CodeIgniter其中值IN(Field1,Field2) - CodeIgniter Where value IN (Field1,Field2) 如果Field1是特定的,则Field1等于Field2。 - Field1 equals Field2 if Field1 is specific… MySQL IF函数-WHERE&#39;field1&gt; 0&#39;吗? &#39;field2 &lt;field1&#39;:&#39;&#39; - MySQL IF function - WHERE 'field1 > 0' ? 'field2 < field1' : '' (MySQL)OrderBy Field1 = 3,Field2 - (MySQL) OrderBy Field1=3, Field2 复制field2时更新field1 - Update field1 when field2 is duplicated 从select1字段1插入field1,field2,field2仅填充1个字段 - Inserting into field1,field2 from a select field1, field2 populates only 1 field mysql计数(条件1然后字段1,字段2,字段3结束时区分大小写)作为结果不允许在case语句中使用多个字段 - mysql count(distinct case when condition1 then field1, field2, field3 end) as result doesn't allow multiple fields in case statement MYSQL仅在field1或field2不等于文本的情况下选择field1,field2 - MYSQL select field1, field2 only where field1 or field2 does not equal text MATCH(字段1,字段2)和MATCH(字段1)或MATCH(字段2)之间的区别 - Difference between MATCH (field1, field2) and MATCH(field1) OR MATCH(field2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM