Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql组和和优化_Mysql_Performance_Group By_Sum_Query Optimization - Fatal编程技术网

Mysql组和和优化

Mysql组和和优化,mysql,performance,group-by,sum,query-optimization,Mysql,Performance,Group By,Sum,Query Optimization,我有两张桌子: CREATE TABLE IF NOT EXISTS `products` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `categoryId` int(10) unsigned DEFAULT NULL, `parentId` int(10) unsigned DEFAULT NULL, `barcode` varchar(255) DEFAULT NULL, `code` varchar(255) DE

我有两张桌子:

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `categoryId` int(10) unsigned DEFAULT NULL,
  `parentId` int(10) unsigned DEFAULT NULL,
  `barcode` varchar(255) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_products_product_categories1_idx` (`categoryId`),
  KEY `fk_products_products1_idx` (`parentId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=71521 ;

CREATE TABLE IF NOT EXISTS `inventory` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `depotId` int(10) unsigned NOT NULL,
  `productId` int(10) unsigned NOT NULL,
  `remain` int(11) DEFAULT NULL,
  `remain2` int(10) unsigned DEFAULT NULL,
  `updatedDateTime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_inventory_depots1_idx` (`depotId`),
  KEY `fk_inventory_products1_idx` (`productId`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=89291 ;
请帮我优化下面的查询应该添加什么索引来提高性能,查询耗时0.2578秒MySQL正在使用索引fk_inventory_depots1_idx,删除时i.depotId=3查询耗时0.5484秒。解释查询显示额外:使用where;使用临时设备;使用文件排序

SELECT `p`.* , SUM(i.remain) AS `remain` , SUM(i.remain2) AS `remain2`
FROM `products` AS `p`
LEFT JOIN `inventory` AS `i` ON p.id = i.productId
WHERE remain != 0 AND i.depotId = 3
GROUP BY `p`.`id`
ORDER BY `p`.`id` DESC
LIMIT 50 

在您的语句中使用的和函数将考虑所有行,极限子句将无效,如最后计算的。当剩余、剩余2的所有值相加时,可能会导致全表扫描

如果希望获得最近50行的总和,请使用


您的查询看起来还可以,但是,您可以将其作为左连接,但是可以使用WHERE-remaine=0和i.DepotID=3将其强制为内部联接。如果您真的只打算输入剩余量不为零的条目,那么您就可以了。但是,如果您想要库存产品,则必须将查询调整为

SELECT 
      `p`.* , 
      SUM(i.remain) AS `remain` , 
      SUM(i.remain2) AS `remain2`
   FROM `
      products` AS `p`
         LEFT JOIN `inventory` AS `i` 
            ON p.id = i.productId
           AND remain != 0 
           AND i.depotId = 3
   GROUP BY 
      `p`.`id`
   ORDER BY 
      `p`.`id` DESC
   LIMIT 50 
请注意连接条件中的细微差别。这允许所有产品-无论匹配的库存记录如何。现在,也就是说,您现在从所有库存中获取,而不仅仅是那些DepotID=3的标准

为了帮助您建立索引,我唯一建议的是使用多字段键。在本例中,您使用表中的大多数字段进行联接或求和。如果字段是索引的一部分,则不必返回到每个记录的实际数据页。小浪费,但可能会使您的最终结果得到改善


键fk_inventory_multiplefields_idx productid,depotid,remain,remain2,

我不能在这里使用JOIN,因为有些产品在inventory table中没有剩余,但WHERE子句使用条件chking从来自inventor的字段中选择我假设I.remain,I.remain2,remaine属于inventor table在inventory table productid上创建索引,留下来,德博蒂德。希望这有助于。。。
SELECT 
      `p`.* , 
      SUM(i.remain) AS `remain` , 
      SUM(i.remain2) AS `remain2`
   FROM `
      products` AS `p`
         LEFT JOIN `inventory` AS `i` 
            ON p.id = i.productId
           AND remain != 0 
           AND i.depotId = 3
   GROUP BY 
      `p`.`id`
   ORDER BY 
      `p`.`id` DESC
   LIMIT 50