473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

LEFT JOIN Optimization

Assistance needed to optimize this query
Expand|Select|Wrap|Line Numbers
  1. SELECT SD.content_id   AS Id, 
  2.        SD.title        AS Title,
  3.        CT.name         AS Contenttype,
  4.        PV.content_id   AS SponsorId,
  5.        PV.display_name AS Sponsor, 
  6.  
  7. CONCAT_WS("", NT.title, " - ", N.pubdate, " [", LG.id, "]") AS ListGroup,
  8.  
  9. CONCAT_WS("", N.format, " [", N.content_id, "]")            AS Newsletter,
  10.  
  11. L.description AS List,
  12. LG.id         AS ListGroupId,
  13.  
  14. DATE_FORMAT(UC.clicktime, "%Y-%m-%d") AS ClickDate,
  15.  
  16. COUNT(UC.id) AS Clicks FROM reporting.user_clicks UC 
  17.  
  18. INNER JOIN sl__content.sponsordeals SD ON SD.content_id = UC.content_id 
  19.  
  20. AND SD.version_id =  (SELECT live_version_id FROM sl__content.content C_SD WHERE C_SD.id = SD.content_id)
  21.  
  22. INNER JOIN sl__content.content C ON C.id = SD.content_id 
  23.  
  24. INNER JOIN sl__content.content_types CT ON CT.id = C.content_type_id 
  25.  
  26. LEFT JOIN sl__content.newsletters N ON N.content_id = UC.nlid 
  27.  
  28. AND N.version_id =  (SELECT live_version_id FROM sl__content.content C_N WHERE C_N.id = N.content_id) 
  29.  
  30. LEFT JOIN sl__content.content CP ON CP.id = UC.placementid 
  31.  
  32. LEFT OUTER JOIN sl__content.content_relationships CR_NT ON CR_NT.content_id = N.content_id AND CR_NT.content_version_id = 
  33. N.version_id AND CR_NT.relationship_id = 26 
  34.  
  35. LEFT OUTER JOIN sl__content.nl_types NT ON NT.content_id = CR_NT.related_content_id  AND NT.version_status = 'live'
  36.  
  37. LEFT OUTER JOIN sl__content.content_relationships CR_PV ON CR_PV.content_id = SD.content_id AND CR_PV.content_version_id = 
  38. SD.version_id AND CR_PV.relationship_id = 11 
  39.  
  40. LEFT OUTER JOIN sl__content.providers PV ON PV.content_id = CR_PV.related_content_id  AND PV.version_status = 'live' 
  41.  
  42. LEFT JOIN qb.qbLists L ON L.id = N.listid 
  43.  
  44. LEFT JOIN qb.qbListGroups LG ON LG.id = L.listGroupId WHERE 1 AND UC.nlid = 0 AND 
  45.  
  46. UC.clicktime BETWEEN '20070901000000' AND '20071001000000' 
  47.  
  48. GROUP BY SD.content_id, ClickDate;
  49.  
  50. This is the explain plan i get... it takes abt 30 min for this query to run..Need help desperately...
  51.  
  52.  

*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: SD
type: ALL
possible_keys: idx_content_id_version_id,idx_gordon_test
key: NULL
key_len: NULL
ref: NULL
rows: 13632
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: UC
type: ref
possible_keys: idx_nlid,idx_clicktime,idx_contentid
key: idx_contentid
key_len: 4
ref: sl__content.SD.content_id
rows: 1140
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: C
type: eq_ref
possible_keys: PRIMARY,content_type_id_idx
key: PRIMARY
key_len: 4
ref: sl__content.SD.content_id
rows: 1
Extra:
*************************** 4. row ***************************
id: 1
select_type: PRIMARY
table: CT
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: sl__content.C.content_type_id
rows: 1
Extra:
*************************** 5. row ***************************
id: 1
select_type: PRIMARY
table: N
type: ref
possible_keys: idx_content_id_version_id
key: idx_content_id_version_id
key_len: 4
ref: reporting.UC.nlid
rows: 1
Extra:
*************************** 6. row ***************************
id: 1
select_type: PRIMARY
table: CP
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: reporting.UC.placementid
rows: 1
Extra: Using index
*************************** 7. row ***************************
id: 1
select_type: PRIMARY
table: CR_NT
type: ref
possible_keys: relationship,content_id_version_idx
key: relationship
key_len: 12
ref: sl__content.N.content_id,sl__content.N.version_id, const
rows: 2
Extra: Using index
*************************** 8. row ***************************
id: 1
select_type: PRIMARY
table: NT
type: ref
possible_keys: idx_content_id_version_id,idx_status_content_id
key: idx_status_content_id
key_len: 5
ref: const,sl__content.CR_NT.related_content_id
rows: 1
Extra:
*************************** 9. row ***************************
id: 1
select_type: PRIMARY
table: CR_PV
type: ref
possible_keys: relationship,content_id_version_idx
key: relationship
key_len: 12
ref: sl__content.SD.content_id,sl__content.SD.version_i d,const
rows: 2
Extra: Using index
*************************** 10. row ***************************
id: 1
select_type: PRIMARY
table: PV
type: ref
possible_keys: idx_content_id_version_id,idx_status_content_id
key: idx_content_id_version_id
key_len: 4
ref: sl__content.CR_PV.related_content_id
rows: 1
Extra:
*************************** 11. row ***************************
id: 1
select_type: PRIMARY
table: L
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: sl__content.N.listid
rows: 1
Extra:
*************************** 12. row ***************************
id: 1
select_type: PRIMARY
table: LG
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: qb.L.listGroupID
rows: 1
Extra: Using index
*************************** 13. row ***************************
id: 3
select_type: DEPENDENT SUBQUERY
table: C_N
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: sl__content.N.content_id
rows: 1
Extra:
*************************** 14. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: C_SD
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: sl__content.SD.content_id
rows: 1
Extra:
14 rows in set (0.00 sec)
Oct 12 '07 #1
4 2692
pbmods
5,821 Expert 4TB
Heya, polycom. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=mysql]
MySQL code goes here.
[/CODE]

Whew, that query is a mess!

I'd rewrite it from scratch. There's subqueries where there should be joins, there are joins where they should probably just be taken out and made into separate queries and nobody took out the original "WHERE 1".
Oct 12 '07 #2
Heya, polycom. Welcome to TSDN!

Please use CODE tags when posting source code:

Expand|Select|Wrap|Line Numbers
  1. MySQL code goes here.
  2.  
Whew, that query is a mess!

I'd rewrite it from scratch. There's subqueries where there should be joins, there are joins where they should probably just be taken out and made into separate queries and nobody took out the original "WHERE 1".

Hey pbmods,

I appreciate you looking into this and you are right .i want to break the query into two parts and probably create a temporary table for one of them...I am a newbie and not well versed with query tunning. Can you give me a query struture which i can follow for this query. or any example....

Also can you let me know what problems did you find in the query structure or any donts that any Mysql DBA should keep in mind while writing a query or tuning.

I hope i am not asking a lot from you, however i would greatly appreciate your guidance.

regards
POLYCOM
Oct 15 '07 #3
pbmods
5,821 Expert 4TB
Heya, POLYCOM.

First thing (minor), if you don't want a separator, use CONCAT(), not CONCAT_WS().

The presence of LEFT OUTER JOIN suggests that you really just need to execute multiple queries.

It looks like you're searching for several types of unrelated data and trying to mash them together into one result set.

If you don't have the ability to simplify this query, I strongly recommend that you bring in a professional.
Oct 17 '07 #4
Heya, POLYCOM.

First thing (minor), if you don't want a separator, use CONCAT(), not CONCAT_WS().

The presence of LEFT OUTER JOIN suggests that you really just need to execute multiple queries.

It looks like you're searching for several types of unrelated data and trying to mash them together into one result set.

If you don't have the ability to simplify this query, I strongly recommend that you bring in a professional.

thanks for your input.

The user_clicks table is a merge table and has got around 40 merge tables, however the condition is to search only two tables(as you can see the range of dates) i want to know that is there a possibility of creating a temporary table from this query.
Oct 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marek Lewczuk | last post by:
Hello, I have a strange problem, maybe some of you will be able to explain me something. I use LEFT JOIN as a substitute for subselects. It's true that many subselects can be rewriten using LEFT...
0
by: Petre Agenbag | last post by:
Hi List Me again. I'm trying to return from multiple tables, the records that have field "information_sent" between two dates. The tables are all related by means of the id of the entry in the...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
2
by: tricard | last post by:
Good day all, I have a large outer joined query that I want to have some criteria. The select query is gathering all part numbers from tblPartNumbers, left joining to tblPartNumberVendor (since...
9
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to...
1
by: naveenchhibber | last post by:
Hi all pls tell me that the following statment is valid in oracle 9i or 10g.. update ws set received_by_facility = coalesce(rbf_ouk.organizational_unit_id, 0), ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.