使用SQL Server搜索数千条记录

时间:2013-08-03 05:27:42

标签: .net sql-server sql-server-2005

我有一张包含数千条记录的表格,我使用以下查询在30秒内获得结果,但它的工作速度非常慢。我怎样才能做得更好?

 WITH cte AS 
 (select 
     ROW_NUMBER() OVER(ORDER BY sb.nbofviews desc) AS ROW,   
     sb.ImageId, sb.LicenceType, sb.ImgCollection, sb.nbofviews, 
     sb.orientation, sb.shootdate, sd.ImgName, sd.Caption, sd.ImgPath, 
     sd.ImageType, sd.ImgHoverPath, sc.image_color 
  from 
     SearchBrain sb,SearchDetails sd, searchcolors sc 
  where 
     sc.Imageid = sd.Imageid 
     and sb.ImageId = sd.ImageId 
     and sd.imageid in (select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                             (Select top 1 Primary_kwd 
                              from p2_kwdmgmt 
                              where all_kwd = 'man') 
                        intersect 
                        select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                              (Select top 1 Primary_kwd 
                               from p2_kwdmgmt 
                               where all_kwd = 'jumping') 
                        intersect 
                        select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                              (Select top 1 Primary_kwd 
                               from p2_kwdmgmt 
                               where all_kwd = 'beach')
                       )
) 
SELECT * 
FROM cte 
WHERE ROW BETWEEN 0 AND 31

2 个答案:

答案 0 :(得分:0)

我建议如下。如果它产生正确的结果,只需检查In ('man','jumping','beach') GROUP BY Primary_kwd部分。

WITH cte AS (
  SELECT ROW_NUMBER() OVER(ORDER BY sb.nbofviews DESC) AS ROW
  ,sb.ImageId
  ,sb.LicenceType
  ,sb.ImgCollection
  ,sb.nbofviews
  ,sb.orientation
  ,sb.shootdate
  ,sd.ImgName
  ,sd.Caption
  ,sd.ImgPath
  ,sd.ImageType
  ,sd.ImgHoverPath
  , sc.image_color 
FROM SearchBrain sb,SearchDetails sd, searchcolors sc 
WHERE sc.Imageid=sd.Imageid AND sb.ImageId=sd.ImageId AND sd.imageid in 
(
    SELECT ImageId FROM SearchInitial WHERE Primarykeyword In (SELECT Primary_kwd FROM p2_kwdmgmt WHERE all_kwd In ('man','jumping','beach') GROUP BY Primary_kwd) 
)
) 
SELECT * FROM cte WHERE ROW BETWEEN 0 AND 31

答案 1 :(得分:0)

请试试这个。希望这会有所帮助。

 WITH cte AS 
 (select 
     ROW_NUMBER() OVER(ORDER BY sb.nbofviews desc) AS ROW,   
     sb.ImageId, sb.LicenceType, sb.ImgCollection, sb.nbofviews, 
     sb.orientation, sb.shootdate, sd.ImgName, sd.Caption, sd.ImgPath, 
     sd.ImageType, sd.ImgHoverPath, sc.image_color 
  from (SELECT * FROM SearchDetails 
        WHERE imageid in (select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                             (Select top 1 Primary_kwd 
                              from p2_kwdmgmt 
                              where all_kwd = 'man'))
              OR imageid in (select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                             (Select top 1 Primary_kwd 
                              from p2_kwdmgmt 
                              where all_kwd = 'jumping'))
              OR imageid in (select ImageId 
                        from SearchInitial 
                        where Primarykeyword = 
                             (Select top 1 Primary_kwd 
                              from p2_kwdmgmt 
                              where all_kwd = 'beach'))
        ) sd
     INNER JOIN SearchBrain sb ON sd.ImageId = sb.ImageId
     INNER JOIN searchcolors sc ON sd.ImageId = sc.ImageId 
) 
SELECT * 
FROM cte 
WHERE ROW BETWEEN 0 AND 31