ich unter Abfrage haben:MySQL-Abfrage verwendet nicht den Index
SELECT bank_code, bank_name
FROM system_bank_info
WHERE company_id=1 AND country_id=103 AND status='ACTIVE'
GROUP BY bank_name
ORDER BY bank_name ASC
TABLE SCHEMA:
CREATE TABLE `system_bank_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`country_id` int(11) NOT NULL,
`bank_code` varchar(50) NOT NULL,
`bank_name` varchar(255) NOT NULL,
`branch_code` varchar(50) NOT NULL,
`branch_name` varchar(255) NOT NULL,
`status` enum('ACTIVE','INACTIVE') NOT NULL DEFAULT 'ACTIVE',
`bank_state` varchar(255) DEFAULT NULL,
`bank_district` varchar(255) DEFAULT NULL,
`bank_city` varchar(255) DEFAULT NULL,
`bank_address` text,
`bank_contact` varchar(255) DEFAULT NULL,
`service_type` enum('INSTA_CREDIT') DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `country_id` (`country_id`),
KEY `bank_code` (`bank_code`),
KEY `branch_code` (`branch_code`),
KEY `company_id_country_id_status` (`company_id`,`country_id`,`status`),
CONSTRAINT `system_bank_info_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`),
CONSTRAINT `system_bank_info_ibfk_2` FOREIGN KEY (`country_id`) REFERENCES `system_country_list` (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=150398 DEFAULT CHARSET=utf8
ERKLÄREN:
+----+-------------+------------------+------+-----------------------------------------+------------+---------+-------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+-----------------------------------------+------------+---------+-------+-------+----------------------------------------------+
| 1 | SIMPLE | system_bank_info | ref | country_id,company_id_country_id_status | country_id | 4 | const | 59324 | Using where; Using temporary; Using filesort |
+----+-------------+------------------+------+-----------------------------------------+------------+---------+-------+-------+----------------------------------------------+
Wie man dort sehen kann ein Index genannt company_id_country_id_status
. Aber Abfrage verwendet immer noch country_id
nur. Wie kann ich diese Abfrage effizienter machen?
Ich habe auch versucht, Indizes bank_code
und bank_name
hinzuzufügen. Aber, das gleiche Ergebnis!
Wenn 'country_id' und' company_id' sind beide 'int' Art, warum man sie als Zeichenfolgen in der Abfrage vorbei? Könnten Sie auch das Leistungsproblem quantifizieren, das Ihnen bevorsteht, und uns mitteilen, wie viele Zeilen es für 'company_id' 1 und' country_id' 103 und in der Tabelle insgesamt gibt? –
Es gibt '124294' Zeilen mit' company_id' 1 und 'country_id' 103 –
Wenn' company_id' kein selektiver Index (niedrige Kardinalität) ist, wird MySQL es nicht verwenden, da es eine Verschwendung ist und keine Leistung erbringen wird. Du sagtest, es gibt '124294' Zeilen mit' country_id = 1' - das bedeutet, dass seine Kardinalität niedrig ist und es ein nutzloser Index ist (es verschwendet nur Platz und kann nicht helfen, irgendeine Leistung zu erbringen). Sie können MySQL zwingen, es zu benutzen, aber es wird die Abfrage nicht schneller machen - im Gegenteil. – Mjh