MySQL获取仅存在于一个表性能中的行

时间:2012-01-20 10:24:46

标签: mysql

之前已经多次询问和回答此查询的基础知识,但我仍然遇到性能问题。以下是详细信息:

我有表,Products,有105724行。 我有一个更新表,_e360products,有51813行。 我匹配一个字母数字10字符代码,在两个表上都是索引(唯一)。

我试过了:

SELECT _e360products.Product_Code, products.StockCode
FROM _e360products Left Join Products ON _e360products.Product_Code = Products.StockCode
WHERE products.StockCode IS NULL

SELECT Product_Code 
FROM _e360products
WHERE Product_code NOT IN (SELECT StockCode FROM Products)

而且,只是为了笑,甚至:

SELECT Product_Code
FROM _e360products 
WHERE (SELECT count(*) FROM Products WHERE StockCode = Product_code) = 0

这些都没有在20分钟内返回结果!

如果我反转查询,即从_e360产品中获取唯一行,我会很快得到结果。

有没有人有任何想法?

~~~~~更新~~~~~ 解释结果是:

+----+-------------+---------------+--------+---------------+--------------+---------+------------------------------------------+-------+--------------------------------------+
| id | select_type | table         | type   | possible_keys | key          | key_len | ref                                      | rows  | Extra                                |
+----+-------------+---------------+--------+---------------+--------------+---------+------------------------------------------+-------+--------------------------------------+
|  1 | SIMPLE      | _e360products | index  | NULL          | Product_code | 12      | NULL                                     | 50811 | Using index                          |
|  1 | SIMPLE      | Products      | eq_ref | stockcode     | stockcode    | 12      | plumbase_bkup._e360products.Product_code |     1 | Using where; Using index; Not exists |
+----+-------------+---------------+--------+---------------+--------------+---------+------------------------------------------+-------+--------------------------------------+

CREATE TABLE `_e360products` (
  `Product_code` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '',
  `Manufacturers_code` varchar(255) DEFAULT '',
  `Description` varchar(255) DEFAULT '',
  `Supplier` varchar(255) DEFAULT '',
  `Price` varchar(20) DEFAULT '',
  `VAT` varchar(20) DEFAULT '',
  `Analysis_code` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`Product_code`),
  KEY `Product_code` (`Product_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `products` (
  `productid` int(11) NOT NULL AUTO_INCREMENT,
  `QPUM2` varchar(50) NOT NULL DEFAULT '1',
  `NWID` varchar(50) NOT NULL DEFAULT '0',
  `NHEI` varchar(50) NOT NULL DEFAULT '0',
  `NLEN` varchar(50) NOT NULL DEFAULT '0',
  `donotdisplayprice` tinyint(2) DEFAULT '0',
  `productname` text,
  `stockcode` varchar(10) NOT NULL DEFAULT '',
  `analysiscode` varchar(50) DEFAULT '',
  `usestockcontrol` int(11) DEFAULT '0',
  `stockvalue` int(11) DEFAULT '0',
  `stock_notification_level` int(11) DEFAULT '0',
  `sectionid` int(11) DEFAULT '0',
  `productprice` varchar(50) DEFAULT '',
  `productprice_incvat` varchar(50) DEFAULT '',
  `deleted` int(11) DEFAULT '0',
  PRIMARY KEY (`productid`),
  UNIQUE KEY `stockcode` (`stockcode`) USING BTREE,
  KEY `deleted` (`deleted`),
  KEY `allowordering` (`allowordering`),
) ENGINE=MyISAM AUTO_INCREMENT=147440 DEFAULT CHARSET=latin1;

NoteL Products表不包含所有字段,因为有很多......

2 个答案:

答案 0 :(得分:1)

错字? StockCode [在这里添加空间]是

SELECT _e360products.Product_Code, products.StockCode
FROM _e360products Left Join Products ON _e360products.Product_Code = Products.StockCode
WHERE products.StockCode IS NULL

答案 1 :(得分:1)

请提供查询执行计划(EXPLAIN),似乎您的索引未被使用。同时显示为两个表的CREATE TABLE。

相关问题