帮忙优化一下查询

sw47 2004-09-18 02:04:55
表结构
depid depname itemid itemname totalmoney reportdate docid docname
部门id 部门名称 项目id 项目名称 收入 日期 医生id 医生姓名

部门id对应部门名称 项目id对应项目名称 医生id对应医生名称
日期为该收入的日期

每条记录记录了某个部门的某医生在某天某项目上的收入数额

我的sql语句
rsxm1=STMT1.executeQuery("select distinct itemid,itemname from inhos_oareport where reportdate between '"+stime+"' and '"+etime+"' order by itemid");
rsxm2=STMT2.executeQuery("select distinct itemid,itemname from clinic_oareport where reportdate between '"+stime+"' and '"+etime+"' order by itemid");
rsks1=STMT3.executeQuery("select distinct depid,depname from inhos_oareport where reportdate between '"+stime+"' and '"+etime+"' order by depid desc");
rsks2=STMT4.executeQuery("select distinct depid,depname from clinic_oareport where reportdate between '"+stime+"' and '"+etime+"' order by depid desc");
rsys1=STMT5.executeQuery("select distinct docid,docname,depid from inhos_oareport where reportdate between '"+stime+"' and '"+etime+"' order by docid desc");
rsys2=STMT6.executeQuery("select distinct docid,docname,depid from clinic_oareport where reportdate between '"+stime+"' and '"+etime+"' order by docid desc");
rssy1=STMT7.executeQuery("select depid,itemid,docid,totalmoney from inhos_oareport where reportdate between '"+stime+"' and '"+etime+"' order by depid desc");
rssy2=STMT8.executeQuery("select depid,itemid,docid,totalmoney from clinic_oareport where reportdate between '"+stime+"' and '"+etime+"' order by depid desc");

写的很臭的,达人看了不爽尽管骂:D
inhos_oareport和clinic_oareport是俩个库里的2个表,表结构相同,查询的语句也相同

我想查得stime到etime时间段之间有多少不同的项目,不同的医生,不同的科室,已经这个时间段的收入情况
...全文
161 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zanglinfeng 2004-09-20
  • 打赏
  • 举报
回复
没有必要的时候最好不要用order by
joycool78 2004-09-20
  • 打赏
  • 举报
回复
select a.OperDep,b.depname,a.sortid,(case when a.sortid=2 then '西药费' when a.sortid=3 then '中药费'
else '草药费' end),sum(a.totalmoney),convert(char(10),feetime,20),a.docid,c.opername
from inhosfees a,dep_table b,opertable c
where a.OperDep=b.depid and a.sortid in (2,3,4) and a.Docid=c.operid
group by a.OperDep,b.depname,a.sortid,a.docid,c.opername,convert(char(10),feetime,20)
sw47 2004-09-20
  • 打赏
  • 举报
回复
select a.OperDep,b.depname,a.sortid,(case when a.sortid=2 then '西药费' when a.sortid=3 then '中药费'
else '草药费' end),sum(a.totalmoney),feetime,a.docid,c.opername
from inhosfees a,dep_table b,opertable c
where a.OperDep=b.depid and a.sortid in (2,3,4) and a.Docid=c.operid
group by a.OperDep,b.depname,a.sortid,a.docid,c.opername
想选feetime又不group by feetime怎么办
sw47 2004-09-19
  • 打赏
  • 举报
回复
我感觉慢的原因还有一个就是执行了多此查询所以多次访问了数据库
哪位大侠能帮我把结果写到一句sql里吗?
dnvodcwan 2004-09-19
  • 打赏
  • 举报
回复
第一:order by 字段建Index ,第二:在没有必要的时候最好不要用order by 特别不要order by DESC 因为Order by 是一条条记录比较的,
jimmyge 2004-09-19
  • 打赏
  • 举报
回复
其實rockyljt(Rocky)說的表結構,符合关系数据库的规范.呵呵
yyhyy23 2004-09-18
  • 打赏
  • 举报
回复
就现在的情况看,没什么好优化的,只有建立排序的索引了
zhangzs8896 2004-09-18
  • 打赏
  • 举报
回复
我也觉得是,这个表结构设计的不好,不符合关系数据库的规范。
---涛声依旧--- 2004-09-18
  • 打赏
  • 举报
回复
另建議你改一下表結構﹕
將表结构
depid depname itemid itemname totalmoney reportdate docid docname
部门id 部门名称 项目id 项目名称 收入 日期 医生id 医生姓名

分成四個表

表1:
depid depname
部门id 部门名称
表2:
itemid itemname
项目id 项目名称
表3:
docid docname
医生id 医生姓名
表4:
depid itemid totalmoney reportdate docid
部门id 项目id 收入 日期 医生id


查詢時再關聯四個表就行了
zhangzs8896 2004-09-18
  • 打赏
  • 举报
回复
你这样要得到多少个结果?
---涛声依旧--- 2004-09-18
  • 打赏
  • 举报
回复
把你經常要order by 的那些字段建上索引
31737951 2004-09-18
  • 打赏
  • 举报
回复
自己在查询分析器中用ctrl + L来分析你的语句不就可以了,不过库要连真实的库,这样才能给出你真实的信息,比如优化索引,优化查询条件什么的,光在这里说是没用的,没你的真实环境也无从优化,所以还是要你自己动手,呵呵
sw47 2004-09-18
  • 打赏
  • 举报
回复
inhos_oareport和clinic_oareport数据分别有n百万条

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧