php - MySQL 查询未使用索引

标签 php mysql performance indexing query-performance

我有以下查询:

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

表模式:

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

解释:

+----+-------------+------------------+------+-----------------------------------------+------------+---------+-------+-------+----------------------------------------------+
| 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 |
+----+-------------+------------------+------+-----------------------------------------+------------+---------+-------+-------+----------------------------------------------+

如您所见,有一个名为 company_id_country_id_status 的索引。但是,查询仍然只使用 country_id。我怎样才能使这个查询更有效率?

我还尝试添加索引 bank_codebank_name。但是,同样的结果!

最佳答案

您可以向优化器提供提示,以便它使用特定的索引。 使用索引 ()

SELECT bank_code, bank_name
FROM system_bank_info USE INDEX (company_id_country_id_status)
WHERE company_id=1 AND country_id=103 AND status='ACTIVE'

关于php - MySQL 查询未使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36502660/

相关文章:

c# - 将 PHP 代码转换为 C#

c# - 将数据从 SQL Server 传输到 MySQL

mysql - 在 Laravel 中加入具有冲突列名的三个表

php - laravel查询绑定(bind)使用“<”或“>”失败

performance - 快速 SSE 阈值算法

sql - 对 Service Broker 的性能影响 WAITFOR RECEIVE with no TIMEOUT

PHP数组性能

php - Laravel 存储库模型中的 API 调用

performance - 关于通过 OMNet++ TraCI 在 SUMO 中改变车速的问题

php - 在 PHP 中对表中的数据进行分组