2014-01-12 62 views
3

後,這是一個非常簡單的查詢:MySQL的NOT IN查詢慢得多Mysql的升級

SELECT * FROM temp_company WHERE number NOT IN (SELECT number FROM company) 

它之前服用15分鐘,但是這是在MySQL安裝過低緩衝池大小,15分鐘是可以的,因爲這是一項月度任務。我升級到Mysql 5.7(從5.1或5.2),原始安裝是32位,我無法將innodb緩衝池大小調整到這個數據庫所需的最小10GB(我已經在一臺機器上將它設置爲16GB 。32GB的RAM我現在已經去了一個月後運行該查詢,並將其6小時後仍在運行

的解釋上面是:

​​

對公司和temp_company主索引是編號,但數字是他們匹配,這是一個關鍵在兩個但上述建議它沒有使用temp_company表索引?

o療法我想嘗試的邏輯查詢是:

EXPLAIN SELECT tc.* FROM temp_company tc 
LEFT JOIN company c on c.number = tc.number 
WHERE c.number IS NULL 

這是一樣慢,說明的是:

id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra               | 
1 | SIMPLE  | tc |   | ALL |    |  |   |  | 3226661 | 100.00 |                 | 
2 | SIMPLE  | c  |   | index | number  | number | 33  |  | 3383517 | 100.00 | Using where; Ising index; Using join buffer (block nested loop) | 

任何幫助將非常感激。也許Mysql改變了它找到索引的方式?

* *更新1 -------

SHOW CREATE的: 公司

CREATE TABLE `company` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `count_telephone` mediumint(8) unsigned NOT NULL, 
    `count_fax` mediumint(8) unsigned NOT NULL, 
    `count_person` mediumint(8) unsigned NOT NULL, 
    `person_date` date DEFAULT NULL COMMENT 'Date the company_person relation was updated', 
    `count_email_address` mediumint(8) unsigned NOT NULL, 
    `name` varchar(255) DEFAULT NULL, 
    `url` varchar(255) DEFAULT NULL, 
    `url_date` date DEFAULT NULL, 
    `url_status` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Failure count for crawling the URL', 
    `website_stamp_start` int(10) unsigned DEFAULT NULL, 
    `website_stamp` int(10) unsigned DEFAULT NULL, 
    `ch_url` varchar(255) DEFAULT NULL COMMENT 'Companies house URL', 
    `keywords_stamp_start` int(10) unsigned DEFAULT NULL, 
    `keywords_stamp` int(11) DEFAULT NULL, 
    `number` varchar(30) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, 
    `category` varchar(255) DEFAULT NULL, 
    `status` varchar(255) DEFAULT NULL, 
    `status_date` date DEFAULT NULL COMMENT 'Date the status field was updated', 
    `country_of_origin` varchar(80) DEFAULT NULL, 
    `dissolution_date` date DEFAULT NULL, 
    `incorporation_date` date DEFAULT NULL, 
    `account_ref_day` smallint(5) unsigned DEFAULT NULL, 
    `account_ref_month` smallint(5) unsigned DEFAULT NULL, 
    `account_next_due_date` date DEFAULT NULL, 
    `account_last_made_up_date` date DEFAULT NULL, 
    `account_category` varchar(255) DEFAULT NULL, 
    `returns_next_due_date` date DEFAULT NULL, 
    `returns_last_made_up_date` date DEFAULT NULL, 
    `mortgages_num_charges` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_outstanding` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_part_satisfied` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_satisfied` smallint(5) unsigned DEFAULT NULL, 
    `partnerships_num_gen_partners` smallint(5) unsigned DEFAULT NULL, 
    `partnerships_num_lim_partners` smallint(5) unsigned DEFAULT NULL, 
    `ext_name` varchar(255) DEFAULT NULL, 
    `turnover` decimal(18,2) DEFAULT NULL, 
    `turnover_date` date DEFAULT NULL, 
    `trade_debtors` decimal(18,2) DEFAULT NULL, 
    `other_debtors` decimal(18,2) DEFAULT NULL, 
    `debtors_date` date DEFAULT NULL, 
    `real_turnover_band` int(11) DEFAULT NULL, 
    `est_turnover_band` int(11) DEFAULT NULL, 
    `ext_address_date` date DEFAULT NULL, 
    `care_of` varchar(255) DEFAULT NULL, 
    `po_box` varchar(60) DEFAULT NULL, 
    `line_1` varchar(255) DEFAULT NULL, 
    `line_2` varchar(255) DEFAULT NULL, 
    `town` varchar(60) DEFAULT NULL, 
    `county` varchar(60) DEFAULT NULL, 
    `country` varchar(60) DEFAULT NULL, 
    `post_code` varchar(20) DEFAULT NULL, 
    `DirScrapeID` int(10) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `homepage_keywords_stamp` (`keywords_stamp`), 
    KEY `number` (`number`), 
    KEY `url` (`url`), 
    KEY `town` (`town`), 
    KEY `county` (`county`), 
    KEY `post_code` (`post_code`), 
    KEY `name` (`name`), 
    KEY `website_stamp` (`website_stamp`), 
    KEY `website_stamp_start` (`website_stamp_start`), 
    KEY `keywords_stamp_start` (`keywords_stamp_start`), 
    KEY `turnover` (`turnover`), 
    KEY `status` (`status`), 
    KEY `category` (`category`), 
    KEY `incorporation_date` (`incorporation_date`), 
    KEY `real_turnover_band` (`real_turnover_band`), 
    KEY `est_turnover_band` (`est_turnover_band`) 
) ENGINE=InnoDB AUTO_INCREMENT=3706459 DEFAULT CHARSET=utf8 

temp_company:

CREATE TABLE `temp_company` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `name` varchar(255) DEFAULT NULL, 
    `url` varchar(255) DEFAULT NULL, 
    `ch_url` varchar(255) DEFAULT NULL, 
    `number` varchar(30) DEFAULT NULL, 
    `category` varchar(255) DEFAULT NULL, 
    `status` varchar(255) DEFAULT NULL, 
    `country_of_origin` varchar(80) DEFAULT NULL, 
    `dissolution_date` date DEFAULT NULL, 
    `incorporation_date` date DEFAULT NULL, 
    `account_ref_day` smallint(5) unsigned DEFAULT NULL, 
    `account_ref_month` smallint(5) unsigned DEFAULT NULL, 
    `account_next_due_date` date DEFAULT NULL, 
    `account_last_made_up_date` date DEFAULT NULL, 
    `account_category` varchar(255) DEFAULT NULL, 
    `returns_next_due_date` date DEFAULT NULL, 
    `returns_last_made_up_date` date DEFAULT NULL, 
    `mortgages_num_charges` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_outstanding` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_part_satisfied` smallint(5) unsigned DEFAULT NULL, 
    `mortgages_num_satisfied` smallint(5) unsigned DEFAULT NULL, 
    `partnerships_num_gen_partners` smallint(5) unsigned DEFAULT NULL, 
    `partnerships_num_lim_partners` smallint(5) unsigned DEFAULT NULL, 
    `ext_name` varchar(255) DEFAULT NULL, 
    `turnover` decimal(18,2) DEFAULT NULL, 
    `turnover_date` date DEFAULT NULL, 
    `trade_debtors` decimal(18,2) DEFAULT NULL, 
    `other_debtors` decimal(18,2) DEFAULT NULL, 
    `debtors_date` date DEFAULT NULL, 
    `real_turnover_band` int(11) DEFAULT NULL, 
    `est_turnover_band` int(11) DEFAULT NULL, 
    `ext_address_date` date DEFAULT NULL, 
    `care_of` varchar(255) DEFAULT NULL, 
    `po_box` varchar(60) DEFAULT NULL, 
    `line_1` varchar(255) DEFAULT NULL, 
    `line_2` varchar(255) DEFAULT NULL, 
    `town` varchar(60) DEFAULT NULL, 
    `county` varchar(60) DEFAULT NULL, 
    `country` varchar(60) DEFAULT NULL, 
    `post_code` varchar(20) DEFAULT NULL, 
    `sic_code` varchar(10) DEFAULT NULL, 
    `DirScrapeID` int(10) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `number` (`number`), 
    KEY `status` (`status`), 
    KEY `name` (`name`), 
    KEY `sic_code` (`sic_code`) 
) ENGINE=InnoDB AUTO_INCREMENT=3297833 DEFAULT CHARSET=utf8 

更新2:查詢個人資料(限制5)

+-------------------------------+----------+ 
| Status      | Duration | 
+-------------------------------+----------+ 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000111 | 
| executing      | 0.000001 | 
| Sending data     | 0.000110 | 
| executing      | 0.000001 | 
| Sending data     | 0.000110 | 
| executing      | 0.000001 | 
| Sending data     | 0.000110 | 
| executing      | 0.000001 | 
| Sending data     | 0.000111 | 
| executing      | 0.000001 | 
| Sending data     | 0.000111 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000112 | 
| executing      | 0.000001 | 
| Sending data     | 0.000113 | 
| executing      | 0.000001 | 
| Sending data     | 0.000114 | 
| executing      | 0.000001 | 
| Sending data     | 0.000114 | 
| executing      | 0.000001 | 
| Sending data     | 0.000114 | 
| executing      | 0.000001 | 
| Sending data     | 0.000115 | 
| executing      | 0.000001 | 
| Sending data     | 0.000116 | 
| executing      | 0.000001 | 
| Sending data     | 0.000115 | 
| executing      | 0.000001 | 
| Sending data     | 0.000115 | 
| executing      | 0.000001 | 
| Sending data     | 0.000116 | 
| executing      | 0.000001 | 
| Sending data     | 0.000116 | 
| executing      | 0.000001 | 
| Sending data     | 0.000115 | 
| executing      | 0.000001 | 
| Sending data     | 0.000115 | 
| executing      | 0.000001 | 
| Sending data     | 0.000116 | 
| executing      | 0.000001 | 
| Sending data     | 0.000116 | 
| executing      | 0.000001 | 
| Sending data     | 0.000117 | 
| executing      | 0.000001 | 
| Sending data     | 0.000117 | 
| executing      | 0.000001 | 
| Sending data     | 0.000117 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000118 | 
| executing      | 0.000001 | 
| Sending data     | 0.000120 | 
| executing      | 0.000001 | 
| Sending data     | 0.000120 | 
| executing      | 0.000001 | 
| Sending data     | 0.000121 | 
| executing      | 0.000001 | 
| Sending data     | 0.00| 
| executing      | 0.000001 | 
| Sending data     | 0.000121 | 
| executing      | 0.000001 | 
| Sending data     | 0.000120 | 
| executing      | 0.000001 | 
| Sending data     | 0.000121 | 
| executing      | 0.000001 | 
| Sending data     | 0.000121 | 
| executing      | 0.000001 | 
| Sending data     | 0.000121 | 
| executing      | 0.000001 | 
| Sending data     | 0.000122 | 
| executing      | 0.000001 | 
| Sending data     | 0.00| 
| executing      | 0.000001 | 
| Sending data     | 0.000124 | 
| executing      | 0.000001 | 
| Sending data     | 1.063880 | 
| end       | 0.000009 | 
| query end      | 0.000008 | 
| closing tables    | 0.000009 | 
| freeing items     | 0.000007 | 
| Waiting for query cache lock | 0.000002 | 
| freeing items     | 0.000062 | 
| Waiting for query cache lock | 0.000002 | 
| freeing items     | 0.000001 | 
| storing result in query cache | 0.000002 | 
| cleaning up     | 0.000028 | 
+-------------------------------+----------+ 
+0

表中有多少行? – Bojangles

+2

請爲您的表提供SHOW CREATE語句 – Strawberry

+0

@Bojangles – Knightsy

回答

1

原來,問題是temp_company表號字段沒有將ascii_bin設置爲Collat​​ion(比如Company表)。

正如在MySQL論壇(http://forums.mysql.com/read.php?24,603620,603732#msg-603732)中所解釋的,具有不同歸類或字符集的varchar字段被認爲是不同類型的,因此它們之間不能使用索引。

補救措施是在temp_company表的number字段上設置相同的排序規則。查詢然後花了3.3秒(使用左連接方法2.7秒)。

0

與以前不同,MySQL現在可能未被優化。你應該檢查出 的配置文件,並讓你的系統管理員優化它。此外, 可能是您運行的Web應用程序 和MySQL 5的基礎問題,例如某些較早的插件無法正常工作。

+0

查詢速度與mysql命令行相同 - 不幸的是,我也是系統管理員,無法看到任何配置變量問題。我更新了可能會顯示一些信息的查詢配置文件。 – Knightsy

2

我不知道爲什麼它突然運行速度比較慢,但我建議轉換爲加盟,這應該有更好的表現:

SELECT t.* 
FROM temp_company t 
LEFT JOIN company c ON c.number = t.number 
WHERE c.number is null 

這是通過解決一個not in (...)的一個相當標準的方式加入和工作,因爲外連接不匹配匹配在連接表的列中有空值。

+0

儘管這不是關鍵問題,並且我在我原來的問題中包含了這個查詢,但它的速度比使用'不在'2.7s和3.3s稍快 – Knightsy