为什么每次调用时SYSDATETIME()都没有更改?

时间:2019-04-05 21:04:18

标签: sql-server tsql

我正在尝试分析查询的性能。不幸的是,我无权对正在读取的数据库运行估算或实际的查询计划。 ({SHOWPLAN权限在数据库中被拒绝)

我已经重写了原始查询,但是想比较原始查询和我的两个查询的执行时间。为此,我想我将创建4个datetime2变量,并在每次迭代之间记录SYSDATETIME(),然后找出差异。

不幸的是,我发现SYSDATETIME()一经调用就不会更新。

我的3个查询总共运行了17秒。在每次执行之间,我应该能够看到时间戳的变化,但是每次变量都记录相同的时间戳。

declare @a datetime2, @b datetime2, @c datetime2, @d datetime2

set @a = SYSDATETIME()

select bunch of columns from a bunch of joined tables

set @b = SYSDATETIME()

select bunch of columns and sub-queries from a bunch of tables joined a little differently

set @c = SYSDATETIME()

select bunch of columns and sub-queries from a bunch of differently joined tables and sub-queries

set @d = SYSDATETIME()

print @a
print @b
print @c
print @d

所有4个变量都具有相同的值。

a   2019-04-05 16:00:09.3947421 
b   2019-04-05 16:00:09.3947421 
c   2019-04-05 16:00:09.3947421 
d   2019-04-05 16:00:09.3947421

总执行时间为19秒,因此应该有有效的数据要检查。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

好吧,我不再打印担心它的周期,而是打印了时间戳,并在每次测试的迭代之间放置了一个GO。然后再做数学。

仍然不知道为什么SYSDATETIME每次调用都没有返回真实值。也许SQL Server试图提供帮助。

FYI,从该rtrim(ltrim(FIELD)))比FIELD更快,例如'A%',在FIELD为char(10)时,它比FIELD ='A'更快。

答案 1 :(得分:-1)

整个查询可能一次全部开始。

请尝试分批运行每个查询。那应该给你一些有用的时间戳。

        # Call the Gmail API to fetch INBOX

        results = service.users().messages().list(userId='me',labelIds = ['INBOX', 'UNREAD']).execute()
        messages = results.get('messages', [])


        if not messages:
            frase = 'Sem novas mensagens na caixa de email'
            a = Pesquise(frase)
            a.fala(frase)
        else:
            for message in messages:
                msg = service.users().messages().get(userId='me', id=message['id']).execute()
                payld = msg['payload'] # get payload of the message 
                headr = payld['headers'] # get header of the payload
                for one in headr: # getting the Subject
                    if one['name'] == 'Subject':
                        msg_subject = remover_acentos(one['value'])
                    else:
                        pass
                frase = 'Hugo, novo email na caixa de entrada, ' + msg_subject
                a = Pesquise(frase)
                a.fala(frase)
        sleep(600)
except Exception as err:
    print err
相关问题