避免文件排序和查询优化

时间:2014-06-03 07:15:50

标签: mysql performance group-by

我已经编写了一个查询并对其进行了优化。但仍需要3分钟的查询才能输出。

请查看以下查询。

 SELECT ps_product.id_product,ps_product_lang.name,pa.id_product_attribute,
    pa.`ean13`         AS `a_ean13`,MAX(IF((agl.name LIKE "Color"),al.name,'')) AS Color,
    MAX(IF((agl.name LIKE "Size"),al.name,'')) AS Size,
    ps_product.price,
    ps_product_lang.`description`,`ps_product_lang`.`description_short`,`ps_product_lang`.`meta_description`,
    `ps_product_lang`.`meta_keywords`,`ps_product_lang`.`meta_title`,pa.`default_on`,
    ps_product.`additional_shipping_cost`,
    pa.`reference`, 
    pa.`price`
    FROM 
    `ps_product`
    JOIN ps_product_lang          ON ps_product_lang.id_product = ps_product.id_product
    join ps_product_attribute pa  on pa.`id_product` = ps_product.id_product
    LEFT JOIN ps_product_attribute_combination pac ON pac.id_product_attribute = pa.id_product_attribute
    LEFT JOIN ps_attribute a ON a.id_attribute = pac.id_attribute
    LEFT JOIN ps_attribute_group ag ON ag.id_attribute_group = a.id_attribute_group
    LEFT JOIN ps_attribute_lang al ON (a.id_attribute = al.id_attribute AND al.id_lang = 1)
    LEFT JOIN ps_attribute_group_lang agl ON (ag.id_attribute_group = agl.id_attribute_group AND agl.id_lang = 1)
    GROUP BY pa.id_product_attribute ORDER BY ps_product.id_product;

这是解释命令输出

    id  select_type  table            type    possible_keys                                                                key                                         key_len  ref                                rows  Extra                            
------  -----------  ---------------  ------  ---------------------------------------------------------------------------  ------------------------------------------  -------  -------------------------------  ------  ---------------------------------
     1  SIMPLE       ps_product       ALL     PRIMARY                                                                      (NULL)                                      (NULL)   (NULL)                            11378  Using temporary; Using filesort  
     1  SIMPLE       pa               ref     product_attribute_product,product_default                                    product_attribute_product                   4        catalog.ps_product.id_product         2                                   
     1  SIMPLE       pac              ref     id_product_attribute                                                         id_product_attribute                        4        catalog.pa.id_product_attribute       1  Using index                      
     1  SIMPLE       a                eq_ref  PRIMARY                                                                      PRIMARY                                     4        catalog.pac.id_attribute              1                                   
     1  SIMPLE       ag               eq_ref  PRIMARY                                                                      PRIMARY                                     4        catalog.a.id_attribute_group          1  Using index                      
     1  SIMPLE       al               ref     id_lang,ps_attribute_lang_lang_idx,ps_attribute_lang_attribute_idx           ps_attribute_lang_attribute_idx             4        catalog.a.id_attribute                1                                   
     1  SIMPLE       agl              ref     ps_attribute_group_lang_attributegroup_idx,ps_attribute_group_lang_lang_idx  ps_attribute_group_lang_attributegroup_idx  4        catalog.ag.id_attribute_group         1                                   
     1  SIMPLE       ps_product_lang  ref     ps_product_lang_product_lang_shop,ps_product_lang_product_idx                ps_product_lang_product_lang_shop           4        catalog.ps_product.id_product         3

有什么方法可以提高性能。

以下是创建声明。

CREATE TABLE `ps_product` (
  `id_product` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_supplier` int(10) unsigned DEFAULT NULL,
  `id_manufacturer` int(10) unsigned DEFAULT NULL,
  `id_category_default` int(10) unsigned DEFAULT NULL,
  `id_shop_default` int(10) unsigned NOT NULL DEFAULT '1',
  `id_tax_rules_group` int(11) unsigned NOT NULL,
  `on_sale` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `online_only` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `ean13` varchar(13) DEFAULT NULL,
  `upc` varchar(12) DEFAULT NULL,
  `ecotax` decimal(17,6) NOT NULL DEFAULT '0.000000',
  `quantity` int(10) NOT NULL DEFAULT '0',
  `minimal_quantity` int(10) unsigned NOT NULL DEFAULT '1',
  `price` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `wholesale_price` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `unity` varchar(255) DEFAULT NULL,
  `unit_price_ratio` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `additional_shipping_cost` decimal(20,2) NOT NULL DEFAULT '0.00',
  `reference` varchar(32) DEFAULT NULL,
  `supplier_reference` varchar(32) DEFAULT NULL,
  `location` varchar(64) DEFAULT NULL,
  `width` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `height` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `depth` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `weight` decimal(20,6) NOT NULL DEFAULT '0.000000',
  `out_of_stock` int(10) unsigned NOT NULL DEFAULT '2',
  `quantity_discount` tinyint(1) DEFAULT '0',
  `customizable` tinyint(2) NOT NULL DEFAULT '0',
  `uploadable_files` tinyint(4) NOT NULL DEFAULT '0',
  `text_fields` tinyint(4) NOT NULL DEFAULT '0',
  `active` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `available_for_order` tinyint(1) NOT NULL DEFAULT '1',
  `available_date` date NOT NULL,
  `condition` enum('new','used','refurbished') NOT NULL DEFAULT 'new',
  `show_price` tinyint(1) NOT NULL DEFAULT '1',
  `indexed` tinyint(1) NOT NULL DEFAULT '0',
  `visibility` enum('both','catalog','search','none') NOT NULL DEFAULT 'both',
  `cache_is_pack` tinyint(1) NOT NULL DEFAULT '0',
  `cache_has_attachments` tinyint(1) NOT NULL DEFAULT '0',
  `is_virtual` tinyint(1) NOT NULL DEFAULT '0',
  `cache_default_attribute` int(10) unsigned DEFAULT NULL,
  `date_add` datetime NOT NULL,
  `date_upd` datetime NOT NULL,
  `advanced_stock_management` tinyint(1) NOT NULL DEFAULT '0',
  `style_guide_url` varchar(100) DEFAULT NULL,
  `sequence` int(10) DEFAULT '0',
  `a_createddate` datetime DEFAULT NULL,
  `a_createdby` bigint(20) DEFAULT NULL,
  `a_modifieddate` datetime DEFAULT NULL,
  `a_modifiedby` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id_product`),
  KEY `product_supplier` (`id_supplier`),
  KEY `product_manufacturer` (`id_manufacturer`),
  KEY `id_category_default` (`id_category_default`),
  KEY `indexed` (`indexed`),
  KEY `date_add` (`date_add`),
  KEY `ps_product.active_ind` (`active`)
)

CREATE TABLE `ps_product_lang` (
  `a_productlangid` int(11) NOT NULL AUTO_INCREMENT,
  `id_product` int(10) unsigned NOT NULL,
  `id_shop` int(11) unsigned NOT NULL DEFAULT '1',
  `id_lang` int(10) unsigned NOT NULL,
  `description` text,
  `description_short` text,
  `link_rewrite` varchar(128) NOT NULL,
  `meta_description` varchar(255) DEFAULT NULL,
  `meta_keywords` varchar(255) DEFAULT NULL,
  `meta_title` varchar(128) DEFAULT NULL,
  `name` varchar(128) NOT NULL,
  `available_now` varchar(255) DEFAULT NULL,
  `available_later` varchar(255) DEFAULT NULL,
  `a_createddate` datetime DEFAULT NULL,
  `a_createdby` bigint(20) DEFAULT NULL,
  `a_modifieddate` datetime DEFAULT NULL,
  `a_modifiedby` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`a_productlangid`,`id_product`,`id_shop`,`id_lang`),
  UNIQUE KEY `ps_product_lang_product_lang_shop` (`id_product`,`id_shop`,`id_lang`),
  KEY `id_lang` (`id_lang`),
  KEY `name` (`name`),
  KEY `ps_product_lang_product_idx` (`id_product`),
  KEY `ps_product_lang_shop_idx` (`id_shop`),
  CONSTRAINT `ps_product_lang_lang` FOREIGN KEY (`id_lang`) REFERENCES `ps_lang` (`id_lang`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `ps_product_lang_product` FOREIGN KEY (`id_product`) REFERENCES `ps_product` (`id_product`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `ps_product_lang_shop` FOREIGN KEY (`id_shop`) REFERENCES `ps_shop` (`id_shop`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

0 个答案:

没有答案