sql求优化

为啥呢 2013-01-22 03:19:52

select Min(datacode)
from(
select datacode from data where type='0'
)a
where
LEN(datacode)=(select max(len(datacode))from (
select datacode from data where type='0'
)a)

以上这句需求是从目标表获取列datacoe中值的长度最大的值中的最小值
例如从1 2 32 9 71 123 567 234获取123
我这样写执行效率很低,要进行2次聚集索引扫描。
请优化一下
...全文
151 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
为啥呢 2013-01-22
  • 打赏
  • 举报
回复
引用 3 楼 szm341 的回复:
select top 1 len(datacode) ld,min(len(datacode)) from data group by len(datacode) order by ld desc 主要是一般加了函数进行计算时都用不到索引,好像也没什么好优化的了
有没有其他方法呢?只要满足我的需求:获取列datacoe中值的长度最大的值中的最小值
szm341 2013-01-22
  • 打赏
  • 举报
回复
select top 1 len(datacode) ld,min(len(datacode)) from data group by len(datacode) order by ld desc 主要是一般加了函数进行计算时都用不到索引,好像也没什么好优化的了
为啥呢 2013-01-22
  • 打赏
  • 举报
回复
引用 1 楼 szm341 的回复:
SQL code?1234select Min(datacode) from data where type='0'and LEN(datacode)=(select max(len(datacode))from data) 这样执行多久呢
...那只是我贴sql过来时的一个疏忽,实际上type不等于0的数据量极少,基本不会影响性能。
szm341 2013-01-22
  • 打赏
  • 举报
回复

select Min(datacode) 
from data 
where type='0'
and LEN(datacode)=(select max(len(datacode))from data)
这样执行多久呢

22,210

社区成员

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

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