찾다

 >  Q&A  >  본문

数据库 - MySQL 单表500W+数据,查询超时,如何优化呢?

伊谢尔伦伊谢尔伦2854일 전789

모든 응답(4)나는 대답할 것이다

  • 怪我咯

    怪我咯2017-04-17 16:53:13

    이유는 record_global_id 속성을 ​​필터링 중인데 조건이 동일하지 않아 복합색인 뒤의 부분이 사용되지 않기 때문입니다.

    상태 열은 얼마나 차별화되나요? 인덱스 (status, Record_global_id)를 추가하고 시도해 보세요.

    회신하다
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:53:13

    여러 개의 SQL 문으로 분할하여 개별적으로 쿼리합니다.

    회신하다
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:53:13

    질문에 따르면 SQL 문에는 조건이 너무 많지만 인덱스는 하나만 사용할 수 있습니다. 안타깝지 않습니까? WHERE 조건은 다음과 같습니다.

    <사전><코드>( ( ( (`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3) 또는 (`from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1) ) AND `유형` = 2 AND `qa_id` = 0 ) 또는 ------------------ 이 OR 여기 -------------------------- -- ------ (`type` = 3 AND `to_uid` = 52494 AND `to_type` = 3 AND `from_uid` = 5017446 AND `from_type` = 1 AND `모듈` IN ('community.doctor:appointment:notice' , '커뮤니티.의사:이송.치료', 'community.doctor:이전.치료.지불', 'community.doctor:weiyi.guahao.to.user', 'community.doctor:weiyi.prescription.to.patient', 'community.doctor:user.buy.prescription') ) ) AND `상태` = 1 AND `record_global_id` <

    전체 WHERE는 두 부분으로 나눌 수 있습니다. 아이디어는 UNION입니다. 유용하다면 채택해 주시기 바랍니다.

    변환 후 SQL:

    <사전><코드>( 선택하다 `레코드_글로벌_ID`, '유형', '마크', `from_uid`, 'from_type', `to_uid`, `to_type`, `보내기_방법`, '액션', '모듈', `보내기_시간`, '내용' 에서 `im_data_record` 어디 ( (`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3) 또는 (`from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1) ) AND `유형` = 2 AND `qa_id` = 0 AND `상태` = 1 AND `record_global_id` < ) 노동 조합 ( 선택하다 `레코드_글로벌_ID`, '유형', '마크', `from_uid`, 'from_type', `to_uid`, `to_type`, `보내기_방법`, '액션', '모듈', `보내기_시간`, '내용' 에서 `im_data_record` 어디 `type` = 3 AND `to_uid` = 52494 AND `to_type` = 3 AND `from_uid` = 5017446 AND `from_type` = 1 AND `모듈` IN ('community.doctor:appointment:notice' , '커뮤니티.의사:이송.치료', 'community.doctor:이전.치료.지불', 'community.doctor:weiyi.guahao.to.user', 'community.doctor:weiyi.prescription.to.patient', 'community.doctor:user.buy.prescription') AND `상태` = 1 AND `record_global_id` < ) ORDER BY `record_global_id` DESC 제한 0, 20; <시간>

    만약 성공했다면 댓글로 실행 계획 스크린샷을 올려주실 수 있나요? 의심을 테스트하고 싶습니다. 감사합니다!

    회신하다
    0
  • ringa_lee

    ringa_lee2017-04-17 16:53:13

    创建复合索引(from_uid,to_uid,from_type,to_type,type >,상태,record_global_id)
    修改sql为union如下:

    <코드>에서 *를 선택합니다((SELECT 
        `레코드_글로벌_ID`,
        '유형',
        '마크',
        `from_uid`,
        `from_type`,
        `to_uid`,
        `to_type`,
        `보내기_방법`,
        '액션',
        '모듈',
        `보내기_시간`,
        '내용'
    에서
        `im_data_record`
    어디
    `from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3 AND `type` = 2 AND `qa_id` = 0 AND `status` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20
    ) 노동조합
    (선택하다 
        `레코드_글로벌_ID`,
        '유형',
        '마크',
        `from_uid`,
        `from_type`,
        `to_uid`,
        `to_type`,
        `보내기_방법`,
        '액션',
        '모듈',
        `보내기_시간`,
        '내용'
    에서
        `im_data_record`
    어디
    `from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1 AND `type` = 2 AND `qa_id` = 0 AND `status` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20
    ) 노동조합
    (선택하다 
        `레코드_글로벌_ID`,
        '유형',
        '마크',
        `from_uid`,
        'from_type',
        `to_uid`,
        `to_type`,
        `보내기_방법`,
        '액션',
        '모듈',
        `보내기_시간`,
        '내용'
    에서
        `im_data_record`
    어디
    `from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3 AND `type` = 3 AND `module` IN ('community.doctor:appointment:notice' , 
                                 '커뮤니티.의사:이송.치료',
                                 'community.doctor:이전.치료.지불',
                                 'community.doctor:weiyi.guahao.to.user',
                                 'community.doctor:weiyi.prescription.to.patient',
                                 'community.doctor:user.buy.prescription')
    AND `상태` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20
    )) aa ORDER BY `record_global_id` DESC LIMIT 0 , 20;

    如果根据from_uid,to_uid,from_type,to_type,type,status筛选的结果集较少的话,可union子查询中不사용加AND record_global_id < 5407938 ORDER BY record_global_id DESC LIMIT 0 , 20

    회신하다
    0
  • 취소회신하다