0

我需要加快 SQL Server 上的几个查询。

感谢您的任何帮助。

第一个在实时数据库上大约需要 30-45 秒,第二个需要 90 秒。

如果您想知道我在医院工作,因此有类别名称,我们有一个非常大的数据库

select Status, StatusChoice, Category, VisitID, 
     OrderedProcedureName, OrderDateTime, ServiceDateTime 
from OeOrders 
where Category IN 'RAD' 
    and StatusChoice = 'S'

.

select Status, StatusChoice, Category, VisitID, 
     OrderedProcedureName, OrderDateTime, ServiceDateTime 
from OeOrders 
where Category IN ('RAD','CT','US','MRI') 
    and StatusChoice = 'S'
4

3 回答 3

1

您可以尝试通过在表上创建索引来提高您的 SELECT 性能;对于非查询命令(例如 UPDATE、INSERT、DELETE),它可能会使性能稍差,因为 SQL 也必须更新他的索引,但请随意尝试。

运行此脚本一次,然后以秒为单位再次检查延迟:

CREATE INDEX iCategoryChoiche ON OeOrders 
(StatusChoice,Category) INCLUDE (Status, StatusChoice, Category, VisitID, OrderedProcedureName, OrderDateTime, ServiceDateTime)
于 2012-11-16T14:26:33.540 回答
0

好吧,我可以说,在第一个 SQL 中,您可以使用=而不是IN,我相信它会更快。

select Status, StatusChoice, Category, VisitID, OrderedProcedureName, OrderDateTime, ServiceDateTime from OeOrders where Category='RAD' and StatusChoice = 'S'

也看看这些,它可能会有所帮助: http ://hungred.com/useful-information/ways-optimize-sql-queries/

于 2012-11-16T14:25:46.770 回答
0

您可以尝试通过将子查询放在 FROM 中来首先强制执行 StatusChoice 过滤:

select src.*
from (
select Status, StatusChoice, Category, VisitID, 
     OrderedProcedureName, OrderDateTime, ServiceDateTime 
from OeOrders 
where StatusChoice = 'S'
) src
where src.Category IN ('RAD','CT','US','MRI') 

但是改进的机会很小——DBMS 可能已经在以这种方式执行查询了。这种 rewerite 的原因是具有多个值的“IN”通常转换为使用“或”谓词进行过滤,这在 SQL 中相当慢,因此可能会更快地返回较小的要“或”的行集。

查询非常简单,因此您宁愿查看执行计划。从执行计划中,您将知道哪些索引可以使用重组,以及哪些统计信息应该更新(或者可能创建新的)。当然,(StatusChoice,Category)上的新索引会有所帮助,如果它还没有出现的话。

如果你不关心得到一些“脏”的结果,你也可以使用 (NOLOCK) 提示。

于 2012-11-16T14:47:55.937 回答