日期:2014-05-17  浏览次数:20994 次

帮忙看看这个SQL怎么优化一下?谢谢了
  select A =
         (select COUNT(*)from Activity where CrtUserID = 178864 and State = 0) *
         (select CKey1 from Category where CategoryID = 1004) +
         ((select COUNT(*) from ActivityCmt where CrtUserID = 178864 and State = 0) +  (select COUNT(*) from TopicCmt where CrtUserID = 178864 and State = 0) +(select COUNT(*) from PhotoCmt where CrtUserID = 178864 and State = 0)) *
         (select CKey1 from Category where CategoryID = 1006) +
         ((select COUNT(*) from ActivityPhotos where CrtUserID = 178864 and State = 0) + (select COUNT(*) from Photos where CrtUserID = 178864 and State = 0)) *
         (select CKey1 from Category where CategoryID = 1015) +
         (select COUNT(*) from Topic where CrtUserID = 178864 and State = 0) *
         (select CKey1 from Category where CategoryID = 1001) +
         (select COUNT(*)from Tweet  where CrtUserID = 178864 and State = 0) *
         (select CKey1 from Category where CategoryID = 1002)


谢谢各位搭救了~~
------解决方案--------------------
一堆没任何联系的表 怎么优化?

sql是不能下手 只能从其他方面考虑了...
------解决方案--------------------
引用:
Quote: 引用:

离开了表结构,索引字段,数据分布和业务要求的优化,都是耍流氓

这~~什么意思?不懂~~


sql优化说白了是算法针对当前业务逻辑和数据分布的一种固化算法
离开了这些,剩下能做的就不多了,无非就是建建索引,建建分区表什么的
------解决方案--------------------
这种计算类的语句不可能优化。
楼主应该也是编程的,可以像程序一样写成函数。
------解决方案--------------------
这个对where后面的字段做好索引就是优化