I want to select from a table and then make select for each of them.
tables:
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
now the question is how can i list categories and count of emails in them with minimum runtime? my try is waiting 20sec for 200000 email and 20 category.
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
This is your query:
SELECT c.*,
(SELECT COUNT(DISTINCT sc.userId)
FROM subscriber_category sc JOIN
email e
ON e.id = sc.userId JOIN
domain d
ON d.id = e.domainId
WHERE sc.categoryId = c.id AND e.blackList = 0 AND d.blackList = 0
) AS qty
FROM category c
WHERE c.userId = 1
ORDER BY c.title ASC;
The structure is quite reasonable and indexes should help performance. The first index is on category(userId, title, id)
. This index should be used for the WHERE
clause, the ORDER BY
, and the correlated subquery.
Next, I assume that you have indexes on the id
columns in email
and domain
. You could make these slightly more applicable to the query if you include the blacklist
flag as a second column in the index. More importantly, you want an index on subscriber_category(categoryId, userId)
. I would also recommend removing the count(distinct)
if it is not necessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.