簡體   English   中英

查詢優化| Magento | MariaDB的

[英]Query optimization | Magento | MariaDB

請幫助優化以下查詢。 “Order By”子句不適用於添加的索引。

查詢:

SELECT
    `lpsi`.`listing_product_id`
FROM
    `m2epro_listing_product_instruction` AS `lpsi`
LEFT JOIN `m2epro_processing_lock` AS `pl` ON
    pl.object_id = lpsi.listing_product_id
    AND model_name = 'M2ePro/Listing_Product'
WHERE
    (lpsi.component = 'ebay')
    AND (pl.id IS NULL)
GROUP BY
    `lpsi`.`listing_product_id`
ORDER BY
    MAX(lpsi.priority) DESC,
    MIN(lpsi.create_date) ASC
LIMIT 10

索引信息:

m2epro_listing_product_instruction,0,PRIMARY,1,id,A,34,,,"",BTREE,"",""
m2epro_listing_product_instruction,1,listing_product_id,1,listing_product_id,A,34,,,"",BTREE,"",""
m2epro_listing_product_instruction,1,component,1,component,A,4,,,YES,BTREE,"",""
m2epro_listing_product_instruction,1,type,1,type,A,11,,,"",BTREE,"",""
m2epro_listing_product_instruction,1,priority,1,priority,A,8,,,"",BTREE,"",""
m2epro_listing_product_instruction,1,create_date,1,create_date,A,34,,,YES,BTREE,"",""

按條款排序的結果:

"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
1,SIMPLE,lpsi,ALL,component,,,,34,Using where; Using temporary; Using filesort
1,SIMPLE,pl,ref,"model_name,object_id",model_name,"767",const,1,Using where; Not exists

無法按順序排序的結果:

CLAUSE:"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
1,SIMPLE,lpsi,index,component,listing_product_id,"4",,10,Using where
1,SIMPLE,pl,ref,"model_name,object_id",model_name,"767",const,1,Using where; Not exists

下面提供的表格詳細信息:顯示創建表格:

CREATE TABLE `m2epro_listing_product_instruction` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`listing_product_id` INT(11) UNSIGNED NOT NULL,
`component` VARCHAR(10) DEFAULT NULL,
`type` VARCHAR(255) NOT NULL,
`initiator` VARCHAR(255) NOT NULL,
`priority` INT(11) UNSIGNED NOT NULL,
`additional_data` LONGTEXT DEFAULT NULL,
`create_date` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `listing_product_id` (`listing_product_id`),
INDEX `component` (`component`),
INDEX `type` (`type`),
INDEX `priority` (`priority`),
INDEX `create_date` (`create_date`)
)

CREATE TABLE `m2epro_processing_lock` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `processing_id` INT(11) UNSIGNED NOT NULL,
  `model_name` VARCHAR(255) NOT NULL,
  `object_id` INT(11) UNSIGNED NOT NULL,
  `tag` VARCHAR(255) DEFAULT NULL,
  `update_date` DATETIME DEFAULT NULL,
  `create_date` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX `processing_id` (`processing_id`),
  INDEX `model_name` (`model_name`),
  INDEX `object_id` (`object_id`),
  INDEX `tag` (`tag`)
)

這個復合覆蓋索引可以加速ORDER BY版本:

INDEX(component, listing_product_id, priority, create_date)

並且,對於另一個表:

INDEX(object_id, model_name, id)  -- in either order

(來自評論)

添加索引后仍未找到優化結果。 請檢查EXPLAIN結果。

"id","select_type","table","type","possible_keys", 
   "key","key_len","ref","rows","Extra"  
1,SIMPLE,lpsi,ref,"component,m2epro_listing_produ_idx_component_id",
   m2epro_listing_produ_idx_component_id,"33",const,37,Using where; Using index; Using temporary; Using filesort 
1,SIMPLE,pl,ref,"model_name,object_id",
   model_name,"767",const,1,Using where; Not exists

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM