简体   繁体   English

减少查询的执行时间

[英]Reduce execution time of query

I want to reduce the time taken by the query in mysql. 我想减少mysql中查询所花费的时间。

There are three tables say 有三张桌子说

  1. A ~600k rows, 约60万行
  2. B ~2K rows, B〜2K行,
  3. C ~100K rows C〜100K行

having 2 columns each. 每个都有2列。

  1. A has one column which is used in aggregation and other to join with table B. A有一个列用于聚合,另一列与表B连接。
  2. B has one column to join with A and other with C B具有与A联接的一列,与C联接的另一列
  3. C has one column to join with B and other column to group by. C有一个列要与B连接,而另一列要分组。

What should be the indexing plan to reduce the run time. 应该使用什么索引计划来减少运行时间。 As of now it is using temporary tables and then file sort. 到目前为止,它正在使用临时表,然后使用文件排序。 Is there any way we could avoid temporary tables. 有什么办法可以避免使用临时表。

Sample query : 查询样例:

 SELECT
      sum(`revenue_facts`.`total_price`) AS `m0`
FROM 
    `category_groups` AS `category_groups`,
    `revenue_facts` AS `revenue_facts`,
    `dim_products` AS `dim_products`
WHERE 
    `dim_products`.`product_category_group_sk` =       `category_groups`.`product_category_group_sk` AND  
    `revenue_facts`.`product_sk` = `dim_products`.`product_sk`
GROUP BY `category_groups`.`category_name`;

I already have indexes on group by column and the columns in join. 我已经有按列和联接中的列的索引。

my query is currently taking * 6 minute *s. 我的查询当前需要* 6分钟 * s。 I want to reduce the time taken. 我想减少花费的时间。 table structure is as 表结构为

table A : 表A:

CREATE TABLE `revenue_facts` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `product_sk` bigint(20) unsigned NOT NULL,
  `total_price` decimal(12,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `product_sk` (`product_sk`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Table B : 表B:

CREATE TABLE `dim_products` (
  `product_sk` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `product_category_group_sk` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`product_sk`),
  KEY `product_id` (`product_id`),
 KEY (`product_sk`) (`product_sk`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

table C : 表C:

CREATE TABLE `category_groups` (
  `product_category_group_sk` bigint(20) unsigned NOT NULL,
  `category_sk` bigint(20) unsigned NOT NULL,
  `category_name` varchar(255) NOT NULL,
  PRIMARY KEY (`product_category_group_sk`,`category_sk`),
  KEY `category_sk` (`category_sk`),
  KEY `product_category_group_sk` (`product_category_group_sk`
  KEY `category_sk` (`category_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Execution plan used is: 使用的执行计划是:

1   SIMPLE  dim_products    index   PRIMARY,product_category_group_index    product_category_group_index    8   NULL    651264  Using index; Using temporary; Using filesort
1   SIMPLE  category_groups ref PRIMARY,category_sk,product_category_group_sk,category_name product_category_group_sk   8 etl_testing.dim_products.product_category_group_sk    4   Using index
1   SIMPLE  revenue_facts   ref product_sk  product_sk  8 etl_testing..dim_products.product_sk  5   NULL

Try this: 尝试这个:

SELECT
    sum(`revenue_facts`.`total_price`) AS `m0`
FROM 
    (`dim_products` LEFT JOIN `category_groups` ON `dim_products`.`product_category_group_sk` =  `category_groups`.`product_category_group_sk`)
    LEFT JOIN `revenue_facts` ON `dim_products`.`product_sk` = `revenue_facts`.`product_sk`
GROUP BY `category_groups`.`category_name`;

Also, as Abdul said: 另外,正如阿卜杜勒所说:

"Post your table structures and explain plan" “发布您的表结构并说明计划”

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

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