sql语句优化问题

弦弦 2011-09-30 03:37:20

SELECT soft_id,(SELECT COUNT(distinct(uc_softrecord.user_id))AS A FROM uc_softrecord where
uc_softrecord.soft_id='2' and uc_softrecord.id='511331') AS ClickPeoNumber,
(select uc_softrecord.click from uc_softrecord where uc_softrecord.soft_id='2' and uc_softrecord.id='511331') AS
ClickNumber,
(SELECT count(distinct(user_id) ) as DropNumber FROM uc_softrecord where soft_id='2' and uc_softrecord.`status`
in (1,2) and uc_softrecord.id='511331') AS DropNumber,
(Select count(distinct(user_id) ) AS num from uc_softrecord where uc_softrecord.`status`='2' and
uc_softrecord.soft_id='2' and uc_softrecord.id='511331') AS DropOkNumber
FROM uc_softrecord where uc_softrecord.soft_id='2' and uc_softrecord.id='511331' and uc_softrecord.createtime
between '2011-09-15' and '2011-09-30'


别说建索引,视图,什么的。。。就提论题,优化这个sql语句,
...全文
119 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
弦弦 2011-09-30
  • 打赏
  • 举报
回复
不太可能。因为uc_softrecord.soft_id 和uc_softrecord.id值是变化的。
xiangaylian 2011-09-30
  • 打赏
  • 举报
回复
每一列都是一个统计的数据,每一个统计的条件都不一样,楼主可能是想在方式方法上面去优化。
那join的话估计就很难做到了。
提议一种方式:
建立一个临时表,将uc_softrecord.soft_id='2' and uc_softrecord.id='511331'的所有查询结果先放到临时表中,然后上面的查询都从临时表中查询,那么可以去掉所有的uc_softrecord.soft_id='2' and uc_softrecord.id='511331'条件了。
xiangaylian 2011-09-30
  • 打赏
  • 举报
回复

SELECT soft_id,
(
SELECT COUNT(distinct(uc_softrecord.user_id))AS A FROM uc_softrecord
where uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
) AS ClickPeoNumber,
(
select uc_softrecord.click from uc_softrecord where uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
) AS ClickNumber,
(
SELECT count(distinct(user_id)) as DropNumber FROM uc_softrecord
where soft_id='2' and uc_softrecord.`status` in (1,2) and uc_softrecord.id='511331'
) AS DropNumber,
(
Select count(distinct(user_id)) AS num from uc_softrecord
where uc_softrecord.`status`='2' and uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
) AS DropOkNumber
FROM uc_softrecord
where uc_softrecord.soft_id='2' and uc_softrecord.id='511331' and uc_softrecord.createtime
between '2011-09-15' and '2011-09-30'

-------------------------------------------------------------------
写成这样是否会清晰点??
  • 打赏
  • 举报
回复
laowang134 2011-09-30
  • 打赏
  • 举报
回复
mysql!~不会!·
弦弦 2011-09-30
  • 打赏
  • 举报
回复
忘了说了 mysql数据库。
laowang134 2011-09-30
  • 打赏
  • 举报
回复
uc_softrecord.`status`这个是什么写法!~。。。。mysql??
  • 打赏
  • 举报
回复
from cte right join uc_softrecord on cte.user_Id=uc.user_id where
  • 打赏
  • 举报
回复
SELECT COUNT(distinct(uc_softrecord.user_id))AS A FROM uc_softrecord where
uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
这个查了多次完全没必要,改成join吧

2005或2008以上可以用CTE提取出来
;with cte
as
{
select distinct(uc_softrecord.user_id) AS A FROM uc_softrecord where
uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
}
select cte.soft_id,
(select Count(cte.A) AS ClickPeoNumber from cte INNER JOIN uc_softrecord as uc
on cte.user_Id=uc.user_id where uc_softrecord.`status`='1' or uc_softrecord.`status`
='2') AS DropNumber,
(select Count(cte.A) from cte where uc_softrecord.`status`='2') AS DropOkNumber ,
from right join uc_softrecord on cte.user_Id=uc.user_id where
uc_softrecord.createtime between '2011-09-15' and '2011-09-30'



laowang134 2011-09-30
  • 打赏
  • 举报
回复
有数据没!~。。。
弦弦 2011-09-30
  • 打赏
  • 举报
回复
SELECT soft_id,(SELECT COUNT(distinct(uc_softrecord.user_id))AS A FROM uc_softrecord where
uc_softrecord.soft_id='2' and uc_softrecord.id='511331') AS ClickPeoNumber,

我如果不加and uc_softrecord.id='511331' 这句话的话 mysql提示行数大于1,
laowang134 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nevermore_0923 的回复:]
uc_softrecord.id 这个是主键,
[/Quote]
如果你说的没错的话,uc_softrecord.id='511331'只有一条。那count出来的结果不都是1????
zk11223344 2011-09-30
  • 打赏
  • 举报
回复
(select uc_softrecord.click from uc_softrecord where uc_softrecord.soft_id='2' and uc_softrecord.id='511331') AS
ClickNumber
除了这一段都是聚集函数,难道不会报错?
弦弦 2011-09-30
  • 打赏
  • 举报
回复
uc_softrecord.id 这个是主键,
fangyuantdy 2011-09-30
  • 打赏
  • 举报
回复
表结构,想实现什么功能,看你的SQL语句理解业务有点困难啊
zk11223344 2011-09-30
  • 打赏
  • 举报
回复
where uc_softrecord.soft_id='2' and uc_softrecord.id='511331'
都确定了,那不是只有一条数据。。。
你是不是想按照soft_id进行分组统计?
zk11223344 2011-09-30
  • 打赏
  • 举报
回复
uc_softrecord表主键是什么?

62,046

社区成员

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

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

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

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