1

1.以下のクエリは、実行に約 49 秒かかります。2.私たちの目標は、1秒から2秒で結果を得る必要があります。3.クエリにインデックスがあり、4.この長時間の実行を回避する方法を使用していました。5. これは単純なクエリです。 6. 可能であれば、クエリを書き直すのを手伝ってください。

query:
select cppm.* from cat_ctlg_product_product_map cppm, cat_product_product_map ppm where cppm.product_product_map_id = ppm.product_product_map_id and ppm.product_id = 2585682 and cppm.catalog_id in ( 2136359, 2136371);

Explain plan:
+----+-------------+-------+-------+------------------------------------+---------------------------+---------+-------+--------+--------------------------------+
| id | select_type | table | type  | possible_keys                      | key                       | key_len | ref   | rows   | Extra                          |
+----+-------------+-------+-------+------------------------------------+---------------------------+---------+-------+--------+--------------------------------+
|  1 | SIMPLE      | ppm   | ref   | PRIMARY,idx_3342,idx_5419          | idx_3342                  | 4       | const |      1 | Using index                    |
|  1 | SIMPLE      | cppm  | range | idx_3472,fkey_cat_ctlg_produc_4100 | fkey_cat_ctlg_produc_4100 | 4       | NULL  | 135334 | Using where; Using join buffer |
+----+-------------+-------+-------+------------------------------------+---------------------------+---------+-------+--------+--------------------------------+
2 rows in set (0.00 sec)

Table structures:
mysql> show create table cat_ctlg_product_product_map\G
*************************** 1. row ***************************
       Table: cat_ctlg_product_product_map
Create Table: CREATE TABLE `cat_ctlg_product_product_map` (
  `row_mod` datetime DEFAULT NULL,
  `row_create` datetime DEFAULT NULL,
  `product_product_map_id` int(11) NOT NULL,
  `catalog_id` int(11) NOT NULL,
  UNIQUE KEY `idx_3472` (`product_product_map_id`,`catalog_id`),
  KEY `fkey_cat_ctlg_produc_4100` (`catalog_id`),
  CONSTRAINT `fkey_cat_ctlg_produc_4100` FOREIGN KEY (`catalog_id`) REFERENCES `cat_catalogs` (`catalog_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fkey_cat_ctlg_produc_5415` FOREIGN KEY (`product_product_map_id`) REFERENCES `cat_product_product_map` (`product_product_map_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)

mysql> show create table cat_product_product_map\G
*************************** 1. row ***************************
       Table: cat_product_product_map
Create Table: CREATE TABLE `cat_product_product_map` (
  `row_mod` datetime DEFAULT NULL,
  `row_create` datetime DEFAULT NULL,
  `product_product_map_id` int(11) NOT NULL,
  `owner_catalog_id` int(11) NOT NULL,
  `parent_product_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `precedence` int(11) DEFAULT '100',
  `is_default` int(11) DEFAULT NULL,
  `product_product_type` enum('cross-sell','skuoption','up-sell','addon','works','kit','autocross') COLLATE latin1_bin DEFAULT NULL,
  PRIMARY KEY (`product_product_map_id`),
  KEY `idx_3342` (`product_id`,`product_product_type`),
  KEY `idx_5251` (`parent_product_id`,`product_product_type`,`product_id`),
  KEY `idx_5419` (`product_product_map_id`,`parent_product_id`,`product_id`),
  KEY `fkey_cat_product_pro_4229` (`owner_catalog_id`),
  KEY `cat_product_product_map_n1` (`parent_product_id`,`product_product_type`,`product_product_map_id`,`precedence`),
  CONSTRAINT `fkey_cat_product_pro_3617` FOREIGN KEY (`product_id`) REFERENCES `cat_products` (`product_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fkey_cat_product_pro_4229` FOREIGN KEY (`owner_catalog_id`) REFERENCES `cat_catalogs` (`catalog_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fkey_cat_product_pro_4362` FOREIGN KEY (`parent_product_id`) REFERENCES `cat_products` (`product_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
4

2 に答える 2

1

結合を使用

select cppm.* from cat_ctlg_product_product_map cppm
INNER JOIN cat_product_product_map ppm ON (cppm.product_product_map_id = ppm.product_product_map_id AND cppm.catalog_id in ( 2136359, 2136371))
WHERE ppm.product_id = 2585682;
于 2012-12-28T13:12:26.227 に答える
1

次のインデックスを作成することをお勧めします。

ALTER TABLE cat_ctlg_product_product_map
  ADD INDEX (catalog_id, product_product_map_id);

ALTER TABLE cat_product_product_map
  ADD INDEX (product_id, product_product_map_id);
于 2012-12-28T13:13:47.287 に答える