简体   繁体   中英

To execute SQL query takes a lot of time

I have two tables. Tables 2 contains more recent records. Table 1 has 900K records and Table 2 about the same.

To execute the query below takes about 10 mins. Most of the queries (at the time of execution the query below) to table 1 give timeout exception.

    DELETE T1
    FROM Table1 T1 WITH(NOLOCK)
    LEFT OUTER JOIN Table2 T2
    ON T1.ID = T2.ID
    WHERE T2.ID IS NULL AND T1.ID IS NOT NULL

Could someone help me to optimize the query above or write something more efficient? Also how to fix the problem with time out issue?

Optimizer will likely chose to block whole table as it is easier to do if it needs to delete that many rows. In the case like this I delete in chunks.

while(1 = 1)
begin
    with cte
    as
    (
        select *
        from Table1
        where Id not in (select Id from Table2)
    )
    delete top(1000) cte

    if @@rowcount = 0
        break

    waitfor delay '00:00:01' -- give it some rest :)
end

So the query deletes 1000 rows at a time. Optimizer will likely lock just a page to delete the rows, not whole table.

The total time of this query execution will be longer, but it will not block other callers.

Disclaimer: assumed MS SQL.

Another approach is to use SNAPSHOT transaction. This way table readers will not be blocked while rows are being deleted.

Wait a second, are you trying to do this...

DELETE Table1 WHERE ID NOT IN (SELECT ID FROM Table2)

?

If so, that's how I would write it. You could also try to update the statistics on both tables. And of course indexes on Table1.ID and Table2.ID could speed things up considerably.

EDIT: If you're getting timeouts from the designer, increase the "Designer" timeout value in SSMS (default is 30 seconds). Tools -> Options -> Designers -> "Override connection string time-out value for table designer updates" -> enter reasonable number (in seconds).

Both ID columns need an index

Then use simpler SQL

DELETE Table1 WHERE NOT EXISTS (SELECT * FROM Table2 WHERE Table1.ID = Table2.ID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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