带有 where 子句的 SQL 内连接在一种条件下工作速度很快,但在添加 activeFlag 时则不然

问题描述 投票:0回答:1

我有这样的疑问

select C.customerId, C.firstName, C.LastName, R.IsActive from
Customer as C
Inner join Rider as R ON R.customerId = C.customerid
where R.rideid = 'xyz' and
R.rideareacode = 'abc' and 
R.isActive = 1

这需要 90 秒或更长时间。但如果我删除 R.IsActive = 1 条件查询只需要一秒钟。 IsActive 只有两个值 1 或 0,而且 Customer 和 Rider 表很大(80k 行)

我尝试对 Rider 表的 IsActive 列建立索引,但查询仍然花费太多时间,我也尝试建立索引(rideid、rideareacode、isActive),但仍然花费太长时间。

Rider 表的 Index_keys :

IsActive (nonclustered)
rideid, rideareacode, isActive, userid (nonclustered, unique)
riderid (nonclustered, unique, primary key)

customers 表没有索引。

sql mysql indexing query-optimization
1个回答
0
投票

R
需要
INDEX(rideid, rideareacode, isActive)
,各列的顺序任意。

更好的是这个“覆盖索引”:

INDEX(rideid, rideareacode, isActive,  -- in any order
      customerId)                      -- after the orhers
© www.soinside.com 2019 - 2024. All rights reserved.