简体   繁体   English

SQL Query 执行需要时间

[英]SQL Query takes time for execution

My table having 2 million records.我的表有 200 万条记录。 I want to insert data in temp table and use that temp table in my procedure.我想在临时表中插入数据并在我的过程中使用该临时表。 But for insertion it's taking 30 seconds.但是插入需要 30 秒。 Please suggest how to reduce the time?请建议如何减少时间? I cant show actual query.我无法显示实际查询。 so my query is in this format.所以我的查询是这种格式。

Note: I had already used CTE.注意:我已经使用过 CTE。

;With Tmp_TableName
        as
        (
        SELECT  T1.A, T1.B, T1.C, T1.D, T1.E, T1.F, T1.G , T1.H
        FROM    TableName T1 WITH(NOLOCK)
        WHERE   T1.A = 1
                AND CONVERT( NVarchar, T1.H, 111) <= CONVERT( NVarchar, getdate(), 111)

        )
    SELECT  *
    FROM    Tmp_TableName T1 WITH(NOLOCK);

I would recommend fixing your WHERE clause:我建议修复您的WHERE子句:

WHERE T1.A = 1 AND
      T1.H < DATEADD(DAY, 1, CAST(GETDATE() as DATE)0

This can then take advantage of an index on TABLE1(A, H) .然后,这可以利用TABLE1(A, H)上的索引。

You can just filter out with cast() :您可以使用cast()过滤掉:

WHERE T1.A = 1 AND
      T1.H <= CAST(GETDATE() AS DATE) -- This assumes `T1.H` has no time. (i.e. 00:00:00);

If in your SELECT statement has any field which has varchar/nvarchar(max) type then it might run to slow, you can exclude it & check further.如果在您的SELECT语句中有任何具有varchar/nvarchar(max)类型的字段,那么它可能会运行缓慢,您可以排除它并进一步检查。

However, you didn't included any table structure with indexes so, it is difficult to answer anyone.但是,您没有包含任何带有索引的表结构,因此很难回答任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM