我遇到了SQL問題和響應時間慢的問題。使用LIMIT加速大型MySQL查詢
這不是因爲我缺少索引,而是因爲結果很大。我使用這些來提供API響應,並將其傳遞給外部客戶端。爲了使響應易於管理,我正在使用分頁。最終,大型網頁最終會放慢速度,直到需要800秒才能完成。這意味着Web服務在等待提供響應的這麼長時間內處於懸置狀態。
mysql> EXPLAIN SELECT * FROM externallinks_global LEFT JOIN externallinks_paywall ON externallinks_global.paywall_id=externallinks_paywall.paywall_id WHERE `has_archive` = 1 LIMIT 1385000,1001;
+------+-------------+-----------------------+--------+------------------------------------------------------------------+------------+---------+--------------------------------------------------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------------------+--------+------------------------------------------------------------------+------------+---------+--------------------------------------------------+---------+-------+
| 1 | SIMPLE | externallinks_global | ref | HASARCHIVE,APIINDEX7,APIINDEX10,APIINDEX11,APIINDEX12,APIINDEX13 | APIINDEX13 | 1 | const | 4291236 | |
| 1 | SIMPLE | externallinks_paywall | eq_ref | PRIMARY | PRIMARY | 4 | s51059__cyberbot.externallinks_global.paywall_id | 1 | |
+------+-------------+-----------------------+--------+------------------------------------------------------------------+------------+---------+--------------------------------------------------+---------+-------+
2 rows in set (0.01 sec)
以上是正在解釋的問題查詢。這需要800秒執行。這是很好的索引,可以看出。我的問題是,如何在大集合內獲取深度結果時幾乎立即得到結果?有沒有辦法做到這一點?
這裏是表的查詢正在運行和表連接它:
CREATE TABLE IF NOT EXISTS `externallinks_global` (
`url_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`paywall_id` INT UNSIGNED NOT NULL,
`url` VARCHAR(767) NOT NULL,
`archive_url` BLOB NULL,
`has_archive` TINYINT UNSIGNED NOT NULL DEFAULT '0',
`live_state` TINYINT UNSIGNED NOT NULL DEFAULT '4',
`last_deadCheck` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`archivable` TINYINT UNSIGNED NOT NULL DEFAULT '1',
`archived` TINYINT UNSIGNED NOT NULL DEFAULT '2',
`archive_failure` BLOB NULL DEFAULT NULL,
`access_time` TIMESTAMP NOT NULL,
`archive_time` TIMESTAMP NULL DEFAULT NULL,
`reviewed` TINYINT UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`url_id` ASC),
UNIQUE INDEX `url_UNIQUE` (`url` ASC),
INDEX `LIVE_STATE` (`live_state` ASC),
INDEX `LAST_DEADCHECK` (`last_deadCheck` ASC),
INDEX `PAYWALLID` (`paywall_id` ASC),
INDEX `REVIEWED` (`reviewed` ASC),
INDEX `HASARCHIVE` (`has_archive` ASC),
INDEX `ISARCHIVED` (`archived` ASC),
INDEX `APIINDEX1` (`live_state` ASC, `paywall_id` ASC),
INDEX `APIINDEX2` (`live_state` ASC, `paywall_id` ASC, `archived` ASC),
INDEX `APIINDEX3` (`live_state` ASC, `paywall_id` ASC, `reviewed` ASC),
INDEX `APIINDEX4` (`live_state` ASC, `archived` ASC),
INDEX `APIINDEX5` (`live_state` ASC, `reviewed` ASC),
INDEX `APIINDEX6` (`archived` ASC, `reviewed` ASC),
INDEX `APIINDEX7` (`has_archive` ASC, `paywall_id` ASC),
INDEX `APIINDEX8` (`paywall_id` ASC, `archived` ASC),
INDEX `APIINDEX9` (`paywall_id` ASC, `reviewed` ASC),
INDEX `APIINDEX10` (`has_archive` ASC, `live_state` ASC, `paywall_id` ASC, `archived` ASC, `reviewed` ASC),
INDEX `APIINDEX11` (`has_archive` ASC, `archived` ASC, `reviewed` ASC),
INDEX `APIINDEX12` (`has_archive` ASC, `live_state` ASC, `paywall_id` ASC),
INDEX `APIINDEX13` (`has_archive` ASC, `live_state` ASC),
INDEX `APIINDEX14` (`has_archive` ASC, `live_state` ASC, `paywall_id` ASC, `reviewed` ASC),
INDEX `APIINDEX15` (`has_archive` ASC, `live_state` ASC, `reviewed` ASC),
INDEX `APIINDEX16` (`has_archive` ASC, `paywall_id` ASC, `reviewed` ASC));
和
CREATE TABLE IF NOT EXISTS `externallinks_paywall` (
`paywall_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`domain` VARCHAR(255) NOT NULL,
`paywall_status` TINYINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`paywall_id` ASC),
UNIQUE INDEX `domain_UNIQUE` (`domain` ASC),
INDEX `PAYWALLSTATUS` (`paywall_status` ASC));
全局表具有約佔27萬行,而付費牆表有大約1600萬。
有可能(或可能不會)改善您的查詢,數據/數據補充或您的設置,但我們知道的只是索引的名稱。這就像「我有一輛汽車,它是由很多零件組成的,我將其中一個'api13'命名爲'api13'(不確定它是否真的是一個有用的零件)。汽車發出奇怪的噪音,不開車。我該怎麼辦?」沒有更多的細節,我們只能給你非常一般的提示,所以請嘗試[MySQL數據 - 實現分頁的最佳方式?](https://stackoverflow.com/questions/3799193/mysql-data-best-way-to-implement-paging)以及所有鏈接。 – Solarflare
感謝您的評論。你在尋找什麼額外的數據?桌子本身?桌子大小? – Cyberpower678
@Solarflare我添加了更多細節。如果你需要更多的細節,請讓我知道。 – Cyberpower678