MySQL - 大型表上的ORDER BY和LIMIT性能

时间:2014-06-21 09:45:32

标签: mysql sql-order-by limit myisam

以下SQL查询的平均持续时间 100秒

SELECT id FROM members WHERE boolean_deleted = 0 ORDER BY company_name ASC LIMIT 3045700, 25

该表有 300万记录。

为boolean_deleted和company_name列设置索引

如何优化此SQL查询?

说明:

id: 1
select_type: SIMPLE
table: members
type: range
possible_keys: boolean_deleted
key: boolean_deleted
key_len: 1
ref:
rows: 3045758
Extra: Using where; Using file sort

表格结构:

-- ----------------------------
--  Table structure for `members`
-- ----------------------------
DROP TABLE IF EXISTS `members`;
CREATE TABLE `members` (
    `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creator_id` int(11) UNSIGNED NOT NULL,
    `number` varchar(255) DEFAULT NULL,
    `type` enum('Zakelijk','Prive','Onbekend') NOT NULL DEFAULT 'Onbekend',
    `company_name` varchar(50) NOT NULL,
    `primary_contact` int(11) UNSIGNED NOT NULL,
    `primary_address` int(11) UNSIGNED NOT NULL,
    `primary_bankaccount` int(11) UNSIGNED NOT NULL,
    `market` varchar(255) NOT NULL,
    `awp_klasse` enum('Onbekend','Geen','1','2-4','5-9','10-19','20-49','50-99','100-199','200-499','500-749','750-999','>1000') DEFAULT 'Onbekend',
    `awp_reeel` int(11) UNSIGNED DEFAULT 0,
    `vatnumber` varchar(15) NOT NULL COMMENT 'BTW nummer',
    `ccnumber` varchar(15) NOT NULL COMMENT 'KVK nummer',
    `rsin_number` varchar(15) DEFAULT NULL COMMENT 'Rechtspersonen Samenwerkingsverbanden Informatie Nummer',
    `establishment_number` varchar(25) DEFAULT NULL COMMENT 'Vestigingsnummer kamer',
    `rvm` enum('Onbekend','Buitenlandse onderneming','Besloten Vennootschap','Cooperatieve Vereniging','Commanditaire Vennootschap','Eenmanszaak','Maatschap','Naamloze Vennootschap','Onderlinge Waarborgmaatschappij','Rederij','Stichting','Vereniging','Vennootschap Onder Firma','Europees Economisch Samenwerkingsverband','EG-vennootschap met onderneming in Nederland','EG-vennootschap met Hoofdnederzetting in Nederland','Semi EG-vennootschap met onderneming in Nederland','Semi EG-vennootschap met Hoofdnederzetting in Ned.','Nevenvestiging met vestiging buiten Nederland','Geen rechtsvorm (overheid etc.)','Rechtspersoon in oprichting','Kerkgenootschap','Overige rechtsvormen','Openbare Vennootschap','Europese Naamloze Vennootschap','Vereniging van Eigenaars','Publieksrechtelijke rechtspersoon','Privaatrechtelijke rechtspersoon') DEFAULT 'Onbekend' COMMENT 'Rechtsvorm',
    `sbi` varchar(255) DEFAULT NULL COMMENT 'De Standaard Bedrijfsindeling',
    `sideline_activities_1` varchar(255) DEFAULT NULL COMMENT 'Neven omschrijving',
    `sideline_activities_2` varchar(255) DEFAULT NULL COMMENT 'Neven omschrijving',
    `status` enum('Opgeheven','Failliet','Mailstop','Neutraal','Surseance van betaling','Wet Schuldsanering Natuurlijke Personen') NOT NULL DEFAULT 'Neutraal' COMMENT 'Graydon',
    `boolean_deleted` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
    `boolean_member` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
    `date_created` datetime NOT NULL,
    `reason_of_deletion` text NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `creator_id` (`creator_id`) comment '',
    INDEX `boolean_deleted` (`boolean_deleted`) comment '',
    INDEX `type` (`type`) comment '',
    INDEX `primary_contact` (`primary_contact`) comment '',
    INDEX `primary_address` (`primary_address`) comment '',
    INDEX `primary_bankaccount` (`primary_bankaccount`) comment '',
    INDEX `ccnumber` (`ccnumber`) comment '',
    INDEX `company_name` (`company_name`) comment '',
    FULLTEXT `search_company_name` (`company_name`) comment '',
    FULLTEXT `search_ccnumber` (`ccnumber`) comment ''
) ENGINE=`MyISAM` AUTO_INCREMENT=3045765 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT=DYNAMIC COMMENT='' CHECKSUM=0 DELAY_KEY_WRITE=0;

1 个答案:

答案 0 :(得分:0)

对于此查询,您需要在筛选列上方的索引和您排序的列。在这方面。

在你的情况下:

     INDEX `yourindexname` (`boolean_deleted`, `company_name`)

如果有效,"使用文件排序"不应再在你的解释结果中显示。