r/SQLServer • u/PrtScr1 • Dec 05 '25
Question Why SELECT INTO causes wide spread LOCK?
While a job is running with a long duration SELECT INTO query, this block several users connecting to SSMS and expanding the table list with SCHEMA lock, this is very weird even though other users or queries not referring to the tables that are in use by the job. This is annoying so many users and have to wait for the job to complete over couple of hours.
SQL Server 2022 Enter ed.
Any tips to get thru this issue?
8
Upvotes
•
u/AutoModerator Dec 05 '25
After your question has been solved /u/PrtScr1, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
12
u/Lost_Term_8080 Dec 05 '25
The A in ACID stands for atomicity. SELECT INTO is a single statement, and in atomicity either all a transaction goes through or none of it does. When you use SELECT INTO, you are creating a table, this requires a schema lock. That schema lock is then held until the insert either completes or fails and rolls back.
Don't use SELECT INTO. Create the table then insert into it. I would stop dropping the table after it runs, it serves no purpose to remove it when it is going to be recreated every time. If you want to reclaim the space, then truncate it at the end of the process instead of dropping it.
28
u/dbrownems Microsoft Employee Dec 05 '25 edited Dec 11 '25
Currently the SQL Server catalog is always read in READ COMMITTED mode, not with row versioning snapshots. Creating a table requires an exclusive schema lock (SCH-M) on an object, which is held for the duration of the transaction creating the table. An exclusive schema lock on the table prevents other sessions from scanning all the tables. Essentially a Sch-M lock is implemented as a row-level X lock on the affected system catalog tables. So other sessions running "select * from sys.tables" require incompatible S locks on the system tables, and will get blocked.
As others have said, create the table first, to allow the table creation to commit before the data load. Note running SELECT ... INTO without a transaction will commit the table creation before loading it, but if it's in a transaction the table creation will not be committed until the end of the transaction.
2
u/xodusprime Dec 05 '25
This statement mixes ddl and dml. You're making a table in the same transaction as the query to get its data. This blocks other queries trying to enumerate the tables until it is done, including the ones ssms uses to populate it's table list. Make the table first, then insert into it to avoid this.
1
u/ZealousidealBook6639 Dec 06 '25
SELECT INTO can take out wider locks than you’d expect because under the covers SQL Server has to create the new table in the target database (updating metadata/system catalogs) and then insert all the rows, and that DDL + heavy DML combination tends to escalate to higher-level locks (including SCH-M on metadata) that block things like SSMS Object Explorer from enumerating table lists, even if other users aren’t touching the same objects. In SQL Server 2022 this can be more visible in busy systems or when the SELECT INTO runs for hours. A few ways to reduce the pain:
-2
u/SirGreybush 1 Dec 05 '25
This is because of saturation, is your DB set to FULL or Simple for the transaction logging? We use Simple for a reporting server, and Full for the ERP server.
Mine is set to simple, and server settings on the Azure VM is 80% of VM memory allocated to SQL Server, auto for all processors, Cost threshold for parellism is at 50, locks at 0, max degree of parallesim at 12 on a 16-cpu (quad cpu with 4 cores, so seen as 16), query wait -1.
It's not ideal, but it's a reporting server & DW server, the only issue I have is a bad query causing a soft lock on tables, that will block a bulk insert due to the query wait being -1.
There are multiple white papers on this, check out https://vladdba.com/ posts, my setup isn't ideal but it works for us.
The speed that the transaction log gets processed is dependent on available memory for caching, and the speed of the disk(s) the .mdb and .ldb files are stored on.
Also VMs are slower than bare metal - I don't know if you're cloud or on-prem. If on-prem VM, talk to your sysadmin dude to sit and check with you, he has tools to see where the bottleneck is (cpu, ram or disk).
I've freelanced a lot of small companies that have on-prem SQL installs, and when you right-click, properties on the server with SSMS, Advanced, at the bottom, often things are factory default, maximum memory allocated. The OS needs ram too, reserve 10% or at least 2 gigs for it. Also don't remote desktop to it as an admin and run programs like SSMS, you want to avoid the server OS doing memory swapping on the main HD.
HTH