[英]When joining to a very small/empty table why MySQL makes a full scan in spite of I'm using "LIMIT"?
[英]Why does Mysql use a full table scan for table A when joining another table B?
我有一個表A和一個表B.我從表A中選擇信息,其中我只需要表A中的信息,其中tabel B連接表A.我有關於連接的列和WHERE子句的索引。
這是選擇代碼:
SELECT *
FROM tableA
INNER JOIN tableB AS tableB
ON tableB.id = tableA.id
AND tableB.type = 'car'
當我使用說明時,我可以看到mysql正在對tableA中的所有行使用全表掃描。 此外,它正確地使用索引來連接tableB。
所以Mysql似乎正在做的是掃描talbeA中的所有行,看看是否與tableB匹配。 但是因為我在兩個表的id上都有索引,所以我不希望表掃描。 由於我的表有50.000行,這個查詢需要幾秒鍾(這對我的應用來說真的太長了)。
這是解釋:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tableA ALL tableA.id NULL NULL 49898
1 SIMPLE tableB eq_ref tableB.type tableB.type 4 1 Use WHere
我的問題:如何優化此查詢和索引,以便Mysql直接從tableA中選擇那些與tableB匹配而不掃描talbeA中所有行的行?
這是您可以獲得JOIN查詢的最大優化,請參閱她以獲取更多詳細信息https://cryptkcoding.com/blog/2012/04/06/how-to-optimize-mysql-join-queries-through-indexing/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.