2017-01-01 77 views
0

你有什麼想法,我怎麼能加快下面的查詢?我怎樣才能加速這一MySQL查詢(計數明顯與加盟)

select count(distinct t1.var1) 
from table_a t1 
join table_b t2 on t1.var2 = t2.var1 
    and t2.date between '2016-11-05 00:00:00' and '2016-11-10 23:59:59' 
    and t2.var3 = 3; 

我已經試過這樣:

select count(*) 
from (select distinct t1.var1 from table_a t1 
join table_b t2 on t1.var2 = t2.var1 
    and t2.date between ('2016-11-05 00:00:00') and ('2016-11-10 23:59:59') 
    and t2.var3 = 3) as temp 

,但它並不比前一個

+1

什麼'EXPLAIN'之前應用fiters說? – Barmar

+1

見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-查詢。另外,關於查詢的性能問題,總是有一個EXPLAIN – Strawberry

+1

這就是說,在3分T2列覆蓋索引似乎是一個不錯的主意 – Strawberry

回答

-2
select count(distinct(t1.var1)) 
from table_a t1 
join (select * from table_b where date between ('2016-11-05 00:00:00') and ('2016-11-10 23:59:59') and var3 = 3) t2 
on t1.var2 = t2.var1; 

快執行聯接

+0

爲什麼要這樣做? – Strawberry

+0

從表而不是選擇所有的數據只選擇需要的數據,然後加入它 – prashant

+0

哇!這大約快3倍! – marmas01

相關問題