mysql - 使用 2 个连接和 group by 子句优化 mysql 查询

标签 mysql indexing query-optimization mariadb temp-tables

我有一个需要 10-20 秒的查询,但我确信它可以优化,我只是不够好。我需要一些帮助和解释,以便我可以将其应用于类似的查询。 这是我的查询:

SELECT
        `store_formats`.`Store Nbr`,
        `store_formats`.`Store Name`,
        `store_formats`.`Format Name`,
        `eds_sales`.`Date`,
         sum(`eds_sales`.`EPOS Sales`) AS Sales,
         sum(`eds_sales`.`EPOS Quantity`) AS Quantity
         FROM
         `eds_sales`
         INNER JOIN `item_codes` ON `eds_sales`.`Prime Item Nbr` = `item_codes`.`Customer Item`
         INNER JOIN `store_formats` ON `eds_sales`.`Store Nbr` = `store_formats`.`Store Nbr`
         WHERE
         `eds_sales`.`Store Nbr` IN ($storenbr) AND
         `eds_sales`.`Date`  BETWEEN '$startdate' AND '$enddate' AND
         `eds_sales`.`Client` = '$customer' AND
         `eds_sales`.`Retailer` IN ($retailer) AND
         `store_formats`.`Format Name` IN ($storeformat) AND
         `item_codes`.`Item Number` IN ($products)
         GROUP BY
         `store_formats`.`Store Name`,
         `store_formats`.`Store Nbr`,
         `store_formats`.`Format Name`,
         `eds_sales`.`Date`

这是解释输出: enter image description here

正如您将在此处看到的那样,我已经尝试创建了一些索引,其中涉及的列收效甚微。我认为主要延迟是由复制到临时表引起的。

这些是涉及的表:

存储格式:

CREATE TABLE `store_formats` (
`id` int(12) NOT NULL,
`Store Nbr` smallint(5) UNSIGNED DEFAULT NULL,
`Store Name` varchar(27) DEFAULT NULL,
`City` varchar(19) DEFAULT NULL,
`Post Code` varchar(9) DEFAULT NULL,
`Region #` int(2) DEFAULT NULL,
`Region Name` varchar(10) DEFAULT NULL,
`Distr #` int(3) DEFAULT NULL,
`Dist Name` varchar(26) DEFAULT NULL,
`Square Footage` varchar(7) DEFAULT NULL,
`Format` int(1) DEFAULT NULL,
`Format Name` varchar(23) DEFAULT NULL,
`Store Type` varchar(20) DEFAULT NULL,
`TV Region` varchar(12) DEFAULT NULL,
`Pharmacy` varchar(3) DEFAULT NULL,
`Optician` varchar(3) DEFAULT NULL,
`Home Shopping` varchar(3) DEFAULT NULL,
`Retailer` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `store_formats`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `uniqness` (`Store Nbr`,`Store Name`,`Format`),
ADD KEY `Store Nbr_2` (`Store Nbr`,`Format Name`,`Store Name`);

编辑销售:

CREATE TABLE `eds_sales` (
`id` int(12) UNSIGNED NOT NULL,
`Prime Item Nbr` mediumint(7) NOT NULL,
`Prime Item Desc` varchar(255) NOT NULL,
`Prime Size Desc` varchar(255) NOT NULL,
`Variety` varchar(255) NOT NULL,
`WHPK Qty` int(5) NOT NULL,
`SUPPK Qty` int(5) NOT NULL,
`Depot Nbr` int(5) NOT NULL,
`Depot Name` varchar(50) NOT NULL,
`Store Nbr` smallint(5) UNSIGNED NOT NULL,
`Store Name` varchar(255) NOT NULL,
`EPOS Quantity` smallint(3) NOT NULL,
`EPOS Sales` decimal(13,2) NOT NULL,
`Date` date NOT NULL,
`Client` varchar(10) NOT NULL,
`Retailer` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `eds_sales`
ADD UNIQUE KEY `uniqness` (`Prime Item Nbr`,`Prime Item Desc`,`Prime Size Desc`,`Variety`,`WHPK Qty`,`SUPPK Qty`,`Depot Nbr`,`Depot Name`,`Store Nbr`,`Store Name`,`Date`,`Client`) USING BTREE,
ADD KEY `Store Nbr` (`Store Nbr`),
ADD KEY `Prime Item Nbr_2` (`Prime Item Nbr`,`Date`),
ADD KEY `id` (`id`) USING BTREE,
ADD KEY `Store Nbr_2` (`Prime Item Nbr`,`Store Nbr`,`Date`,`Client`,`Retailer`) USING BTREE,
ADD KEY `Client` (`Client`,`Store Nbr`,`Date`),
ADD KEY `Date` (`Date`,`Client`,`Retailer`);

项目代码:

CREATE TABLE `item_codes` (
`id` int(12) NOT NULL,
`Item Number` varchar(30) CHARACTER SET latin1 NOT NULL,
`Customer Item` mediumint(7) NOT NULL,
`Description` varchar(255) CHARACTER SET latin1 NOT NULL,
`Status` varchar(15) CHARACTER SET latin1 NOT NULL,
`Customer` varchar(30) CHARACTER SET latin1 NOT NULL,
`Sort Name` varchar(255) CHARACTER SET latin1 NOT NULL,
`EquidataCustomer` varchar(30) CHARACTER SET latin1 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `item_codes`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `uniq` (`Item Number`,`Customer Item`,`Customer`,`EquidataCustomer`),
ADD KEY `Item Number_2` (`Item Number`,`Sort Name`,`EquidataCustomer`),
ADD KEY `Customer Item` (`Customer Item`,`Item Number`,`Sort Name`,`EquidataCustomer`),
ADD KEY `Customer Item_2` (`Customer Item`,`Item Number`,`EquidataCustomer`);

所以我的问题: 如您所见,我加入了 3 个表,我正在按商店格式按日期查找销售额。我一直在尝试不同类型的连接,或者例如,不是将销售连接到 item_codes 和 store_formats,而是将 store_formats 连接到其他连接,但结果相同。我还使用 IN 传递一些变量数组,因为它们由应用程序中的选择框提供。

  1. 加入这些表的最佳方式
  2. 建议每个表的最佳索引
  3. 为什么我得到临时表?是因为group by吗?有解决方法吗?
  4. 如果需要临时表,是否可以加快创建速度? (我已经在 8 个磁盘的 raid 中有了数据文件夹,但仍然很慢。
  5. 当然欢迎任何建议的替代方案

更新:根据评论中的一些建议更新了我的表格

更新:修改我的 my.cnf 以提高性能(我的 RAM 是 8GB,2 核,/data/tmp 在 8 驱动器 raid 上,与数据所在的位置相同)

tmpdir          = /dev/shm/:/data/tmp:/tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
expire_logs_days        = 10
max_binlog_size   = 100M
innodb_buffer_pool_size = 6G
innodb_buffer_pool_instances = 6
query_cache_type=1

最佳答案

(评论太多了;请原谅我使用答案。)

当你有 INDEX(a)INDEX(a,b) 时,前者是多余的,应该被删除。我看到大约 5 个这样的案例。

每个 store_nbr 恰好有一个 store_name?如果是这样,在多个表中使用 store_name 是多余的。我不知道 store_formats 的用途,但我猜这是用来存放 store_name 的一张 table 。请注意,两个 store_name 列和 store_nbr 列的数据类型大小不一致!

似乎每个商店都应该有一个唯一的编号,如果是这样,那么 ADD UNIQUE KEY uniqness (Store Nbr,Store Name) 应该可能会变成 PRIMARY KEY(store_nbr)。 (抱歉,我不会在您的列名中放置空格。)

以日期开始索引很少有用,所以去掉 KEY Date_2 (Date,Client)。在其位置添加 INDEX(Client, store_nbr, Date);这应该对查询速度有直接影响。您可能会看到 EXPLAIN SELECT... 更改。

int(4) -- 也许您的意思是 SMALLINT UNSIGNED

UNIQUE(或 PRIMARY)键中包含 Date 通常是“错误的”。什么是“客户”在同一天购买了两次相同的东西?

完成这些更改后,让我们再谈谈。

为了查看的一致性,请提供SHOW CREATE TABLE

避免这种结构:

FROM ( SELECT ... )
JOIN ( SELECT ... ) ON ...

这是低效的,因为两个子查询都没有使 JOIN 高效的索引。

关于mysql - 使用 2 个连接和 group by 子句优化 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39293012/

相关文章:

mysql - mysql中的拆分功能

mysql - 在单行mysql中显示重复行

php - 数据库设计麻烦。每个用户的表

mysql - 不确定要对我的 SQL 表应用什么索引

sql - 不一致的 SQL Server 执行计划键查找

javascript - MongoDB - 多个查询和单个文档结果

php - 使用 php 和 mysql 的产品网站

iphone - 使 UITableView 可按索引滚动

mysql - 什么是最好的查询方式?

optimization - 子查询优化实例谈