萍水拾光 2022-03-03 14:20 采纳率: 50%
浏览 68
已结题

mysql关联模糊查询时,加入排序变得很慢

从表A模糊查询出结果,能够再B表按条件查询出的结构中找到的数据
下面是sql语句:
select t1.showCode, t1.Name, t1.Sex, t.AppointmentDate from (select *from A where AppointmentDate >= date_sub(current_date(), interval '10' day) and isFinish = 0 ) t
inner join (select *from B where (Name = '%1111%' or showCode like '%1111%') and HospitalID = '20220302160409') t1 on t.PatientID = t1.PatientID order by t.AppointmentDate desc;
这样在结尾加上 order by t.AppointmentDate desc排序语句就变得非常慢,比不加排序语句相差几十倍,请问要如何优化啊?

补充下explain结果如图:

img

  • 写回答

5条回答 默认 最新

  • zhang.yao 新星创作者: Java技术领域 2022-03-03 14:34
    关注

    表的数据量有多大 排序字段加个索引试试

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月6日
  • 修改了问题 3月3日
  • 创建了问题 3月3日