473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LEFT JOIN Optimization

4 New Member
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_cl icktime,idx_con tentid
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.c ontent_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.nl id
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.pl acementid
rows: 1
Extra: Using index
*************** ************ 7. row *************** ************
id: 1
select_type: PRIMARY
table: CR_NT
type: ref
possible_keys: relationship,co ntent_id_versio n_idx
key: relationship
key_len: 12
ref: sl__content.N.c ontent_id,sl__c ontent.N.versio n_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_cont ent_id
key_len: 5
ref: const,sl__conte nt.CR_NT.relate d_content_id
rows: 1
Extra:
*************** ************ 9. row *************** ************
id: 1
select_type: PRIMARY
table: CR_PV
type: ref
possible_keys: relationship,co ntent_id_versio n_idx
key: relationship
key_len: 12
ref: sl__content.SD. content_id,sl__ content.SD.vers ion_id,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_cont ent_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.l istid
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.listGroupI D
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.c ontent_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 2714
pbmods
5,821 Recognized Expert Expert
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
polycom
4 New Member
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 Recognized Expert Expert
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
polycom
4 New Member
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
2450
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 JOIN. I have made a query which use LEFT JOIN statement and... when there are many LEFT JOIN's (over 3) on the same table MySQL execute this query very long time... few hours or more. Maybe there is something wrong with my table structures... Are...
0
13011
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 main table, ie.. main id entity_name ...
0
2363
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 null default 0 auto_increment, a1 varchar(64) not null default '',
1
3456
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 in trees which form parent-child relationships, sort of like newsgroups. For example, the parent_id field points to another element. Indent_level is there for denormalization purposes, to avoid costly recursive issues in querying. The...
4
4107
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 results, then doing a self left join with those results, etc. What puzzles me is that the time required for the query seems to grow exponentially as I add additional left joins, which I didn't expect. I expected the inner select to return about 25...
3
23100
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 he doesn't know how to do that, or even if he can, and i don't have to time to learn DB2 from scratch right now. The following SQL Query is a trimmed sample of the full View (i.e. Logical) definition - and i would create it on an SQL based...
2
3143
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 more than one vendor can make the part), then left joining to tblPartNumberSupplier (since more than one supplier can distribute the vendor's part), then left joining to tblPartNumberCost (since more than one cost can be associated with a single...
9
9227
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 filter my left table in the WHERE clause and cannot filter it in the FROM clause. This seems like it would cause a lot of overhead especially when my left table is ten thousand rows. Am I wrong in thinking that the two tables get joined with the...
1
4037
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), assigned_to_facility = coalesce(atf_ouk.organizational_unit_id, 0), received_by_team = coalesce(rbt_ouk.organizational_unit_id, 0), assigned_to_team = coalesce(att_ouk.organizational_unit_id, 0)
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.