0
mysql
에 61K 개의 행이 있고 계산을 시도하지만 쿼리가 4 초가 걸리며 너무 많이 사용한다고 생각합니다. 카운트 쿼리에 많은 시간이 걸릴 때
select count(distinct appeloffre0_.ID_APPEL_OFFRE)
from ao.appel_offre appeloffre0_
inner join ao.lot lots1_ on appeloffre0_.ID_APPEL_OFFRE=lots1_.ID_APPEL_OFFRE
inner join ao.lieu_execution lieuexecut2_ on appeloffre0_.ID_APPEL_OFFRE=lieuexecut2_.appel_offre
inner join ao.acheteur acheteur3_ on appeloffre0_.ID_ACHETEUR=acheteur3_.ID_ACHETEUR
inner join ao.ao_activite aoactivite4_ on appeloffre0_.ID_APPEL_OFFRE=aoactivite4_.ID_APPEL_OFFRE
내 쿼리 결과
:+----------------------------------------------+
| count(distinct appeloffre0_.ID_APPEL_OFFRE) |
+----------------------------------------------+
| 61100 |
+----------------------------------------------+
1 row in set (4.35 sec)
이유 explain
cmd를 표 appeloffre0_
사용 FK 컬럼 ID_ACHETEUR
에 인덱스 인 appel_offre_ibfk_2
키? 내가 빠른 카운트 쿼리를 실행하기 위해 인덱스를 사용하는 방법을 appel_offre
+-------------+------------+--------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+--------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| appel_offre | 0 | PRIMARY | 1 | ID_APPEL_OFFRE | A | 60953 | NULL | NULL | | BTREE | | |
| appel_offre | 1 | appel_offre_ibfk_1 | 1 | ID_APPEL_OFFRE_MERE | A | 2 | NULL | NULL | YES | BTREE | | |
| appel_offre | 1 | appel_offre_ibfk_2 | 1 | ID_ACHETEUR | A | 2 | NULL | NULL | | BTREE | | |
| appel_offre | 1 | appel_offre_ibfk_3 | 1 | USER_SAISIE | A | 2 | NULL | NULL | YES | BTREE | | |
| appel_offre | 1 | appel_offre_ibfk_4 | 1 | USER_VALIDATION | A | 2 | NULL | NULL | YES | BTREE | | |
| appel_offre | 1 | ao_fk_3 | 1 | TYPE_MARCHE | A | 2 | NULL | NULL | YES | BTREE | | |
| appel_offre | 1 | ao_fk_5 | 1 | USER_CONTROLE | A | 2 | NULL | NULL | YES | BTREE | | |
+-------------+------------+--------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
7 rows in set (0.03 sec)
에서
+----+-------------+--------------+--------+----------------------------+--------------------+---------+--------------------------------+-------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------+--------+----------------------------+--------------------+---------+--------------------------------+-------+--------------------------+ | 1 | SIMPLE | appeloffre0_ | index | PRIMARY,appel_offre_ibfk_2 | appel_offre_ibfk_2 | 4 | NULL | 60031 | Using index | | 1 | SIMPLE | acheteur3_ | eq_ref | PRIMARY | PRIMARY | 4 | ao.appeloffre0_.ID_ACHETEUR | 1 | Using index | | 1 | SIMPLE | lieuexecut2_ | ref | fk_ao_lieuex | fk_ao_lieuex | 4 | ao.appeloffre0_.ID_APPEL_OFFRE | 1 | Using index | | 1 | SIMPLE | aoactivite4_ | ref | ao_activites_ao_fk | ao_activites_ao_fk | 4 | ao.lieuexecut2_.appel_offre | 3 | Using where; Using index | | 1 | SIMPLE | lots1_ | ref | FK_LOT_AO | FK_LOT_AO | 4 | ao.lieuexecut2_.appel_offre | 5 | Using where; Using index | +----+-------------+--------------+--------+----------------------------+--------------------+---------+--------------------------------+-------+--------------------------+ 5 rows in set (0.00 sec)
쇼 지수?
여러 참여가있을 때 색인을 사용하는 방법은 무엇입니까?
여러 조인 및 복수 검색 쿼리를 사용할 때 인덱스를 사용하는 방법은 무엇입니까?
감사 0.7s로 내 시간을 줄일 수 @FuzzyTree 당신의 WHERE 절에서 조건을 존재 추가 , 2, 3 점에 대답 할 수 있습니까? 왜냐하면 나는 여러 조인과 함께 다중 기준 검색을 사용할 것이기 때문입니다. 너는 할 수 있으면 너는 나에게 좋은 투투를 줄 수있어. – Youssef
@Youssef 귀하의 설명 출력에 따라 그것은 이미 여러 조인/어디 조건이 – FuzzyTree
그것은 몇 가지 조항을 추가 한 후 그것은 다시 여기에 그것을 확인했다 http://stackoverflow.com/questions/을 추가하는 것 같습니다 인덱스를 올바르게 사용하는 것 같습니다 27784778/slow-count-with-where-clause – Youssef