【问题标题】:Query is running too slow?查询运行太慢?
【发布时间】:2021-09-24 03:39:59
【问题描述】:

我正在编写一个带有左连接的 MySQL 查询,当我删除并使用左连接条件时,它会在 28 秒内给我结果,然后它会在一秒钟内工作,谁能告诉我查询中的问题是什么以及它将如何修改了吗?

  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 查询计划:

【问题讨论】:

  • Explain of command 会很有用。
  • 我已经用查询计划图像更新了我的查询。

标签: mysql


【解决方案1】:

你的目的对我来说不是很清楚。例如加入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

没有真实数据很难评估性能。

【讨论】:

  • 您能否按照您的建议更改整个查询。
  • select * FROM regist_queue rq left join 在 rq.token_number = ap.daily_ticket_no 和 rq.LocationId = ap.LocationId 和 date(rq.QueueDate) = CURRENT_DATE() 和 date(ap.dAppDate ) = date(now()) left join 患者 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 和 (ap.LocationId = 15800) 按 rq.token_number asc 排序;
  • 或者您可能想要更改 where 条件如下(ap.LocationId = 15800 或 rq.LocationId = 15800)
  • 谢谢@Lakshitha,我的问题已经用你给定的解决方案解决了。
【解决方案2】:

在sql查询中使用SQLwith (nolock)

喜欢:

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;

【讨论】:

  • 没有锁给我语法错误。接近预期输入结尾的语法不正确。
  • 我阅读了它并为 mysql 实现了 SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 但它需要相同的时间来执行我用查询计划图像更新了我的问题。
【解决方案3】:

好像有笛卡尔连接....

基于三个谓词以 on .........开头,

我认为sql语句不是基于你的真实目的...

【讨论】:

  • 这是一个答案吗?
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多