+----------------------------+------------------------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+------------------------------------------------------------------------------+------+-----+---------+----------------+
| type | enum('Website','Facebook','Twitter','Linkedin','Youtube','SeatGeek','Yahoo') | NO | MUL | NULL | |
| name | varchar(100) | YES | MUL | NULL | |
| processing_interface_id | bigint(20) | YES | MUL | NULL | |
| processing_interface_table | varchar(100) | YES | MUL | NULL | |
| create_time | datetime | YES | MUL | NULL | |
| run_time | datetime | YES | MUL | NULL | |
| completed_time | datetime | YES | MUL | NULL | |
| reserved | int(10) | YES | MUL | NULL | |
| params | text | YES | | NULL | |
| params_md5 | varchar(100) | YES | MUL | NULL | |
| priority | int(10) | YES | MUL | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| status | varchar(40) | NO | MUL | none | |
+----------------------------+------------------------------------------------------------------------------+------+-----+---------+----------------+
select * from remote_request use index (processing_order) where remote_request.status = 'none' and type = 'Facebook' and reserved = '0' order by priority desc limit 0, 40;
이 테이블은 매우 많은 양의 쓰기 및 읽기를 수신합니다. 각각의 remote_request는 하나의 프로세스가되고, 요청의 유형과 요청에 따라 다른 remote_requests 0에서 5 사이의 아무 곳이나 생성 할 수 있습니다.MySQL 쿼리 최적화 다중 열 인덱스는 이러한 느린 문제를 해결합니까?
테이블은 현재 약 350 만 레코드에 앉아 있으며 사이트 자체에 부하가 많이 걸리고 동시에 50 개 이상의 인스턴스가 동시에 실행될 때 달팽이 속도로 이동합니다. (REST 요청은 확실하지 않은 경우를 대비해 테이블의 목적입니다.)
테이블이 커질수록 더 악화됩니다. 처리 된 요청을 매일 삭제할 수 있지만 궁극적으로는 문제가 해결되지 않습니다.
이 쿼리는 항상 매우 낮은 응답 비율을 유지해야합니다.
다음은 테이블의 현재 색인입니다.
+----------------+------------+----------------------------------+--------------+----------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+----------------------------------+--------------+----------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| remote_request | 0 | PRIMARY | 1 | id | A | 2403351 | NULL | NULL | | BTREE | | |
| remote_request | 1 | type_index | 1 | type | A | 18 | NULL | NULL | | BTREE | | |
| remote_request | 1 | processing_interface_id_index | 1 | processing_interface_id | A | 18 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | processing_interface_table_index | 1 | processing_interface_table | A | 18 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | create_time_index | 1 | create_time | A | 160223 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | run_time_index | 1 | run_time | A | 343335 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | completed_time_index | 1 | completed_time | A | 267039 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | reserved_index | 1 | reserved | A | 18 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | params_md5_index | 1 | params_md5 | A | 2403351 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | priority_index | 1 | priority | A | 716 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | status_index | 1 | status | A | 18 | NULL | NULL | | BTREE | | |
| remote_request | 1 | name_index | 1 | name | A | 18 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | processing_order | 1 | priority | A | 200 | NULL | NULL | YES | BTREE | | |
| remote_request | 1 | processing_order | 2 | status | A | 200 | NULL | NULL | | BTREE | | |
| remote_request | 1 | processing_order | 3 | type | A | 200 | NULL | NULL | | BTREE | | |
| remote_request | 1 | processing_order | 4 | reserved | A | 200 | NULL | NULL | YES | BTREE | | |
+----------------+------------+----------------------------------+--------------+----------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
어떤 생각을 어떻게 해결하나요? 우선 순위에 따라 자동으로 정렬하는 일종의 복잡한 색인을 만들 수 없으며 'Facebook'유형과 일치하는 처음 40 개를 취할 수 있습니까? 현재는 테이블의 500k 행 이상을 스캔하여 결과가 비효율적으로 반환됩니다.
내가 땜질 된 쿼리의 몇 가지 다른 버전은 다음과 같습니다
select * from remote_request use index (type_index,status_index,reserved_index,priority_index) where remote_request.status = 'none' and type = 'Facebook' and reserv ed = '0' order by priority desc limit 0, 40
우리가 얼마나 많은 종류의 요청이 입력에 따라 1000 행 아래에 스캔 행을 얻을 수 있다면 그것은 놀라운 것 표.
미리 감사드립니다. 이것은 가장 경험이 풍부한 MySQL 전문가를 제외하고는 진짜 호두 까기 인 것일 수 있습니다.
데이터의 강도를 표시 할 수는 없지만 SQLfiddle 예제를 사용하면 문제를 더 쉽게 볼 수 있습니다. – skv