SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Improvement


Query Improvement

Author
Message
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3099 Visits: 2766
When I executet his query it takes a lot of time...Can anyone plz help in this?

SELECT id, english_name
FROM ys_movements
WHERE is_private =0
AND id
IN (

SELECT object_id
FROM profiles_useritems
WHERE content_type_id = (
SELECT id
FROM django_content_type
WHERE name ="movements"
AND app_label ="yoga")
AND user_id
IN (

SELECT target_user_id
FROM follow_follow
WHERE user_id =8
AND target_user_id NOT
IN (

SELECT id
FROM auth_user
WHERE is_staff =1
)))

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5920 Visits: 5084
Hi,

What does its execution plan say? Are there any missing indexes? How big are the tables? Check the statistics on them.

You can replace the IN with EXISTS and NOT EXISTS.

Regards
IgorMi

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3099 Visits: 2766
IgorMi (5/9/2013)
Hi,

What does its execution plan say? Are there any missing indexes? How big are the tables? Check the statistics on them.

You can replace the IN with EXISTS and NOT EXISTS.

Regards
IgorMi

INdexes are there on the columns.. for less data it works fine but when data gets increased it takes more time to execute

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16338 Visits: 19552
kapil_kk (5/9/2013)
IgorMi (5/9/2013)
Hi,

What does its execution plan say? Are there any missing indexes? How big are the tables? Check the statistics on them.

You can replace the IN with EXISTS and NOT EXISTS.

Regards
IgorMi

INdexes are there on the columns.. for less data it works fine but when data gets increased it takes more time to execute


Post the ACTUAL execution plan as a .sqlplan attachment so folks can analyse it.
Here's a close equivalent written in a more conventional style for your perusal:
SELECT y.id, y.english_name
FROM ys_movements y
INNER JOIN (
SELECT DISTINCT p.object_id
FROM profiles_useritems p
INNER JOIN django_content_type d
ON d.id = p.content_type_id
INNER JOIN follow_follow f
ON f.target_user_id = d.user_id
AND f.user_id = 8
WHERE d.name = 'movements'
AND d.app_label = 'yoga')
AND NOT EXISTS (
SELECT 1
FROM auth_user a
WHERE a.is_staff = 1
AND a.id = d.user_id)
) f
ON f.object_id = y.id
WHERE y.is_private =0



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sqlnaive
sqlnaive
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4293 Visits: 2774
ChrisM@Work (5/9/2013)
kapil_kk (5/9/2013)
IgorMi (5/9/2013)
Hi,

What does its execution plan say? Are there any missing indexes? How big are the tables? Check the statistics on them.

You can replace the IN with EXISTS and NOT EXISTS.

Regards
IgorMi

INdexes are there on the columns.. for less data it works fine but when data gets increased it takes more time to execute


Post the ACTUAL execution plan as a .sqlplan attachment so folks can analyse it.
Here's a close equivalent written in a more conventional style for your perusal:
SELECT y.id, y.english_name
FROM ys_movements y
INNER JOIN (
SELECT DISTINCT p.object_id
FROM profiles_useritems p
INNER JOIN django_content_type d
ON d.id = p.content_type_id
INNER JOIN follow_follow f
ON f.target_user_id = d.user_id
AND f.user_id = 8
WHERE d.name = 'movements'
AND d.app_label = 'yoga')
AND NOT EXISTS (
SELECT 1
FROM auth_user a
WHERE a.is_staff = 1
AND a.id = d.user_id)
) f
ON f.object_id = y.id
WHERE y.is_private =0



Kapil, as IgorMi and Chris suggested, EXISTS and NOT EXISTS works way faster that IN and NOT IN. The above query is perfect example of how it should be used. Eventually it gets better as the number of rows grow. cheers. :-)
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16338 Visits: 19552
sqlnaive (5/9/2013)
ChrisM@Work (5/9/2013)
kapil_kk (5/9/2013)
IgorMi (5/9/2013)
Hi,

What does its execution plan say? Are there any missing indexes? How big are the tables? Check the statistics on them.

You can replace the IN with EXISTS and NOT EXISTS.

Regards
IgorMi

INdexes are there on the columns.. for less data it works fine but when data gets increased it takes more time to execute


Post the ACTUAL execution plan as a .sqlplan attachment so folks can analyse it.
Here's a close equivalent written in a more conventional style for your perusal:
SELECT y.id, y.english_name
FROM ys_movements y
INNER JOIN (
SELECT DISTINCT p.object_id
FROM profiles_useritems p
INNER JOIN django_content_type d
ON d.id = p.content_type_id
INNER JOIN follow_follow f
ON f.target_user_id = d.user_id
AND f.user_id = 8
WHERE d.name = 'movements'
AND d.app_label = 'yoga')
AND NOT EXISTS (
SELECT 1
FROM auth_user a
WHERE a.is_staff = 1
AND a.id = d.user_id)
) f
ON f.object_id = y.id
WHERE y.is_private =0



Kapil, as IgorMi and Chris suggested, EXISTS and NOT EXISTS works way faster that IN and NOT IN. The above query is perfect example of how it should be used. Eventually it gets better as the number of rows grow. cheers. :-)


Not suggested by me. The performance of EXISTS and IN are very similar. Here's evidence and an explanation.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3099 Visits: 2766
Thanks all for the suggestion Smile

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Grant Fritchey
Grant Fritchey
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40184 Visits: 32654
Generally, but not always, instead of IN I would be doing a JOIN. The optimizer might interpret it that way any for you. Since you're going for matching records that may be a simpler and more clear approach. Other than that, I'd have to see the actual execution plan to understand where you may be experiencing performance issues.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search