加速MySQL查询

时间:2016-08-26 13:12:19

标签: mysql

这是我的查询的缩写:

SELECT product.* FROM product_list product
LEFT JOIN language_item language ON (product.title=language.languageVariable)
WHERE language.languageID = 1
ORDER BY language.languageValue ASC

当我使用它时,查询有3秒钟。当我通过查询删除订单时有0.3秒。你能推荐一个改变来加速吗?

product.titlelanguage.languageVariable是一个语言变量,如global.product.title1,languageValue是标题,如汽车,玩偶或其他东西。

CREATE TABLE `language_item` (
  `languageItemID` int(10) UNSIGNED NOT NULL,
  `languageID` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `languageVariable` varchar(255) NOT NULL DEFAULT '',
  `languageValue` mediumtext NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ALTER TABLE `language_item`
  ADD PRIMARY KEY (`languageItemID`),
  ADD UNIQUE KEY `languageVariable` (`languageVariable`,`languageID`),
  ADD KEY `languageValue` (`languageValue`(300));

id | select_type | table    | partitions | type | possible_keys                              | key              | key_len | ref              | rows | filtered | Extra 
 1 | SIMPLE      | product  | NULL       | ALL  | PRIMARY,inactive,archive,productCategoryID | NULL             | NULL    | NULL             | 1475 |    88.27 | Using where; Using temporary; Using filesort 
 1 | SIMPLE      | language | NULL       | ref  | languageVariable                           | languageVariable | 767     | db.product.title |  136 |     1.00 | Using index condition

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT d.* from (
  SELECT product.*, language.languageValue AS lv
  FROM product_list product
  JOIN language_item language ON (product.title=language.languageVariable)
  WHERE language.languageID = 1
) as d
ORDER BY d.lv ASC

答案 1 :(得分:0)

以下是language_item的结构,索引为:

CREATE TABLE `language_item` (
  `languageItemID` int(10) UNSIGNED NOT NULL,
  `languageID` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `languageVariable` varchar(255) NOT NULL DEFAULT '',
  `languageValue` mediumtext NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ALTER TABLE `language_item`
  ADD PRIMARY KEY (`languageItemID`),
  ADD UNIQUE KEY `languageVariable` (`languageVariable`,`languageID`),
  ADD KEY `languageValue` (`languageValue`(300));

解释:

  

id | select_type |表|分区|类型| possible_keys |关键|   key_len | ref |行|过滤|额外1 |简单|产品| NULL |   所有| PRIMARY,非活动,归档,productCategoryID | NULL | NULL |空值   | 1475 | 88.27 |用在哪里;使用临时;使用filesort 1 |   简单|语言| NULL | ref | languageVariable | languageVariable |   767 | db.product.title | 136 | 1.00 |使用索引条件