---双色球当期开奖号码2010118   05 08 11 13 15 25 + 05  
  ---条件一:查询表[SSBallDB].[dbo].[BaseDT]的数据(数据有1107568条) 与这组指定号码有(RpNumber  between 0 and 3)几个相同的结果
Select ID as RID,
((Case when NO1 in ('05','08','11','13','15','25') then 1 else 0 end)+ 
 (Case when NO2 in ('05','08','11','13','15','25') then 1 else 0 end)+
 (Case when NO3 in ('05','08','11','13','15','25') then 1 else 0 end)+
 (Case when NO4 in ('05','08','11','13','15','25') then 1 else 0 end)+
 (Case when NO5 in ('05','08','11','13','15','25') then 1 else 0 end)+
 (Case when NO6 in ('05','08','11','13','15','25') then 1 else 0 end)) as RpNumber Into #RepTable 
From [SSBallDB].[dbo].[BaseDT]  ---条件二:查询伪码数WSum符合( Select ID From #WMNumber Where WSum Between 34 and 62)的结果
-----WNo对应的值 ---01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
-----WValue对应的值--2  11  9   8   1   13  8   1   3   3   1    9  1   16  1   17   5   6  14  2   14  2   11  2   1   3   9   2   4   7   14  10  4
Select ID,NoText,RText,RepNumber
,((Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No1=WNO)
+ (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No2=WNO)
+ (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No3=WNO)
+ (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No4=WNO)
+ (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No5=WNO)
+ (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No6=WNO)) as WSum Into #WMNumber   From [SSBallDB].[dbo].[BaseDT]    
Where 1=1 
and No1 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
and No2 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
and No3 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
and No4 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 11 ) 
and No5 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 11 ) 
and No6 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 3 and 14 ) ---条件三:查询号码QYNo所在区域QYWValue的和值符合(Select ID From #QYNumber Where QYSum Between 4 and 20)的结果 
----区域设置
 --在1区的号码QYNo--  '01', '06', '11', '16','21','26' 
 --在2区的号码QYNo--  '02', '07', '12', '17','22','27','31' 
 --在3区的号码QYNo--  '03', '08', '13', '18','23','28','32' 
 --在4区的号码QYNo--  '04', '09', '14', '19','24','29','33' 
 --在5区的号码QYNo--  '05', '10', '15', '20','25','30'
---建立数据表是 [SSBallDB].[dbo].[QYNumber]
---QYNo对应的值   --01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
---QYWValue对应的值--1  2   3   4   5   1   2   3   4    5  1    2   3   4   5  1   2   3   4   5   1   2   3   4   5   1   2   3   4   5    2   3  4
Select ID,NoText,RText,RepNumber
, ((Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber]Where No1=QYNo)
+ (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No2=QYNo)
+ (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No3=QYNo)
+ (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No4=QYNo)
+ (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No5=QYNo)
+ (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] WhereNo6=QYNo)) as QYSum Into #QYNumber   From [SSBallDB].[dbo].[BaseDT]    
Where 1=1 
and No1 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('2','4','5'))  
and No2 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  in     ('1','2','4','5'))  
and No3 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  in     ('1','2','3','4','5'))  
and No4 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('2','5'))
and No5 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('3','4','5')) 
and No6 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  in ('1','2','3','4','5'))   ---条件四:过滤掉不可能出现的号码(这些数据对应的号码),数据有1600多条,这里省略了大部分。
Select ID as PID into #PTable    From [SSBallDB].[dbo].[BaseDT]    Where RText 
 in('225 373 185 36 6 0 0','226 335 214 49 1 0 0','220 355 183 57 10 0 0','232 368 184 39 2 0 0','228 353 199 43 2 0 0','221 364 189 45 6 0 0','220 348 201 51 5 0 0','224 334 203 61 3 0 0','196 393 186 43 7 0 0','242 331 206 41 5 0 0','234 369 181 36 5 0 0','224 380 177 41 3 0 0','230 364 189 39 3 0 0','212 369 195 45 4 0 0','203 376 194 47 4 0 1','219 372 197 35 2 0 0','216 364 197 47 0 0 1','206 367 205 42 4 0 1','221 352 191 56 4 0 1','210 387 186 36 5 0 1','197 ) ---已经建立了数据表[SSBallDB].[dbo].[BaseDT] ,数据有1107568条。
Select ID,NoText as 号码,RepNumber as 组数,RText as 统计数据 , DDTotal as DD和值 , R45Total as R45和值 ,  R13Total as [R1-R3值], R17Total as  R17和值,TotalNo as 和值 ,Rap as 奇偶比 , Rat, CC1 From [SSBallDB].[dbo].[BaseDT]   
Where 1=1  
and (No1  between 7 and 7)    and  NO1 not in ('05', '10', '15', '20','25','30' )                           
and (No2  between 17 and 21)  and  NO2 not in ('04', '09', '14', '19')                                      
and (No3  between 25 and 30)  and  NO3 not in ( '05', '10', '15', '20','25','30')                           
and (No4  between 27 and 31)  and  NO4 not in ('05', '10', '15', '20','25','30')                            
and (No5  between 30 and 32)  and  NO5 in ('28')                                                           
and (No6  between 31 and 33)  and  NO6 not in ( '26')                                                      
and (No6-No5=1 and No5-No4>1  and No4-No3>1  and No3-No2>1  and No2-No1>1)
and( No6-No4-No5 between -29 and 0)                                                                         
and (R1  between 186 and 230)                                                                               
and (R2  between 360 and 396 )                                                                              
and (R3  between 180 and 235)                                                                               
and (R4  between 35 and 57)                                                                                 
and (R5  between 1 and 12)                                                                                  
and (R6  between 0 and 1)                                                                                   
and (R7 in(0))                                                                                                
and ( Rap= '0:6'OR Rap='1:5' OR Rap='2:4' OR Rap='3:3' OR Rap='4:2'OR Rap='5:1' OR Rap='6:0')              
and (CC1 between 3  and 24 )                                                                                
and (TotalNo  between 75 and 155 )                                                                          
and (R13Total  between -39 and 11  )                                                                        
and (R17Total in ('24','33','42','51','60','69') )                                                          
and (R45Total  between 33 and 77 )                                                                          
and (DDTotal  between 12 and 33 )                                                                           
and ID in (Select ID From #RepTable left join [SSBallDB].[dbo].[BaseDT] on                                  
#RepTable.RID=BaseDT.ID   Where 1=1                                                                         
and (RpNumber  between 0 and 3))                                                                            
and (RepNumber  between 1 and 19 )                                                                          
and not exists(Select 1 From #PTable Where #PTable.PID=ID)                                                  
and ID in( Select ID From #WMNumber Where WSum Between 34 and 62)                                           
and ID in( Select ID From #QYNumber Where QYSum Between 4 and 20)
Order by RepNumber                                                                                          
Drop Table #RepTable  Drop Table #WMNumber  Drop Table #QYNumber  Drop Table #PTable   问题说明,数据表[SSBallDB].[dbo].[BaseDT] 的数据有1107568条,执行以上语句得到查询结果,正常的情况下仅仅2分钟左右,可现在增加了“条件三”执行结果就要70-200分钟,效率太低,这些语句可以进一步优化吗?请教大侠修改。(方便的话请给我邮箱,我把这个原始文档发给你,我怕这样发的帖会是乱的而看不明白)    

解决方案 »

  1.   

      ---双色球当期开奖号码2010118   05 08 11 13 15 25 + 05  
      ---条件一:查询表[SSBallDB].[dbo].[BaseDT]的数据(数据有1107568条) 与这组指定号码有(RpNumber  between 0 and 3)几个相同的结果
    Select ID as RID,
    ((Case when NO1 in ('05','08','11','13','15','25') then 1 else 0 end)+ 
     (Case when NO2 in ('05','08','11','13','15','25') then 1 else 0 end)+
     (Case when NO3 in ('05','08','11','13','15','25') then 1 else 0 end)+
     (Case when NO4 in ('05','08','11','13','15','25') then 1 else 0 end)+
     (Case when NO5 in ('05','08','11','13','15','25') then 1 else 0 end)+
     (Case when NO6 in ('05','08','11','13','15','25') then 1 else 0 end)) as RpNumber Into #RepTable 
    From [SSBallDB].[dbo].[BaseDT]  ---条件二:查询伪码数WSum符合( Select ID From #WMNumber Where WSum Between 34 and 62)的结果
    -----WNo对应的值 ---01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
    -----WValue对应的值--2  11  9   8   1   13  8   1   3   3   1    9  1   16  1   17   5   6  14  2   14  2   11  2   1   3   9   2   4   7   14  10  4
    Select ID,NoText,RText,RepNumber
    ,((Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No1=WNO)
    + (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No2=WNO)
    + (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No3=WNO)
    + (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No4=WNO)
    + (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No5=WNO)
    + (Select top 1 WValue From [SSBallDB].[dbo].[WMNumber] Where No6=WNO)) as WSum Into #WMNumber   From [SSBallDB].[dbo].[BaseDT]    
    Where 1=1 
    and No1 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
    and No2 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
    and No3 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 17 ) 
    and No4 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 11 ) 
    and No5 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 1 and 11 ) 
    and No6 in(Select WNo From [SSBallDB].[dbo].[WMNumber] WHere WValue Between 3 and 14 ) ---条件三:查询号码QYNo所在区域QYWValue的和值符合(Select ID From #QYNumber Where QYSum Between 4 and 20)的结果 
    ----区域设置
     --在1区的号码QYNo--  '01', '06', '11', '16','21','26' 
     --在2区的号码QYNo--  '02', '07', '12', '17','22','27','31' 
     --在3区的号码QYNo--  '03', '08', '13', '18','23','28','32' 
     --在4区的号码QYNo--  '04', '09', '14', '19','24','29','33' 
     --在5区的号码QYNo--  '05', '10', '15', '20','25','30'
    ---建立数据表是 [SSBallDB].[dbo].[QYNumber]
    ---QYNo对应的值   --01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33
    ---QYWValue对应的值--1  2   3   4   5   1   2   3   4    5  1    2   3   4   5  1   2   3   4   5   1   2   3   4   5   1   2   3   4   5    2   3  4
    Select ID,NoText,RText,RepNumber
    , ((Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber]Where No1=QYNo)
    + (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No2=QYNo)
    + (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No3=QYNo)
    + (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No4=QYNo)
    + (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No5=QYNo)
    + (Select top 1 QYWValue From [SSBallDB].[dbo].[QYNumber] Where No6=QYNo)) as QYSum Into #QYNumber   From [SSBallDB].[dbo].[BaseDT]    
    Where 1=1 
    and No1 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('3','4' ))  
    and No2 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('1','5' ))  
    and No3 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('1','3' ))  
    and No4 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('3','5'))
    and No5 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('2','3' )) 
    and No6 in(Select QYNo From [SSBallDB].[dbo].[QYNumber] WHere QYWValue  not in ('3','4' ))   ---条件四:过滤掉不可能出现的号码(这些数据对应的号码),数据有1600多条,这里省略了大部分。
    Select ID as PID into #PTable    From [SSBallDB].[dbo].[BaseDT]    Where RText 
     in('226 335 214 49 1 0 0','220 355 183 57 10 0 0','232 368 184 39 2 0 0','228 353 199 43 2 0 0','221 364 189 45 6 0 0','220 348 201 51 5 0 0','224 334 203 61 3 0 0','196 393 186 43 7 0 0','242 331 206 41 5 0 0','234 369 181 36 5 0 0','224 380 177 41 3 0 0','230 364 189 39 3 0 0','212 369 195 45 4 0 0','203 376 194 47 4 0 1','219 372 197 35 2 0 0','216 364 197 47 0 0 1','206 367 205 42 4 0 1','221 352 191 56 4 0 1','210 387 186 36 5 0 1','197 368 194 60 5 0 1','201 362 206 52 3 1 0','211 357 212 41 4 0 0','202 393 183 44 3 0 0','215 350 214 39 6 0 1','198 360 215 48 3 0 1','209 358 215 37 5 0 1','219 369 187 46 3 0 1','232 338 199 48 7 0 1','202 371 189 58 4 0 1','220 367 188 44 5 0 1','232 351 192 41 8 0 1','241 352 194 34 3 0 1','210 385 189 36 4 0 1','229 321 224 47 3 0 1','207 381 189 43 3 1 1','234 344 197 46 3 0 1','215 352 204 49 4 0 1','204 370 206 38 6 0 1','236 352 202 31 4 0 0','219 358 201 43 3 0 1','247 341 199 34 4 0 0','222 345 203 53 1 0 1','231 351 194 43 5 1 0','225 341 200 51 7 0 1','217 361 191 51 4 0 1','214 348 208 50 4 0 1','206 350 202 58 7 1 1','218 358 197 42 8 1 1','222 361 193 44 5 0 0','217 361 185 53 8 1 0','206 369 202 44 4 0 0','225 369 179 45 6 0 1','219 372 188 39 6 1 0','237 353 190 41 3 0 1','214 355 211 41 4 0 0','213 358 205 43 5 0 1','241 336 192 47 8 0 1','222 363 189 42 8 0 1','215 383 180 40 6 0 1','215 374 181 47 6 1 1','209 341 209 61 4 0 1','217 373 181 46 7 0 1')  ---已经建立了数据表[SSBallDB].[dbo].[BaseDT] ,数据有1107568条。
    Select ID,NoText as 号码,RepNumber as 组数,RText as 统计数据 , DDTotal as DD和值 , R45Total as R45和值 ,  R13Total as [R1-R3值], R17Total as  R17和值,TotalNo as 和值 ,Rap as 奇偶比 , Rat, CC1 From [SSBallDB].[dbo].[BaseDT]   
    Where 1=1  
    and (No1  between 2 and 17)    and  NO1 not in ('05', '10', '15', '20','25','30' )                           
    and (No2  between  7 and 21)   and  NO2 not in ('04', '09', '14', '19')                                      
    and (No3  between 15 and 30)   and  NO3 not in ( '05', '10', '15', '20','30')                           
    and (No4  between 17 and 31)   and  NO4 not in ('05', '10', '15', '20','25','30')                            
    and (No5  between 20 and 32)   and  NO5 in ('30')                                                           
    and (No6  between 21 and 33)   and  NO6 not in ( '26')                                                      
    and (No6-No5=1 )  ---and No5-No4>1  and No4-No3>1  and No3-No2>1  and No2-No1>1
    and( No6-No4-No5 between -29 and 0)                                                                         
    and (R1  between 186 and 230)                                                                               
    and (R2  between 360 and 396 )                                                                              
    and (R3  between 180 and 235)                                                                               
    and (R4  between 35 and 57)                                                                                 
    and (R5  between 1 and 12)                                                                                  
    and (R6  between 0 and 1)                                                                                   
    and (R7 in(0))                                                                                                
    and ( Rap= '0:6'OR Rap='1:5' OR Rap='2:4' OR Rap='3:3' OR Rap='4:2'OR Rap='5:1' OR Rap='6:0')              
    and (CC1 between 3  and 24 )                                                                                
    and (TotalNo  between 75 and 155 )                                                                          
    and (R13Total  between -39 and 11  )                                                                        
    and (R17Total in ('24','33','42','51','60','69') )                                                          
    and (R45Total  between 33 and 77 )                                                                          
    and (DDTotal  between 12 and 38 )                                                                           
    and ID in (Select ID From #RepTable left join [SSBallDB].[dbo].[BaseDT] on                                  
    #RepTable.RID=BaseDT.ID   Where 1=1                                                                         
    and (RpNumber  between 0 and 3))                                                                            
    and (RepNumber  between 1 and 19 )                                                                          
    and not exists(Select 1 From #PTable Where #PTable.PID=ID)                                                  
    and ID in( Select ID From #WMNumber Where WSum Between 34 and 62)                                           
    and ID in( Select ID From #QYNumber Where QYSum Between 4 and 20)
    Order by RepNumber                                                                                          
    Drop Table #RepTable  Drop Table #WMNumber  Drop Table #QYNumber  Drop Table #PTable   
      

  2.   

    可以把[dbo].[QYNumber]设定QYID为主键,新建索引“IX_QYNumber”添加QYWValue,[IX_QYNumber_1]添加[QYNO],这样就可以提高执行效率