我写了一个查询,用于从microsoft sql server数据库中检索数据。目标是计算带有图片的账单数量和细分所代表的区域中的账单总数我写的查询工作正常,但需要很多时间来获取结果。在普通的电脑上大约需要4-5分钟但是在电脑上有更高的内存需要不到一分钟。因为我不是sql的专家,如果可能的话,我需要帮助优化我的查询。这是查询请告诉我是否可以进一步优化。
SELECT [sub division],
[total bills],
[pic bills],
( ( t1.[pic bills] * 100 ) / t2.[total bills] ) AS '%'
FROM (SELECT gbsdiv AS 'Sub Division',
Count(*) AS 'Pic Bills'
FROM tblgbmain
WHERE ( gbpicture1 IS NOT NULL
OR gbpicture2 IS NOT NULL )
AND gbbatch = 15
GROUP BY gbsdiv) t1
INNER JOIN (SELECT gbsdiv AS 'Sub Division1',
Count(*) AS 'Total Bills'
FROM tblgbmain
WHERE gbsdiv IN (SELECT gbsdiv
FROM tblgbmain
WHERE ( gbpicture1 IS NOT NULL
OR gbpicture2 IS NOT NULL )
AND gbbatch = 15
GROUP BY gbsdiv)
AND gbbatch = 15
GROUP BY gbsdiv) t2
ON t1.[sub division] = t2.[sub division1]