Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INSERT INTO Mem
	(Ind, CKey
	, ID
	, Date
	, Code
	, Key
	, Cov
	, CovKey)
SELECT 'a', EKey
	, d.ID 
	, d.CEEffDate
	, d.Code 
	, d.Key
	, d.Cov 
	, d.CovKey
FROM DEl d
	WHERE NOT EXISTS
	  (SELECT Mem FROM Mem s 
	    WHERE s.ID = d.ID
	     AND s.PID = d.PID
	     AND s.LID = d.ID
	     AND s.EDate = d.EDate
	    )
	  AND Key1 <> 0
	  AND Key1 between @Binr and @Einr
Posted
Updated 11-Nov-11 3:02am
v3
Comments
Smithers-Jones 11-Nov-11 8:20am    
Added code-block.
JamesWinter 11-Nov-11 8:56am    
i didnt getu.
Mehdi Gholam 11-Nov-11 8:29am    
Have tried indexing?
JamesWinter 11-Nov-11 9:00am    
yes, in the select table colums indexes were present, and on insert table no indexes..bt stil taking mre tme.

Hi....

Use JOINS then Check the Performance

Eg....

SQL
INSERT INTO Mem
	(Ind, CKey
	, ID
	, Date
	, Code
	, Key
	, Cov
	, CovKey)
SELECT 'a', EKey
	, d.ID 
	, d.CEEffDate
	, d.Code 
	, d.Key
	, d.Cov 
	, d.CovKey
FROM DEl d
INNER JOIN Mem s ON s.ID <> d.ID AND s.PID <> d.PID AND s.LID <> d.ID AND s.EDate <> d.EDate
WHERE Key1 <> 0 AND Key1 between @Binr and @Einr
 
Share this answer
 
v3
Comments
Karthik Harve 21-Dec-11 3:31am    
[Edit] pre tags added.
use this query

INSERT INTO Mem
(Ind, CKey
, ID
, Date
, Code
, Key
, Cov
, CovKey)
SELECT 'a', EKey
, d.ID
, d.CEEffDate
, d.Code
, d.Key
, d.Cov
, d.CovKey
FROM DEl d , mem s
WHERE
s.ID = d.ID
AND s.PID = d.PID
AND s.LID = d.ID
AND s.EDate = d.EDate
AND Key1 <> 0
AND Key1 between @Binr and @Einr
and d.mem not in(select mem from mem)
 
Share this answer
 
Hi James,
You have compared 4 values to match the rows between Del and Mem. If its possible make some changes in your database design so that a simple s.ID=d.ID works for finding matching rows.

Otherwise have look at other ideas about the comparison between JOIN and IN and EXIST performance :
http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/[^]

http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx[^]

http://stackoverflow.com/questions/1001543/in-vs-join-with-large-rowsets[^]

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900