mysql中a b为什么是假,当联接另一个表B时,为什么Mysql对表A使用全表扫描?

I have a table A and a table B. I am selecting information from table A where I need only the information from table A where tabel B joins table A. I have indexes on columns that are joint and on the WHERE clause.

This is the select code:

SELECT *

FROM tableA

INNER JOIN tableB AS tableB

ON tableB.id = tableA.id

AND tableB.type = 'car'

When I use explain I can see that mysql is using a full table scan for all the rows in tableA. Further it properly uses the index to join tableB.

So what Mysql seems to be doing is scan all the rows in talbeA to see if there is a match with tableB. However as I have indexes on id of both tables I don't expect a tablescan. As my table has 50.000 rows this query is taking a few seconds (which is really too long for my application).

This is the explain:

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

My question: How can I optimize this query and indexes such that Mysql directly selects those rows from tableA that have a match with tableB without scanning all rows in talbeA?

解决方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值