MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2012, 07:08 PM
dumkat dumkat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 dumkat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 38 sec
Reputation Power: 0
Slow query

This query is taking too long. I am doing an intersect. The main query searches on term that can in be in few tables related to video including tag. I am intersecting the search query with a filter query that is based on 0 to * tags.
I am using an INNER JOIN on the search query and filter query.

I need a faster query.

Ignore the java ibatis injections.

SELECT v.* FROM video v
INNER JOIN channel_has_video chv ON v.video_id = chv.video_id
INNER JOIN channel c ON chv.channel_id = c.channel_id
LEFT JOIN video_has_tag vht ON v.video_id = vht.video_id
LEFT JOIN tag t ON vht.tag_tag_id = t.tag_id
LEFT JOIN video_has_artifact vha ON v.video_id = vha.video_id
LEFT JOIN artifact art ON vha.artifact_id = art.artifact_id
INNER JOIN
(
SELECT vv.* FROM video vv
INNER JOIN channel_has_video chvv ON vv.video_id = chvv.video_id
INNER JOIN channel cv ON chvv.channel_id = cv.channel_id
INNER JOIN video_has_tag vht_f ON vv.video_id = vht_f.video_id

WHERE vv.active = 1
AND( vht_f.tag_tag_id = #{pagination.tags[0]} OR vht_f.tag_tag_id = #{pagination.tags[1]})
) st
ON (st.video_id = v.video_id)
WHERE v.active = 1
AND c.active = 1
AND c.search_enabled = 1
AND ( v.title LIKE #{pagination.searchTermModified} OR v.description LIKE #{pagination.searchTermModified} OR t.tag_text LIKE #{pagination.searchTermModified} OR art.title LIKE #{pagination.searchTermModified}
OR art.description LIKE #{pagination.searchTermModified} )
GROUP BY v.video_id
LIMIT #{pagination.limitStart}, #{pagination.limit}


Thanks. Any help would be greatly appreciated.

Reply With Quote
  #2  
Old September 7th, 2012, 03:39 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,130 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 23 h 48 m 17 sec
Reputation Power: 1736
what is the value of "too long"? how many rows do you have?

You can try to use EXPLAIN SELECT to get an idea on how MySQL execute the query.

Reply With Quote
  #3  
Old September 7th, 2012, 08:18 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
Since you don't actually show us the value for
#{pagination.searchTermModified}

I'll ask do you have the wild card % at the beginning of your search term? If so your query will have to run a table scan.

Have you had a look at FULL TEXT INDEXES? that might be more appropriate for your search.

Reply With Quote
  #4  
Old September 7th, 2012, 09:33 AM
dumkat dumkat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 dumkat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 38 sec
Reputation Power: 0
search term modified is

'%search_term%'

video has 9999 records and tag has 4387.

I am trying to avoid full text indexes if i can.

Reply With Quote
  #5  
Old September 7th, 2012, 11:01 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
Quote:
Originally Posted by dumkat
search term modified is

'%search_term%'

So then you will always be faced with a table scan since no index can be used on a search beginning with the wild card.

Quote:
Originally Posted by dumkat
I am trying to avoid full text indexes if i can.


Why?

Reply With Quote
  #6  
Old September 7th, 2012, 11:06 AM
dumkat dumkat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 dumkat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 38 sec
Reputation Power: 0
How would I rewrite the query?

Reply With Quote
  #7  
Old September 7th, 2012, 11:11 AM
dumkat dumkat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 dumkat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 38 sec
Reputation Power: 0
also, the query really slows down when it is inner joining on the filter and that only uses tag_ids which are indexed.

Reply With Quote
  #8  
Old September 7th, 2012, 12:33 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 25 sec
Reputation Power: 4140
seems to me you are unnecessarily joining the same set of tables twice

first this --

FROM video v
INNER JOIN channel_has_video chv ON v.video_id = chv.video_id
INNER JOIN channel c ON chv.channel_id = c.channel_id
LEFT JOIN video_has_tag vht ON v.video_id = vht.video_id
LEFT JOIN tag t ON vht.tag_tag_id = t.tag_id
LEFT JOIN video_has_artifact vha ON v.video_id = vha.video_id
LEFT JOIN artifact art ON vha.artifact_id = art.artifact_id

then this --

FROM video vv
INNER JOIN channel_has_video chvv ON vv.video_id = chvv.video_id
INNER JOIN channel cv ON chvv.channel_id = cv.channel_id
INNER JOIN video_has_tag vht_0 ON vv.video_id = vht_0.video_id
INNER JOIN video_has_tag vht_1 ON vv.video_id = vht_1.video_id

what's up with that?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #9  
Old September 7th, 2012, 01:47 PM
dumkat dumkat is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 dumkat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 38 sec
Reputation Power: 0
I am filtering the first query which is a search by the inner joining it with the second query which is a filter on tag which can be 0 to * tags.

Although, I have changed the query for less joins in the original post.

Reply With Quote
  #10  
Old September 11th, 2012, 03:40 PM
sr sr is offline
Problem Solver
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2001
Location: Stockholm, Sweden
Posts: 4,430 sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 7 h 26 m 18 sec
Reputation Power: 532
1. What does an EXPLAIN for the query look like?

2. Do you have indexes on the joining columns?
__________________
/Stefan

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Slow query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap