日期:2014-05-18  浏览次数:20760 次

如何优化查询??
有一个视图useraa:
SELECT   DATEADD([day],   SIGN(DATEDIFF(d,   dbo.personal1.u_lastlogintime,   GETDATE())  
            /   28)   *   28,   dbo.personal1.u_lastlogintime)   AS   b_lastlogintime,  
            dbo.personal1.u_web,   dbo.personal1.u_regtime,   b.fashion
FROM   dbo.user_money   b   RIGHT   OUTER   JOIN
            dbo.personal1   ON   b.userid   =   dbo.personal1.u_name   AND   b.id   =
                    (SELECT   TOP   1   id
                  FROM   dbo.user_money
                  WHERE   (from_date   <   GETDATE())   AND   (to_date   >   GETDATE())   AND   (state   =   ' ')   AND  
                              ((manner   =   '时间 ')   OR
                              ((manner   =   '点击 ')   AND   (balance   >   0)))   AND   userid   =   b.userid


然后用查询:
select   dbo.useraa.b_lastlogintime,   dbo.useraa.u_college,  
            dbo.useraa.id,   dbo.useraa.u_name,   dbo.useraa.u_sex,
                      dbo.useraa.u_vocation1,   dbo.useraa.u_vocation2,  
            dbo.useraa.u_lastlogintime   from   dbo.useraa   LEFT   OUTER   JOIN
            dbo.c_order   ON   dbo.useraa.fashion   =   dbo.c_order.set_user   AND  
              dbo.c_order.c_search   =0   and   dbo.c_order.com_user= '个人 '   ORDER   BY   dbo.c_order.corder,convert(varchar(10),dbo.useraa.   b_lastlogintime,120)   DESC,dbo.useraa.u_update   DESC


速度很慢,如何优化???


------解决方案--------------------
--try:
select U.b_lastlogintime, U.u_college, U.id, U.u_name, U.u_sex, U.u_vocation1, U.u_vocation2, U.u_lastlogintime
from dbo.useraa as U LEFT OUTER JOIN
(Select corder from dbo.c_order where com_user= '个人 ' and c_search =0) as C
ON U.fashion = C.set_user
ORDER BY C.corder,convert(varchar(10),U. b_lastlogintime,120) DESC,U.u_update DESC

--try2:
select U.b_lastlogintime, U.u_college, U.id, U.u_name, U.u_sex, U.u_vocation1, U.u_vocation2, U.u_lastlogintime
from dbo.useraa as U LEFT OUTER JOIN
(Select corder from dbo.c_order where com_user= '个人 ' and c_search =0) as C
ON U.fashion = C.set_user
ORDER BY C.corder, U. b_lastlogintime DESC, U.u_update DESC
------解决方案--------------------
这么快!!
------解决方案--------------------
自悟,呵呵
――――――――――――――――――――――――分页与分次取出
--页码i=2
--页数j=4
declare @c int
set @c = (select count(*) from tablea)
declare @a int
declare @b int
set @a = 1
set @b = @c/4--想分的页数
set @a = @b+1--该页首条
set @b = @b*2--页码--该页未条
declare @sql varc