I've got a CTE that does a lot of hard work over tens of thousands of records.
If I just run the select below which uses the CTE it comes back in less than a second.
However if I add the insert line above it, it takes around 30 seconds! Even though the select is only bringing back 1 or 2 records.
I've tried this with temp tables, real tables, variable tables and they are all the same.
INSERT INTO VIPScoreLog --Fast without this line
SELECT
vs.CustomerId,
vs.VIPScore
FROM CTE_VIPScore vs
LEFT JOIN vw_LatestVIPScores lvs on vs.CustomerId = lvs.CustomerId
WHERE lvs.VIPScore IS NULL
OR lvs.VIPScore <> vs.VIPScore
Any ideas how I can get rid of this 29 second delay?
This on SQL Server 2019
Thanks Golden Lion, I tried replacing the CTEs with temp tables instead and it's lightning fast.
Not sure I understand why but I guess I'm sorted now.
Thanks everyone for your help.
Related
Problem
I have table have 20 columns when make select data from new SQL query computer hangs ?
what suggestion to make quickly read data from this table and make performance good ?
What I Try
==========
select * from table where 1=1
take 45 minutes
after minimize column number as
select column 1,column 2,column 3 from table
also hangs but take less time as 42 minutes .
when make select data from tables not other programs open only SQL server opened new query.
table have 1 million records .
Computer Capability
==============
i work on SQL server 2012.
select data from this table hangs computer although my computer capability not bad
ram 8 GIGA and processor core I 5 .
I try same Backup of data on another computer it take too much time as above ?
==============
if possible what suggestions to select data quickly from table and best performance ?
If data is inserting/updating in this tables at the time of select query you may face deadlock issues.
Try with(nolock) and check if table has proper indexes.
select * from table with(nolock) where 1=1
Rebuild the indexes.
My goal is to suppress DEC from JAN using a unique id and export the results into a txt file. The query runs fine except for the fact that it returns a ton of null values (41 fields worth). I'm assuming these are from the DEC table due to the left join. This is my first attempt at a suppression like this. I am sure I am missing a simple statement to cure my ills but I am at a loss for what it might be. Or is there a more efficient technique than left join?
SELECT *
FROM [dbo].[Jan]
LEFT JOIN [dbo].[DEC]
ON [DEC].[ID] = [Jan].[ID]
WHERE [DEC].[SSN] IS NULL
Because you are using *, it is fetching everything for you.
Change SELECT * to SELECT JAN.columnName, DEC.columnName, etc. If you declare what columns you want from both tables, it should give you the right results.
I have got 2 databases sitting on different physical servers and linked. I need to join DB1.T1 with DB2.T2 and create an id. The problem is performance. My senior insists using a function and I have created it below.
IF OBJECT_ID (N'dbo.getXXXId', N'FN') IS NOT NULL
DROP FUNCTION dbo.getTRId;
GO
CREATE FUNCTION dbo.getTRId (#gcPRef bigint)
RETURNS varchar (100)
WITH EXECUTE AS CALLER --may not be necessary. not sure.
AS
BEGIN
DECLARE #TRID varchar (100);
SELECT #TRID = CONVERT(varchar (12), hu2.PropId)
+ '_'+ CONVERT(varchar (12), c.WSId)
FROM [172.29.110.133].DB1.dbo.checks c
join [172.29.110.133].DB1.[dbo].VHier
ON VHier.xx= c.xx
join [172.29.110.133].DB1.[dbo].rvc
ON rvc.xx= VHier.xx
AND rvc.yy= VHier.yy
join [172.29.110.133].DB1.[dbo].HUNIT hu
ON c.xx= hu.xx
WHERE c.CheckId = #gcPRef;
RETURN (#TRID);
END;
GO
I use the query below to query each checkid using the function above.
select getTRId(guestCheckPRef), guestCheckid from DB2.Guest_CHECKS GC
where GC.closeBusinessDate = '2014-06-25'
A couple of things you may like to know:
DB1 and DB2 are hosted on different physical servers.
I am not a DBA so please let me know if I am doing anything wrong.
Approximately 45000 records created daily. so this is the amount of rows..
I have already tried joining them without involving a function. it takes forever. in 30 seconds, 450 records returned only. I cannot keep tables locked for a long time.
CONSTRAINT [DB1.PK_CHECK] PRIMARY KEY CLUSTERED
CONSTRAINT [DB2.XPKGUEST_CHECKS] PRIMARY KEY NONCLUSTERED
I do not know if constraints are playing a role here. DB2.GUEST_CHECKS.guestCheckPRef is NOT even a FK here. guestCheckPRef is PK in DB1.CHECK.
performance is very poor. I need to return DB2.propid + DB2.wsid + DB1.guestCheckid.
This is all I can give for now. Any suggestion is appreciated. It does not have to be done with a function.
Thanks in advance. Regards.Oz.
Here are a few things to try or consider:
Have you checked that the query is using the best available indexes? You could try running the query through the query analyser to see if there's any indexes you could add to improve performance.
What version of SQL Server are you running? Depending on the version you might be able to replicate the table from one server to the other to alleviate the cost of running a query across your network.
I notice that several of the joins are across to the other server - could you consolidate all of those joins into a single view that is optimised using indexes - may result in less network traffic.
Try putting your function on the other server and calling it from the first server to see if there's any performance improvement.
Doing a "select" in a function is generally considered "not a good idea". The select in the function will be repeated once for each row in the result set, which is probably why the performance is bad.
Erp. This was supposed to be a comment, not an answer. To turn this into a proper answer, rewrite the query as a join, without using the function. (I.e. take the contents of the function and integrate it into a single join.)
Your example query should look something like this:
;with getTRID as
(SELECT CONVERT(varchar (12), hu2.PropId)
+ '_'+ CONVERT(varchar (12), c.WSId) AS TRID
FROM [172.29.110.133].DB1.dbo.checks c
join [172.29.110.133].DB1.[dbo].VHier
ON VHier.xx= c.xx
join [172.29.110.133].DB1.[dbo].rvc
ON rvc.xx= VHier.xx
AND rvc.yy= VHier.yy
join [172.29.110.133].DB1.[dbo].HUNIT hu
ON c.xx= hu.xx)
select getTRId.TRID, guestCheckid from DB2.Guest_CHECKS GC
inner join getTRID ON CheckId = guestCheckPRef
where GC.closeBusinessDate = '2014-06-25'
N.B. I'm working from memory here so, please, no flames for syntax errors! Thx.
Steve G.
I need to copy PROD DB to DEV DB to make tests, I need to do it several times a day.
I tried to a truncate then insert but some tables have almost 60 million rows.
Is there a way of updating only the difference between tables?
I heard this can be done through Slow Changing Dimension.
Basically what I want to do is :
delete from DBDEV the result of this :
(SELECT * FROM [DBDEV].[dbo].[TableA] EXCEPT SELECT * FROM [DBPROD].[dbo].[TableA])
Then insert into DBPROD the result of this :
(SELECT * FROM [DBPROD].[dbo].[TableA] EXCEPT SELECT * FROM [DBDEV].[dbo].[TableA])
If there's no SSIS component that can help with that, an SQL query would help me, it's just that I don't know how to write it to reflect what I need.
Thank you in advance
I have a database in SQL Server 2008 R2 where all tables seem to be functioning normally except for one table.
In this table, I can't delete a row or insert a row because it goes for over 30 min and times out.
My insert looks like this:
INSERT INTO [dbo].[brokenTable] ([Change], [Date], [ProdId], [IntCol])
VALUES ('test', getdate(), null, '99999')
However, I can select. Selecting top 200,000 takes 33 seconds. There are only ~260,000 rows in the table. There are five columns(one int primary key, one date, another int columns, and 2 varchar columns.)
This table used to work fine and be quick, I have made no structure changes at all.
Does anyone have any ideas why this might have happened, and how to fix it?
To people of the future who may have the same problem.
I thought the query was going very slow, this was not the case. It was being locked by another session. If you follow Martin Smith's comments, he helped me find which one to fix it.
First, I ran
select * from sys.dm_os_waiting_tasks
while running a query that would not finish, and again while not running it, to find a LCK_M_IX. I used the Session Id from that row, and matched it with the row from
select * from sys.dm_exec_sessions
and found the offending session that was blocking my query!
Since I knew this was a safe session to end, i ended it with
Kill [sessionId]