【问题标题】:SQL General OptimizationSQL 一般优化
【发布时间】:2014-11-07 19:19:18
【问题描述】:

最近被要求帮助查询优化

表格如下所示:

create table dbo.Table
( 
 id int identity primary key clustered ,
 column_1 varchar(64) not null  ,
 Date datetime not null ,
 Column_2 varchar (32) not null ,
 Column_3 int not null 
)

然后选择看起来像

select * from Table where column_1 = @value1 and Date > @value2

我建议在 select 中显示列名称而不是 *,因为它可以帮助避免加载不需要的数据,还建议在 column_1 上显示 create nonclustered index。但是,执行计划仍然显示查询使用的内存量。

我还应该检查或添加什么到查询中?

【问题讨论】:

    标签: sql sql-server-2008-r2 sql-optimization


    【解决方案1】:

    您可以使用索引来优化查询。你想要的是column_1date

    create index idx_table_column1_date on table(column_1, date);
    

    【讨论】:

    • @Andrey 表中有多少行?有多少匹配谓词? @value1 的数据类型是什么?
    • @Andrey - 其中有多少符合条件?您还确定您的查询使用的是= 而不是LIKE?你的评论最初是说'%HR%'
    • 那是 4.3%。这可能已经超过了临界点。如果您使用相同的WHERE 子句(而不是SELECT *)执行SELECT column_1, date,您会得到搜索吗?
    • @Andrey 实际上我只是在您的问题中注意到这一点“执行计划仍然显示查询使用的内存量相同”。很可能你正在关注一些没有任何意义的数字。如果您正在查看内存授予数字,那么这两个查询都只需要最少的内存。您应该查看诸如扫描与搜索之类的内容。
    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多