簡體   English   中英

慢的mysql查詢,使用filesort復制到tmp表

[英]Slow mysql query, copying to tmp table, using filesort

我需要加快此查詢的速度。 花費12秒,返回3917條記錄。 我的mySQL安裝未針對性能進行調整,也許我需要修改一些配置變量以提高性能。 在查詢的大部分時間內,查詢都會顯示“ 正在復制到tmp表 ”。

該查詢用於生成用於參數搜索的過濾器(即,按品牌名稱,顏色等過濾搜索結果)。

查詢:

SELECT  attributenames.attributeid,
        search_attribute_values.valueid,
        attributenames.name,
        search_attribute_values.value,
        count(search_attribute_values.value) as count,
        search_attribute_values.absolutevalue
    FROM  product
    INNER JOIN  vendorimport
               ON (vendorimport.productid = product.productid
              AND  product.categoryid = 4871)
    INNER JOIN  search_attribute
               ON (search_attribute.productid = product.productid
              AND  search_attribute.localeid = 1)
    INNER JOIN  search_attribute_values
               ON (search_attribute.valueid = search_attribute_values.valueid)
    INNER JOIN  attributenames
               ON (attributenames.attributeid = search_attribute.attributeid
              AND  attributenames.localeid = 1)
    GROUP BY  attributenames.attributeid, search_attribute_values.valueid

說明:

+----+-------------+-------------------------+--------+----------------------------------------------------+----------------------------+---------+---------------------------------------+-------+----------------------------------------------+
| id | select_type | table                   | type   | possible_keys                                      | key                        | key_len | ref                                   | rows  | Extra                                        |
+----+-------------+-------------------------+--------+----------------------------------------------------+----------------------------+---------+---------------------------------------+-------+----------------------------------------------+
|  1 | SIMPLE      | product                 | ref    | PRIMARY,product_categoryID,categoryid_productid    | categoryid_productid       | 4       | const                                 | 38729 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | vendorimport            | ref    | productimport_productid                            | productimport_productid    | 5       | microcad.product.productid            |     1 | Using where; Using index                     |
|  1 | SIMPLE      | search_attribute        | ref    | PRIMARY                                            | PRIMARY                    | 8       | microcad.vendorimport.productid,const |     8 | Using where; Using index                     |
|  1 | SIMPLE      | attributenames          | ref    | attributenames_attributeID,attributenames_localeID | attributenames_attributeID | 8       | microcad.search_attribute.attributeid |     4 | Using where                                  |
|  1 | SIMPLE      | search_attribute_values | eq_ref | PRIMARY                                            | PRIMARY                    | 4       | microcad.search_attribute.valueid     |     1 |                                              |
+----+-------------+-------------------------+--------+----------------------------------------------------+----------------------------+---------+---------------------------------------+-------+----------------------------------------------+

架構:

--
-- Table structure for table `attributenames`
--

DROP TABLE IF EXISTS `attributenames`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `attributenames` (
  `attributeid` bigint(20) NOT NULL DEFAULT '0',
  `name` varchar(110) NOT NULL DEFAULT '',
  `localeid` int(11) NOT NULL DEFAULT '0',
  KEY `attributenames_attributeID` (`attributeid`),
  KEY `attributenames_localeID` (`localeid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `product`
--

DROP TABLE IF EXISTS `product`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product` (
  `productid` int(11) NOT NULL DEFAULT '0',
  `manufacturerid` int(11) NOT NULL DEFAULT '0',
  `isactive` tinyint(1) NOT NULL DEFAULT '1',
  `mfgpartno` varchar(70) NOT NULL DEFAULT '',
  `categoryid` int(11) NOT NULL DEFAULT '0',
  `isaccessory` tinyint(1) NOT NULL DEFAULT '0',
  `equivalency` double NOT NULL DEFAULT '0',
  `creationdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modifieddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `lastupdated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`productid`),
  KEY `product_manufacturerID` (`manufacturerid`),
  KEY `product_categoryID` (`categoryid`),
  KEY `product_mfgPartNo` (`mfgpartno`),
  KEY `categoryid_productid` (`categoryid_productid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `search_attribute`
--

DROP TABLE IF EXISTS `search_attribute`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `search_attribute` (
  `productid` int(11) NOT NULL DEFAULT '0',
  `attributeid` bigint(20) NOT NULL DEFAULT '0',
  `valueid` int(11) NOT NULL DEFAULT '0',
  `localeid` int(11) NOT NULL DEFAULT '0',
  `setnumber` tinyint(2) NOT NULL DEFAULT '0',
  `isactive` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`productid`,`localeid`,`attributeid`,`setnumber`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `search_attribute_values`
--

DROP TABLE IF EXISTS `search_attribute_values`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `search_attribute_values` (
  `valueid` int(11) NOT NULL DEFAULT '0',
  `value` varchar(255) NOT NULL DEFAULT '',
  `absolutevalue` double NOT NULL DEFAULT '0',
  `unitid` int(11) NOT NULL DEFAULT '0',
  `isabsolute` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`valueid`),
  KEY `search_attrval_value` (`value`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

每個表中的記錄數:

search_attribute是72,000,000,search_attribute_values是350,000,乘積是4,000,000

搜索過濾器示例

NULLNOT NULL –除非您有NOT NULL的業務原因,否則請使用NOT NULL NULL

在每個表上將InnoDB與相關的PRIMARY KEY一起使用。 那可能會更快。

有意義的“使用索引”將有所幫助。

product.categoryid = 4871不屬於vendorimportON子句; 將其移至WHERE子句。 (這不會加快速度。)

您的查詢無法進一步優化-它必須執行所有JOIN並交付所有行。

但是...您真的想要3917行輸出嗎? 你能應付嗎? 也許您只想要其中的幾個,並且可以在SELECT期間對其進行過濾? 這樣可以加快速度。

暫無
暫無

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

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