简体   繁体   English

MySQL中查询非常慢

[英]Very Slow Query in MySQL

I have a scenario like; 我有一个类似的场景;

I have a table named " tbl_gust_comb_archve_01nov11_beyond " 我有一个名为“ tbl_gust_comb_archve_01nov11_beyond ”的表

Indexed keys are set on these fields " Gid , gip , siteid , kw , kwtype , dt , gpage , dated " 索引键在这些字段设置“ Gidgipsiteidkwkwtypedtgpagedated

And

This is my query: 这是我的查询:

SELECT SQL_CALC_FOUND_ROWS 
  gid, gip, siteid, kw, kwtype, dt, count(id) as vpage, sum(mapped) as mapped 
FROM 
  tbl_gust_comb_archve_01nov11_beyond 
WHERE 
  confirmation = 1 
AND 
  dated BETWEEN '2012-01-31' AND '2012-01-31' 
AND 
  siteid = 'bing' 
GROUP BY gid 
ORDER BY dt 
DESC LIMIT 0,50

If you make the date a RANGE such as '2012-01-31' AND '2012-02-01' then the result will take longer then 10-30 minutes. 如果您将日期设为'2012-01-31' AND '2012-02-01'则结果将花费10-30分钟以上的时间。

If you have a date range and REMOVE the " GROUP BY " then the result will be much faster (about 5 minutes). 如果您有日期范围,并删除“ GROUP BY ”,则结果会快得多(约5分钟)。 Though! 虽然! after removing GROUP BY , 5 minutes are also too much... 删除GROUP BY ,5分钟也太多了...

Table size is "30mill records and 12Gig". 表大小为“ 30mill记录和12Gig”。

Thanks! 谢谢!

You should first do EXPLAIN on the query , as Mark Baker suggested within his comment. 你应该首先EXPLAIN的查询 ,如马克·贝克他的评论中提出。

But probably creating a multi-column index on these columns should solve the problem: 但是可能在这些列上创建多列索引应该可以解决此问题:

  • dt (this probably should be the first) dt (可能应该是第一个)
  • confirmation
  • dated
  • siteid
  • gid

I am not sure how the gid should be indexed (on which position, etc.). 我不确定应该如何索引gid (在哪个位置等)。

More details are here, so you can decide on the solution on your own: 此处有更多详细信息,因此您可以自行决定解决方案:

If siteid does not vary a lot, you can try to remove your index on siteid . 如果siteid相差不大,则可以尝试删除siteid上的索引。 If you have 30mill. 如果您有30mill。 record and 1/3 with siteid == "bing", then your query will 记录,并使用siteid ==“ bing” 1/3,那么您的查询将

  1. Grab those 10mill. 抓住那些10mill。 record 记录
  2. Apply your dated lookup afterwards, on those 10 mill. 之后,在这10个工厂上应用您的日期查找。 records, which can be really slow. 记录,这可能真的很慢。

It's quite logical, since selecting a range is, normally, longer than selecting a simple value. 这是很合乎逻辑的,因为选择范围通常比选择简单值长。 If siteid does vary a lot, you can try to add a dual index on both dated & siteid . 如果SITEID确实有很大的差异,你可以尝试在这两个日期SITEID添加双指数。

For confirmation field, since you are in a archive table, maybe you can move those who have not confirmed to an other table. 对于确认字段,由于您位于存档表中,因此您可以将那些尚未确认的对象移到另一个表中。 You can also gain some speed if you are able to remove this check. 如果您能够删除此支票,则也可以提高速度。

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

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