mysql查询运行太慢?

rqdpfwrv  于 2021-06-25  发布在  Mysql
关注(0)|答案(3)|浏览(419)

我正在用left join编写一个mysql查询,它在28秒内给出结果当我用left join删除并设置条件时,它在一秒钟内工作。有人能告诉我查询中的问题是什么以及如何修改它吗?

select *  
  FROM regist_queue rq
  left join appoint ap
  on rq.token_number = ap.daily_ticket_no 
  and rq.LocationId = 15800
  and ap.LocationId = 15800
  and date(rq.QueueDate) = CURRENT_DATE()
  and date(ap.dAppDate) = date(now())
  left join patient pr
  on ap.iPatID = pr.IPatID
  left join gender ge
  on pr.vGender = ge.iGenderID
  where ifnull(ap.isDel,0) = 0
  and ifnull(ap.is_referred,0) != 1
  and (ap.LocationId  = 15800 or rq.LocationId = 15800 )  
  order by rq.token_number asc;

我还对所有搜索到的参数和应用连接的位置应用了索引。解释查询计划。mysql查询计划

vs3odd8k

vs3odd8k1#

使用sql with (nolock) 在sql查询中
比如:

select *  
  FROM regist_queue rq with (nolock)
  left join appoint ap with (nolock)
  on rq.token_number = ap.daily_ticket_no 
  and rq.LocationId = 15800
  and ap.LocationId = 15800
  and date(rq.QueueDate) = CURRENT_DATE()
  and date(ap.dAppDate) = date(now())
  left join patient pr with (nolock)
  on ap.iPatID = pr.IPatID
  left join gender ge with (nolock)
  on pr.vGender = ge.iGenderID
  where ifnull(ap.isDel,0) = 0
  and ifnull(ap.is_referred,0) != 1
  and (ap.LocationId  = 15800 or rq.LocationId = 15800 )  
  order by rq.token_number asc;
dzjeubhm

dzjeubhm2#

我不太清楚你的目的。例如在join中 and rq.LocationId = 15800 and ap.LocationId = 15800 在where子句中 and (ap.LocationId = 15800 or rq.LocationId = 15800 ) 我的建议是有这样的东西。
在左连接中 rq.LocationId = ap.LocationId 在where子句中 ap.LocationId = 15800 没有真实的数据就很难评估性能。

kh212irz

kh212irz3#

似乎有笛卡尔连接。。。。
基于三个 predicate ,以on……..开头。。。。。。。。。,
我认为sql语句并不是基于您的真正目的。。。

相关问题