I have 2 tables, table A
& table B
.
Table A
(has thousands of rows)
Table B
(has a max of hundred rows)
I am trying to get the best join query to obtain a count with a WHERE
clause. I need the count of distinct created_by
s from table A
with an org_name
in Table B
that contains 'myorg'. I currently have the below query (producing expected results) and wonder if this can be optimized further?
select count(distinct a.created_by)
from a left join
b
on a.org_id = b.org_id
where b.org_name like '%myorg%';
You don't need a left join
:
select count(distinct a.created_by)
from a join
b
on a.org_id = b.org_id
where b.org_name like '%myorg%'
For this query, you want an index on b.org_id
, which I assume that you have.