逻辑读取高,查询简单

时间:2018-11-28 09:43:07

标签: sql-server sqlperformance

我有一个非常简单的查询,例如

def swe(lst):
    s = []
    a = []
    v = []
    q = 0
    l = 0 
    for d in lst:
        s.append(d.keys())
        v.append(d.values())
    for i in s:
        for j in i:
            if len(i) == 1:
                a.append(j)
            if len(i) > 1:
                a.append(j)
                for t in a:
                    if a.count(t) == 1:
                        for q in range(len(v)):
                            for q in range(len(s)):
                                dct1 = v[q]
                                dct2 = s[q]
                                dct3 = dct2+ dct1
                                q = q+1
                                continue
                        return dct3
                    if a.count(t) > 1:
                        for l in range(len(a)):
                            dct5 = v[q]
                            dct6 = s[q]
                            dct7 = dct5 + dct6
                            l = l+ 1
    return dct7



print swe([{(1,3):2, (2,7):1} , {(1,3):6}])

其中pk_col是主键。

当我在统计IO和时间为ON的情况下运行此查询时,我得到以下信息

select col1,col2 from table A where pk_col =1

SQL Server执行时间:  (1 row affected) Table 'A'. Scan count 1, logical reads 2948, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

1)为什么即使只检索记录,逻辑读取还是这么高?

1 个答案:

答案 0 :(得分:2)

由于pk_colvarchar,因此SQL Server需要将pk_col转换为int,因为int的优先级更高:

Data type precedence (Transact-SQL)

因此查询等同于:

SELECT col1,col2 FROM table A 
where CONVERT(int, pk_col) =1

将列包含在函数或表达式中会阻止SQL Server使用索引。因此,SQL Server必须扫描整个表。因此,逻辑读取数量众多