简体   繁体   English

优化一个非常庞大的mysql表(查询或mysql)

[英]Optimizing a very huge mysql table (query or mysql)

I have a mysql DB with 50 GB data and 200M record in a table. 我有一个MySQL DB,其中有50 GB数据和200M记录在表中。 I am running following query and it takes 350 second to complete: 我正在运行以下查询,需要350秒才能完成:

SELECT  x_date, count(*) as totl
    FROM  `rec_ex_15`
    WHERE  x_date > '2014-12-01'
      and  typx = '2'
    group by  x_date
    order by  x_date desc 

x_date and typx are indexed. x_date和typx被索引。

Here is the explain: 这里是解释:

id     select_type table       type     possible_keys  key     key_len  ref    rows
1      SIMPLE      rec_ex_15   range    typx,x_date    x_date  3        NULL   15896931  Using where

Are there any way to get the result faster? 有什么方法可以更快地获得结果?

As noted in the comment... your query is very simple. 如评论中所述,您的查询非常简单。 You would be best to have a covering/compound index on (typx, x_date ) 最好在(typx,x_date)上具有覆盖/化合物索引

The typx = '2' first, then the index can jump directly to the date criteria and getting the results. 首先是typx ='2',然后索引可以直接跳到日期条件并获取结果。

You may (future, consider creating a separate aggregate table, such as counts per type and date, then get aggregates from that if you are dealing with 200m records. 您可以(将来,考虑创建一个单独的汇总表,例如每种类型和日期的计数,如果要处理2亿条记录,则可以从中获取汇总表。

Clarifying the index (typx, x_date) 澄清索引(typx,x_date)

Since you are looking for a specific "typx", you want that in the first position of the index and secondary is the x_date. 由于要查找特定的“ typx”,因此您希望索引的第一个位置是x_date,第二个是x_date。 Think of the index like a room of boxes. 将索引想像成一堆盒子。 Each box holds only 1 instance of a "typx" value (1, 2, 3, etc)... Within the box for (in your case typx = '2'), they are then sorted by x_date. 每个框仅包含1个“ typx”值的实例(1、2、3等)...在框中(在您的情况下,typex ='2'),然后按x_date对其进行排序。 So, within the box for typx = 2, you can jump directly to the date in question, grab the records and be done. 因此,在typx = 2的框中,您可以直接跳至相关日期,获取记录并完成操作。

If the index were based on just x_date (as one you had available), assume each box in a room contains a single date. 如果索引仅基于x_date(如您所能使用的那样),则假定房间中的每个框都包含一个日期。 Yes, you can jump directly to the date starting your list, but then you have to go into the box and look through all entries to pull out any typx = 2 records. 是的,您可以直接跳到列表的起始日期,但是然后您必须进入框并浏览所有条目以提取出任何typx = 2记录。 Now, close the box for day 1 and go to the next box date and look for any typx = 2 and so forth. 现在,关闭第1天的包装盒,然后转到下一个包装盒日期,查找是否有typx = 2,依此类推。

Having an efficient index based on your criteria can significantly help queries. 根据您的条件建立有效的索引可以极大地帮助查询。

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

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