帮忙优化下SQL

huyou1983218 2011-02-28 05:26:18

SELECT DISTINCT Top (100) Percent rd.cell_index,rd.freq_id,rd.data_range_id,rd.org_id,convert(float,rd.curr_term_value) AS curr_term_value, rd.cur_id ,rd.rept_year,rd.rept_month,rd.template_id,rd.version_id, rd.[status],rd.row_id, rd.col_id,dbo.ReplaceX( ci.cell_cn_name,' | 本外币合计') as cell_cn_name,ci.cell_cn_name as cell_cn_name2
FROM report_data rd INNER JOIN cell_info ci ON rd.cell_index=ci.cell_index AND rd.template_id='G0100' AND rd.version_id = (select max(version_id) from template_info where template_id='G0100' and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102))
AND ci.col_id = 'F' AND ci.row_id>=(SELECT MAX(row_id)
FROM cell_info ci WHERE ci.cell_index='G01000690F6' AND ci.version_id=(select max(version_id) from template_info where template_id='G0100'
and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102)))
AND ci.row_id<(select max(row_id) from cell_info where cell_index='G01000690F62' and version_id=
(select max(version_id) from template_info where template_id='G0100'
and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102)))
AND ci.cell_cn_name NOT LIKE '%[0-9].[0-9]%'
AND rd.rept_year=2011 AND rd.rept_month=1 AND rd.data_range_id=1
AND rd.org_id='F104H101520301001'

这个查询结果没多少条记录,查询出的记录也就25条,report_data里有4w多条记录,cell_info里有4万多条,template_info里有几百条记录。
但是查的时间却需要13秒
谁能帮忙分析下,
忘记说了,不能使用存储过程,但是可以使用函数,这个sql语句是存在数据库里的记录。
...全文
150 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ILOVE_ASPNET 2011-02-28
  • 打赏
  • 举报
回复
首先我感觉有的地方是不需要CONVERT的, 你看你的条件中有并且 有或者,还有模糊查询,还有聚合物这些条件能不能整理下,为什么要TOP 100还要 DISTINCT 去掉重复?
Xiao_Ai_Mei 2011-02-28
  • 打赏
  • 举报
回复
建议把 getdatew()贴出来,看怎么写的?
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
主要我感觉奇怪的是如果时间换成2010年12月份 ,查询时间就不到一秒就查出来了
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。

(25 行受影响)
表 'cell_info'。扫描计数 248,逻辑读取 178184 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
表 'report_data'。扫描计数 1,逻辑读取 9668 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
表 'template_info'。扫描计数 3,逻辑读取 6 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。

SQL Server 执行时间:
CPU 时间 = 13104 毫秒,占用时间 = 13191 毫秒。
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。

SQL Server 执行时间:
CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
AcHerat 2011-02-28
  • 打赏
  • 举报
回复
我也没什么想法,主要是多次调用函数,效率肯定上不去。
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xuam 的回复:]

能不能不用 convert.....

[/Quote]
不用convert 那用什么?cast?
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
我在写个你就明白了


select row_number()over(order by tb.curr_term_value desc) as rownum,tb.cell_cn_name from (
SELECT DISTINCT Top (100) Percent rd.cell_index,rd.freq_id,rd.data_range_id,rd.org_id,convert(float,rd.curr_term_value) AS curr_term_value, rd.cur_id ,rd.rept_year,rd.rept_month,rd.template_id,rd.version_id, rd.[status],rd.row_id, rd.col_id,dbo.ReplaceX( ci.cell_cn_name,' | 本外币合计') as cell_cn_name,ci.cell_cn_name as cell_cn_name2
FROM report_data rd INNER JOIN cell_info ci ON rd.cell_index=ci.cell_index AND rd.template_id='G0100' AND rd.version_id = (select max(version_id) from template_info where template_id='G0100' and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102))
AND ci.col_id = 'F' AND ci.row_id>=(SELECT MAX(row_id)
FROM cell_info ci WHERE ci.cell_index='G01000690F6' AND ci.version_id=(select max(version_id) from template_info where template_id='G0100'
and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102)))
AND ci.row_id<(select max(row_id) from cell_info where cell_index='G01000690F62' and version_id=
(select max(version_id) from template_info where template_id='G0100'
and convert(varchar(10),start_date,102)<=convert(varchar(10),dbo.getdatew(2011,1),102)
and convert(varchar(10),dbo.getdatew(2011,1),102)<=convert(varchar(10),end_date,102)))
AND ci.cell_cn_name NOT LIKE '%[0-9].[0-9]%'
AND rd.rept_year=2011 AND rd.rept_month=1 AND rd.data_range_id=1
AND rd.org_id='F104H101520301001') tb
where ......

xuam 2011-02-28
  • 打赏
  • 举报
回复
能不能不用 convert.....
Xiao_Ai_Mei 2011-02-28
  • 打赏
  • 举报
回复
Top (100) Percent 有什么意义呢?
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
只要是Where条件内字段都加了索引,还有单个字段加了索引,组合字段也加了索引,无论怎么读都是13秒
huyou1983218 2011-02-28
  • 打赏
  • 举报
回复
没办法,这个外面程序需要拼Where条件,所有如果要排序就要加上Top (100) Percent
feixianxxx 2011-02-28
  • 打赏
  • 举报
回复
...链接字段 谓词字段加上合适索引...
对语句写法 我没想法
feixianxxx 2011-02-28
  • 打赏
  • 举报
回复
Top (100) Percent ....

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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