简体   繁体   English

使用索引对MYSQL进行查询优化查询

[英]Count query optimization on MYSQL with index

I have this query 我有这个查询

SELECT COUNT(*) as user_count, `partner` FROM (`my_table`) WHERE date_join <='2014-02-19 23:59:59' AND isConfirm=1 GROUP BY `partner`

The table already have 420000 rows and this query is timing out. 该表已经有420000行,此查询正在超时。 I am getting the error lost connection to mysql server during query. 我在查询期间收到与MySQL服务器的连接丢失错误。

I already have indexes for date_join and the explain result is as follows : 我已经有date_join的索引,解释结果如下:

id  select_type     table    type   possible_keys   key      key_len    ref     rows    Extra 
1 SIMPLE           my_table  range  date_join     date_join    9        NULL    112223  Using where; Using temporary; Using filesort 

So this seems utilizing the index, but still I am getting the timeout error. 因此,这似乎是在利用索引,但是仍然出现超时错误。 How can I optimize further ? 如何进一步优化?

You could try creating an index on both date_join and isConfirm. 您可以尝试同时在date_join和isConfirm上创建索引。 Example: 例:

alter table `my_table` add index `my_index` (`date_join`,`isConfirm`);

If you are getting approx same rows by below 2 queries apporox. 如果您在2以下获得大约相同的行,则查询apporox。 112223 rows as your explain suggesting, then there is no benefit of creating combined index as suggested by @RKG but if you are getting much less rows then it can be beneficial. 正如您的解释所建议的那样,使用112223行,那么创建@RKG建议的合并索引没有任何好处,但是如果您得到的行少得多,那么它将是有益的。

SELECT COUNT(*) FROM my_table WHERE date_join <='2014-02-19 23:59:59'; 从my_table WHERE date_join中选择COUNT(*)<='2014-02-19 23:59:59';

SELECT COUNT(*) FROM my_table WHERE date_join <='2014-02-19 23:59:59' and isConfirm=1; 从my_table WHERE date_join <='2014-02-19 23:59:59'中选择COUNT(*)并且isConfirm = 1;

If you are getting approx. 如果你得到约。 same rows from both queries then you can check below things to take care this issue: 来自两个查询的相同行,那么您可以检查以下内容以解决此问题:

First check what is set below variables in your config file /etc/my.cnf 首先检查配置文件/etc/my.cnf中变量下面设置的内容

tmp_table_size = ??? tmp_table_size = ???

max_heap_table_size = ??? max_heap_table_size = ???

sort_buffer_size = ??? sort_buffer_size = ???

join_buffer_size = ??? join_buffer_size = ???

increasing these variables you can get solution but you have to check what total memory in your system and what value will be sufficient for you. 增加这些变量可以得到解决方案,但是您必须检查系统中的总内存以及适合您的值。 As a try you can set values as per below, if your values are less than these values- 如果您的值小于这些值,则可以尝试按照以下方式设置值-

tmp_table_size = 1G tmp_table_size = 1G

max_heap_table_size = 1G max_heap_table_size = 1G

sort_buffer_size = 10M sort_buffer_size = 10M

join_buffer_size = 2M join_buffer_size = 2M

Note: Make sure your system /tmp directory also should have more than 1 GB space. 注意:确保系统/ tmp目录也应具有1 GB以上的空间。


Even you should add a lower date filter also in your query as you are fetching almost all data till yesterday and then applying group by. 甚至您也应该在查询中添加一个较低日期的过滤器,因为您要获取直到昨天的几乎所有数据,然后应用分组依据。

If you need complete count till yesterday then you can try below query. 如果您需要完整的计数,直到昨天,那么您可以尝试以下查询。 I am not sure if it help you but you can try. 我不确定它是否对您有帮助,但您可以尝试。

SELECT COUNT(primary_key) AS user_count, `partner` FROM (SELECT primary_key, partner FROM my_table WHERE date_join <='2014-02-19 23:59:59' AND isConfirm=1) a GROUP BY partner;

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

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