테이블에서 선택하고 각각에 대해 선택하려고합니다.최소 런타임으로 SQL select 쿼리를 실행하는 방법
테이블 :
category
+====+=======+
| id | title |
+====+=======+
this table has list of category
email
+====+=======+==========+=============+
| id | eMail | domainId | elseColumns |
+====+=======+==========+=============+
this table has list of emails but domains are in another table
domain
+====+========+=============+
| id | domain | elseColumns |
+====+========+=============+
list of domains which used in email
subscriber_category
+========+============+
| userId | categoryId |
+========+============+
list of emails in categories
이제 문제는 내가 최소한의 런타임로에 이메일의 종류와 수를 나열 할 수 있습니다 어떻게? 내 시도는 200000 개의 이메일과 20 개의 카테고리에 대해 20 초를 기다리고 있습니다.
SQL :
SELECT category.*,
(SELECT COUNT(DISTINCT subscriber_category.userId) FROM subscriber_category
JOIN email ON email.id=subscriber_category.userId
JOIN domain ON domain.id=email.domainId
WHERE subscriber_category.categoryId=category.id
AND email.blackList=0
AND domain.blackList=0
) AS qty
FROM category WHERE category.userId=1 ORDER BY category.title ASC
맙소사 !!!! 귀하의 기록은'1.0499 초 '입니다. 내가 말한대로 색인을 변경했습니다. 내가 테이블들 사이에 관계를 설정하면 그건 존경인가? – Pooya
@Pooya. . . 나는 당신의 코멘트에있는 질문을 이해하지 못합니다. –
내가 테이블의 외래 키를 설정하면 도움이되는지 알고 싶습니까? 즉 :'email.domainId to domain.id' – Pooya