怎样提高查询速度?

RealSmart 2005-12-30 10:14:40
后台是access数据库,执行如下数据查询:

string strSql="select count(*) AS 总数,sum(Times) AS 频次 from tbl01 where ID in (select distinct ID from tblAdd01 where Format like '"+strText+"') group by TheYear ";

tbl01中大概1.3万多条数据,tblAdd01中大概2.5万多条数据,字查询得到的ID数大概1千多条(也就是说最后的结果集为1千多条记录),结果进度条动的很慢,跑了很久网页还出不来。是不是在access数据库中建立查询速度会快?可是条件在子查询中,不知道怎么把条件值传进去……有没有更好的办法?谢谢!
...全文
107 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
RealSmart 2005-12-30
  • 打赏
  • 举报
回复
我说错了,最后count的就只有几条了……sorry,表达有误
xwjss 2005-12-30
  • 打赏
  • 举报
回复
第一页:
string strSql="select count(*) AS 总数,sum(Times) AS 频次 from tbl01 where ID in (

select distinct top pageSize(每页的记录数) ID from tblAdd01 where Format like '"+strText+"'

) group by TheYear ";
大于第一页:
string strSql="select count(*) AS 总数,sum(Times) AS 频次 from tbl01 where ID in (

select distinct top pageSize(每页的记录数) ID from tblAdd01 where Format like '"+strText+"' and ID not in (select distinct top pageSize(每页的记录数)*(pageIndex(页码)-1) ID from tblAdd01 where Format like '"+strText+"')

) group by TheYear ";
RealSmart 2005-12-30
  • 打赏
  • 举报
回复
2.SQL语句优化,不要用in:
select count(a.id) as 总数,sum(a.Times) as 频次 from tbl01 a,tblAdd01 b where a.id=b.id and b.Format like '"+strText+"' group by a.TheYear

不行啊,因为tblAdd01中ID不是唯一的,因此需要先distinct,不然累加的结果就是错的
luoxia001 2005-12-30
  • 打赏
  • 举报
回复
楼上说得不错
补充一下:
楼主你的程序不可能一页就把1000多条数据显示出来吧
所以最好每次只取每页显示的记录条数,如果一次性取出的话,就算再怎么优化,也是很慢的
wu896222 2005-12-30
  • 打赏
  • 举报
回复
1.tbl01表与tblAdd01表分别建立ID 字段的索引;
2.SQL语句优化,不要用in:
select count(a.id) as 总数,sum(a.Times) as 频次 from tbl01 a,tblAdd01 b where a.id=b.id and b.Format like '"+strText+"' group by a.TheYear
wu896222 2005-12-30
  • 打赏
  • 举报
回复
改为:
select count(a.id) as 总数,sum(a.Times) as 频次
from tbl01 a,(select distinct id from tblAdd01 where Format like '"+strText+"') b
where a.id=b.id
group by a.TheYear

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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