MySql巨大的循环优化

时间:2015-10-07 07:49:00

标签: mysql optimization

我实际上处理由两个主要查询编译的脚本。 第一个将返回类似100.000行的内容。

第二个请求第一行的结果(最多返回1到20行)。

我想优化第二个,因为它实际需要1-2秒。全球治疗变得非常漫长。

第一个请求:

SELECT distinct                                                  
substring_index(aoi.reference,'-',1) as ref1,                                      
substring_index(aoi.reference,'-',-1) as ref2, 
DATE_FORMAT(ao.date_contractuelle,'%Y-%m-%d')  as date     

FROM xxx ao                                                                  

JOIN xxx as t2                                                            
ON ao.package_id = t2.package OR (t2.package IS NULL AND ao.package_id IS NULL)  
AND ao.ordertype_id = t2.`type`                                                  

JOIN xxxx aoi ON ao.id = aoi.order_id                                     
JOIN xxxx stk ON aoi.reference = stk.id_stock_sct                               
JOIN xxxx usr ON usr.id = ao.recipient_id                                        

WHERE                                                                            
t2.annee = DATE_FORMAT(ao.date_contractuelle,'%Y')                               
AND ao.status_id = 5                                                             
AND ao.ordertype_id not in (4,11,13)                                             

AND ao.validationDate >= DATE_SUB(NOW(),INTERVAL 2 YEAR)                         
AND stk.position_orli IS NOT NULL                                                
AND aoi.`type` in ('classique','tu')                                             
AND aoi.exceptionalSize_id IS NULL 

第二次请求:

Select distinct pos, qte from(                                                

SELECT stk.position_orli as pos, sum(aoi.quantity) as qte ,stk.id_nature_stock

FROM xxx aoi                                                          

JOIN xxx ao ON ao.id = aoi.order_id                                       
JOIN xxx stk ON stk.id_stock_sct = aoi.reference                            

-- HERE ARE THE VARS FROM THE FIRST REQUEST                                          
AND aoi.reference like 'ref1-%-ref2'     
AND ao.date_contractuelle LIKE 'date%'  
--  -----------------------------------------                    

AND stk.position_orli IS NOT NULL                                                                                
AND aoi.`type` in ('classique','tu')                                           
AND aoi.exceptionalSize_id IS NULL                                            
AND ao.status_id = 5                                                           
AND ao.ordertype_id not in (4,11,13)                                          

GROUP BY stk.position_orli, stk.id_nature_stock                                       

) as tbl                                                                      
)

表格(仅包含重要数据):

`aoi` 8 500 000 lines
(
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `order_id` INT(11) NOT NULL,
    `quantity` INT(11) NOT NULL,
    `reference` VARCHAR(20) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `type` ENUM('classique','te','surmesure','tu') NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `exceptionalSize_id` INT(11) NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `FK_x` FOREIGN KEY (`exceptionalSize_id`) REFERENCES `aoe` (`id`),
    CONSTRAINT `FK_x` FOREIGN KEY (`order_id`) REFERENCES `ao` (`id`)
)


`ao` 1 300 000 lines
(
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `date_contractuelle` DATETIME NULL DEFAULT NULL,
    `recipient_id` INT(11) NULL DEFAULT NULL,

    `status_id` INT(11) NULL DEFAULT NULL,
    `ordertype_id` INT(11) NULL DEFAULT NULL,
    `validationDate` DATETIME NULL DEFAULT NULL,
    `package_id` INT(11) NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `FK_x` FOREIGN KEY (`recipient_id`) REFERENCES `usr` (`id`),
    CONSTRAINT `FK_x` FOREIGN KEY (`package_id`) REFERENCES `pc` (`id`),
    CONSTRAINT `FK_x` FOREIGN KEY (`ordertype_id`) REFERENCES `ot` (`id`)
)


`stk` 180 000 lines
(
    `id_stock_sct` VARCHAR(40) NOT NULL COLLATE 'utf8_unicode_ci',
    `id_nature_stock` VARCHAR(10) NOT NULL COLLATE 'utf8_unicode_ci',
    `position_orli` ENUM('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20') NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    PRIMARY KEY (`id_stock_sct`, `id_nature_stock`)
)

`t2` 2600 lines
(
    `id` INT(40) NOT NULL AUTO_INCREMENT,
    `package` INT(11) NULL DEFAULT NULL,
    `type` INT(11) NOT NULL,
    `annee` YEAR NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `FK__ot` FOREIGN KEY (`type`) REFERENCES `ot` (`id`),
    CONSTRAINT `FK__pc` FOREIGN KEY (`package`) REFERENCES `pc` (`id`)
)


`usr` 208 000 lines
(
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
)

由于

1 个答案:

答案 0 :(得分:0)

没有二级索引?这些可能帮助:

INDEX(exceptionalSize, type) -- aoi
INDEX(status_id, ordertype_id)  -- ao

请使用别名作为前缀,以便我们知道涉及哪些表格:

GROUP BY position_orli, id_nature_stock

如果这是DATETIME,这怎么可能是真的?

AND ao.date_contractuelle LIKE 'date%'