以下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;
最佳答案
对于此查询,您需要在过滤的列和排序的列上方有一个索引。在这个方向上。
在您的情况下:
INDEX `yourindexname` (`boolean_deleted`, `company_name`)
如果有效,则说明结果中不应再显示“使用文件排序”。
关于mysql - MySQL-大表的ORDER BY和LIMIT性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24340365/