Why is this query slow the first time after I start the service?

前端 未结 2 2025
广开言路
广开言路 2021-01-17 08:56

Ok. Here\'s what I try to run:

USE tempdb;

SELECT TOP 1000000 IDENTITY(INT, 1, 1) Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
CROSS J         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-17 09:30

    Rather than chase this problem, why don't you just create the table once in the model database, then it will be created for you in tempdb automatically?

    For the actual problem, we don't know. My first guess would be that your initial size for your tempdb file(s) is very small (like, 1MB). So, when you create the table, it has to expand the file(s) to accommodate it. This can be quite expensive especially if you don't have instant file initialization enabled, and it can also be very expensive to grow the log to accommodate the activity required there as well.

    Other than that, we could keep guessing, but you will be better suited to investigate what is actually happening. Questions you'll want to ask:

    1. For the spid that is trying to create the table, what does sys.dm_exec_requests say for wait_type?
    2. Does it have a blocking_session_id?
    3. If so, what is that session doing?

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题