Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123202
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:34:52+00:00 2026-05-20T23:34:52+00:00

I have a simple query for update table (30 columns and about 150 000

  • 0

I have a simple query for update table (30 columns and about 150 000 rows).

For example:

UPDATE tblSomeTable set F3 = @F3 where F1 = @F1

This query will affected about 2500 rows.

The tblSomeTable has a trigger:

ALTER TRIGGER [dbo].[trg_tblSomeTable]
   ON  [dbo].[tblSomeTable]
   AFTER INSERT,DELETE,UPDATE
AS 
BEGIN
    declare @operationType nvarchar(1)
    declare @createDate datetime
    declare @UpdatedColumnsMask varbinary(500) = COLUMNS_UPDATED()


 -- detect operation type
if not exists(select top 1 * from inserted)
    begin
        -- delete
        SET @operationType = 'D'
        SELECT @createDate = dbo.uf_DateWithCompTimeZone(CompanyId) FROM deleted
    end 
else if not exists(select top 1 * from deleted)
    begin
        -- insert
        SET @operationType = 'I'
        SELECT @createDate = dbo..uf_DateWithCompTimeZone(CompanyId) FROM inserted
    end
else
    begin
        -- update
        SET @operationType = 'U'
        SELECT @createDate = dbo..uf_DateWithCompTimeZone(CompanyId) FROM inserted
    end


-- log data to tmp table

INSERT INTO tbl1
SELECT
    @createDate,
    @operationType,
    @status,
    @updatedColumnsMask,
    d.F1,
    i.F1,
    d.F2,
    i.F2,
    d.F3,
    i.F3,
    d.F4,
    i.F4,
    d.F5,
    i.F5,
    ...

FROM (Select 1 as temp) t
LEFT JOIN inserted i on 1=1
LEFT JOIN deleted d on 1=1  

END

And if I execute the update query I have a timeout.

How can I optimize a logic to avoid timeout?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:34:53+00:00Added an answer on May 20, 2026 at 11:34 pm

    This query:

    SELECT  *
    FROM    (
            SELECT  1 AS temp
            ) t
    LEFT JOIN
            INSERTED i
    ON      1 = 1
    LEFT JOIN
            DELETED d
    ON      1 = 1
    

    will yield 2500 ^ 2 = 6250000 records from a cartesian product of INSERTED and DELETED (that is all possible combinations of all records in both tables), which will be inserted into tbl1.

    Is that what you wanted to do?

    Most probably, you want to join the tables on their PRIMARY KEY:

    INSERT
    INTO    tbl1
    SELECT  @createDate,
            @operationType,
            @status,
            @updatedColumnsMask,
            d.F1,
            i.F1,
            d.F2,
            i.F2,
            d.F3,
            i.F3,
            d.F4,
            i.F4,
            d.F5,
            i.F5,
            ...
    FROM    INSERTED i
    FULL JOIN
            DELETED d
    ON      i.id = d.id
    

    This will treat update to the PK as deleting a record and inserting another, with a new PK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL 2008 - Simple Query logic. In my Table :TableName, i have 3 columns
I have some funny deadlock caused by a stupid simple SQL UPDATE query, on
I have a simple query like this: select * from mytable where id >
I have a simple query: $query = new WP_Query('showposts=5'); that will obviously display 5
I have a simple table made up of two columns: col_A and col_B .
I have a table with around 6 million rows and has many columns with
I have a very simple update statement: UPDATE W SET state='thing' WHERE state NOT
I have a database table with 300 000 records. The most common query done
I have a simple query which is returning records based on the field status
I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.