select COUNT(b.register_no)
from table1 a
INNER JOIN table2 b ON a.register_no = b.register_no
INNER JOIN table3 c ON a.register_no = c.register_no
AND b.card_no = c.card_no;
The SQL statement takes 3 minutes to execute,Table1 has 2 million data,Table2 has 2.5 million data,Table3 has 4 million data,
explain:
table type Extra
tabel2 index Using where; Using index
table3 ref Using where
table1 eq_ref Using index
a: INDEX(register_no)
b: INDEX(register_no, card_no)
c: INDEX(register_no, card_no)
Please provide SHOW CREATE TABLE
for each table.
Be aware that when JOINing
a COUNT
may be inflated. This is because the JOIN
occurs before the COUNT
. (learning's suggestion about EXISTS
may give smaller numbers.)
Is there some reason to check that b.register_no
is NOT NULL
? That is what COUNT(b.register_no)
implies. Probably COUNT(*)
is sufficient.
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.