Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查询超时增加,但脚本无法执行_Sql_Sql Server - Fatal编程技术网

Sql 查询超时增加,但脚本无法执行

Sql 查询超时增加,但脚本无法执行,sql,sql-server,Sql,Sql Server,当我在远程数据库上执行此脚本时,它会给我查询超时错误。我已增加了数据库的超时时间,但仍有此错误。有人告诉我,如果我能够优化脚本使其变得简单,它可能会工作 SELECT TOP 8 MIN( CASE WHEN pic_alb_love.pic=users_pics.pic AND pic_alb_love.email = 'try@mail.com' THEN 'User' ELSE 'Guest' END)AS answer_one, MIN ( CASE WHEN f

当我在远程数据库上执行此脚本时,它会给我查询超时错误。我已增加了数据库的超时时间,但仍有此错误。有人告诉我,如果我能够优化脚本使其变得简单,它可能会工作

SELECT TOP 8 MIN( CASE WHEN pic_alb_love.pic=users_pics.pic  

AND pic_alb_love.email =  'try@mail.com' THEN 'User'     ELSE 'Guest'   END)AS answer_one, 

MIN ( CASE WHEN favorites.pic=users_pics.pic  AND favorites.email = 'try@mail.com'  THEN 'good' ELSE 'Bad' 
  END)AS answer2, 

  (CASE WHEN RTRIM (users_pics.upload_type) = 'wow'  THEN 'loaded' ELSE        

  CASE WHEN RTRIM (users_pics.upload_type)= 'hey' THEN 'added' ELSE       

  CASE WHEN RTRIM (users_pics.upload_type) = 'check'  THEN   'Changed'   END END END)as up_ans,     

  (CASE WHEN RTRIM (users_pics.upload_type)  = 'sample1' THEN 'new'  ELSE    

  CASE WHEN RTRIM (users_pics.upload_type)  = 'sample2' THEN 'existing'       ELSE   

  CASE WHEN RTRIM (users_pics.upload_type)  = 'sample3' THEN 'Profile Picture'       END END END) as exs, 

  COUNT(DISTINCT users_pics.pic) as total,RTRIM (users_pics.wardrobe) as wardrobe, 



 fname,users_pics.wardrobe,

  MIN (make)as make,MIN (htags)as htags,  RTRIM (profile.profile_id) as profile_id,

  users_pics.email,profile.profile_pix, RTRIM (profile.gender) as gender, 

  users_pics.time_group,profile.fpage,up_user_id, MIN (u_pic_id) as u_pic_id, MIN (users_pics.pic) as pic
FROM users_pics 

LEFT join profile on users_pics.email = profile.email  

LEFT join favorites on users_pics.pic = favorites.pic  

LEFT JOIN pic_alb_love on users_pics.pic = pic_alb_love.pic 

left join friends on users_pics.email = friends.resp_email


WHERE req_email = 'try@mail.com' and pic_enable='enable' or pic_view='Public'  

GROUP BY users_pics.upload_type,profile.fname,profile.profile_id,users_pics.wardrobe, 

users_pics.email, profile.gender,users_pics.time_group,profile.profile_pix, profile.fpage,up_user_id


ORDER BY MIN (users_pics.u_pic_id) DESC

增加超时时间会有所帮助,但您还应该检查您的查询是否被其他操作(如插入/更新或打开事务)阻止

最简单的方法是安装和使用程序

其次,您不需要像以前那样嵌套案例:

(CASE WHEN RTRIM (users_pics.upload_type) = 'wow'  THEN 'loaded' ELSE        

  CASE WHEN RTRIM (users_pics.upload_type)= 'hey' THEN 'added' ELSE       

  CASE WHEN RTRIM (users_pics.upload_type) = 'check'  THEN   'Changed'   END END END)as up_ans,  

下一件事:几乎在每个字符串值上,除非需要空格/制表符/换行符等,否则应该在插入过程中清理输入。 这样,您的查询就不需要
RTRIM
,并且可以利用索引(如果存在的话)

/* New values */
INSERT INTO table_name(...) VALUES  (LTRIM(RTRIM(value...)))

/* Existing ones */
UPDATE table_name
SET col = LTRIM(RTRIM(col))
SQL解析器将理解文本墙,人类需要时间来理解

我知道我们可以争论代码风格,但请记住,您是为人们编写代码的。良好的可读性代码使您能够更早地发现错误,并且对于您和您的继任者来说,在将来更容易维护:

1) 一个选定值一行

2) 选择和分组方式中的顺序相同

3) 末尾的聚合列

4) 您可以使用别名,无需使用完全限定的名称

5) 没有不明确的列名,请始终指定从哪个表

6) SQL语法大写

7) 对齐你的代码

您的查询更易于阅读,请访问:

SELECT TOP 8  
   [up_user_id]   /* Always add from which table even if it is unique column name, because in future you may get ambigous column */
  ,[fname]
  ,[profile_id] = RTRIM(profile.profile_id)
  ,[up_ans]     = CASE RTRIM(users_pics.upload_type)
                    WHEN 'wow'   THEN 'loaded'
                    WHEN 'hey'   THEN 'added'
                    WHEN 'check' THEN 'changed'
                    ELSE NULL
                  END
  ,[exs]        = CASE RTRIM(users_pics.upload_type) 
                    WHEN 'sample1' THEN 'new'
                    WHEN 'sample2' THEN 'existing'
                    WHEN 'sample3' THEN 'Profile Picture'
                    ELSE NULL
                  END
  ,[wardrobe]       = RTRIM(users_pics.wardrobe)
  ,users_pics.email
  ,[gender]         = RTRIM(profile.gender)
  ,users_pics.time_group
  ,profile.profile_pix
  ,profile.fpage
  ,[answer_one]     = MIN(CASE 
                            WHEN pic_alb_love.pic=users_pics.pic THEN 'User'
                            ELSE 'Guest'
                          END)
  ,[answer2]        = MIN(CASE
                            WHEN favorites.pic = users_pics.pic  AND favorites.email = 'try@mail.com' WHEN 'good' 
                            ELSE 'Bad'
                         END)
  ,[total]          = COUNT(DISTINCT users_pics.pic)
  ,[make]           = MIN(make)
  ,[htags]          = MIN(htags)
  ,[u_pic_id]       = MIN(u_pic_id)
  ,[pic]            = MIN(users_pics.pic)
FROM users_pics     /* you can use alias like AS up */
LEFT JOIN profile
   ON users_pics.email = profile.email  
LEFT JOIN favorites 
   ON users_pics.pic   = favorites.pic  
LEFT JOIN pic_alb_love
   ON users_pics.pic   = pic_alb_love.pic 
LEFT JOIN friends
   ON users_pics.email = friends.resp_email
WHERE 
        req_email  = 'try@mail.com'
    AND pic_enable = 'enable'
    OR  pic_view   = 'Public'  
GROUP BY                     
     up_user_id
    ,profile.fname
    ,profile.profile_id
    ,users_pics.upload_type
    ,users_pics.wardrobe
    ,users_pics.email
    ,profile.gender
    ,users_pics.time_group
    ,profile.profile_pix
    ,profile.fpage
ORDER BY MIN(users_pics.u_pic_id) DESC
在选择数据期间检查查询是否被阻止后,您可以考虑:

  • 检查表上的索引
  • 添加WHERE条件以获取较小的集合,也许您可以使用一些
    update\u date>current\u date-2周
  • 考虑优化查询,因为现在它需要时间来完成分组和排序
  • 您的
    WHERE
    条件,您确定不应该是:


确保您的查询未被阻止,您可以选择查询DMV或安装
sp\u whoisactive
SELECT TOP 8  
   [up_user_id]   /* Always add from which table even if it is unique column name, because in future you may get ambigous column */
  ,[fname]
  ,[profile_id] = RTRIM(profile.profile_id)
  ,[up_ans]     = CASE RTRIM(users_pics.upload_type)
                    WHEN 'wow'   THEN 'loaded'
                    WHEN 'hey'   THEN 'added'
                    WHEN 'check' THEN 'changed'
                    ELSE NULL
                  END
  ,[exs]        = CASE RTRIM(users_pics.upload_type) 
                    WHEN 'sample1' THEN 'new'
                    WHEN 'sample2' THEN 'existing'
                    WHEN 'sample3' THEN 'Profile Picture'
                    ELSE NULL
                  END
  ,[wardrobe]       = RTRIM(users_pics.wardrobe)
  ,users_pics.email
  ,[gender]         = RTRIM(profile.gender)
  ,users_pics.time_group
  ,profile.profile_pix
  ,profile.fpage
  ,[answer_one]     = MIN(CASE 
                            WHEN pic_alb_love.pic=users_pics.pic THEN 'User'
                            ELSE 'Guest'
                          END)
  ,[answer2]        = MIN(CASE
                            WHEN favorites.pic = users_pics.pic  AND favorites.email = 'try@mail.com' WHEN 'good' 
                            ELSE 'Bad'
                         END)
  ,[total]          = COUNT(DISTINCT users_pics.pic)
  ,[make]           = MIN(make)
  ,[htags]          = MIN(htags)
  ,[u_pic_id]       = MIN(u_pic_id)
  ,[pic]            = MIN(users_pics.pic)
FROM users_pics     /* you can use alias like AS up */
LEFT JOIN profile
   ON users_pics.email = profile.email  
LEFT JOIN favorites 
   ON users_pics.pic   = favorites.pic  
LEFT JOIN pic_alb_love
   ON users_pics.pic   = pic_alb_love.pic 
LEFT JOIN friends
   ON users_pics.email = friends.resp_email
WHERE 
        req_email  = 'try@mail.com'
    AND pic_enable = 'enable'
    OR  pic_view   = 'Public'  
GROUP BY                     
     up_user_id
    ,profile.fname
    ,profile.profile_id
    ,users_pics.upload_type
    ,users_pics.wardrobe
    ,users_pics.email
    ,profile.gender
    ,users_pics.time_group
    ,profile.profile_pix
    ,profile.fpage
ORDER BY MIN(users_pics.u_pic_id) DESC
 WHERE (req_email  = 'try@mail.com'
 AND pic_enable = 'enable')
 OR  pic_view   = 'Public'