TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bhavesh Jadav
NA
1.2k
17.6k
Performance of query execution
Jun 21 2018 5:44 AM
Hello friends,
Suppose I have 5000 records and I want to perform an operation on it.
I want to use 2 columns in sub-query for all above records.
Example:
Way-1:
DECLARE @TempSizeID
INT
;
SET @NewSizeID = ISNULL((SELECT ISNULL(id,0) FROM tb_sizes WHERE StoreID=@NewStoreID),0);
IF(@NewSizeID = 0)
BEGIN
-- sub-query
for
insert data
SET @NewSizeID = SCOPE_IDENTITY()
END
ELSE
BEGIN
IF NOT EXISTS (SELECT ISNULL(SizeID,0) FROM tb_ProductColorSizeMapping WHERE ProductID = @NewProductID AND SizeID = @NewSizeID)
BEGIN
-- sub-query
for
insert mapping data
END
END
Way-2:
DECLARE @TempSizeID
INT
;
IF(@ISNULL((SELECT ISNULL(id,0) FROM tb_sizes WHERE StoreID=@NewStoreID),0) = 0)
BEGIN
-- sub-query
for
insert data
SET @NewSizeID = SCOPE_IDENTITY()
END
ELSE
BEGIN
IF NOT EXISTS (SELECT ISNULL(SizeID,0) FROM tb_ProductColorSizeMapping WHERE ProductID = @NewProductID AND SizeID =
(ISNULL((SELECT ISNULL(id,0) FROM tb_sizes WHERE StoreID=@NewStoreID),0)))
BEGIN
-- sub-query
for
insert mapping data
END
END
In way-1 it number of queries is less but it takes storage to store id
In way-2 it number of queries is more but and not store id
Can anybody suggest me which is fast?.
Reply
Answers (
3
)
How to string Replace in Sql server
How to bulk update an sql server table using xml ?