Hi All,
Below is the detail on this SQL.
Rows returned : 28000
Time taken: 7 to 20 mins
Please let me know whether all joins are correct or not. Also this SQL return mentioned rows with different time as i mentioned. Any kind of suggestion will be deeply appriciated. - SELECT co1.org_name AS CSO_Name, coe.org_club_id_ext AS CSO_ID, co1.org_cst_key AS CSO_GUID, co1.org_ogt_code AS CSO_Type,
-
co2.org_name AS Sponsoring_Rotary_Club_Name, coe2.org_club_id_ext AS Spon_ID, co2.org_cst_key AS SPON_GUID, co3.org_name AS District,
-
(SELECT cst_sort_name_dn
-
FROM dbo.co_customer WITH (nolock)
-
WHERE (cst_key = vcrd.r42_zone_cst_key)) AS Zone, dbo.vw_client_ri_cso_org_status.v03_org_status AS Status,
-
coe.org_cso_certifydate_ext AS Organization_Date, co1.org_date_founded AS Certification_Date, coe.org_cso_homehospyn_ext AS [Home Host],
-
coe.org_cso_partnershipprojyn_ext AS [Partnered Club], coe.org_cso_twinclubyn_ext AS [Twin Club], coe.org_cso_clubbase_ext AS Community_School,
-
dbo.fn_client_ri_get_full_address(co1.org_cst_key) AS CSO_Full_Address, dbo.co_address.adr_country AS CSO_Country,
-
(SELECT r00_language_name
-
FROM dbo.client_ri_language WITH (nolock)
-
WHERE (r00_key = coe.org_language_ext)) AS Language
-
FROM dbo.co_organization AS co1 WITH (nolock) INNER JOIN
-
dbo.co_customer_x_customer AS cxc1 WITH (nolock) ON co1.org_cst_key = cxc1.cxc_cst_key_2 AND co1.org_ogt_code IN ('RCC', 'Rotaract Club') AND
-
cxc1.cxc_delete_flag = 0 INNER JOIN
-
dbo.co_organization_ext AS coe WITH (nolock) ON coe.org_cst_key_ext = co1.org_cst_key LEFT OUTER JOIN
-
dbo.client_ri_r42_org_structure_by_date AS vcrd WITH (nolock) ON vcrd.r42_club_cst_key = cxc1.cxc_cst_key_1 AND
-
(vcrd.r42_end_date >= GETDATE() OR
-
vcrd.r42_end_date IS NULL) AND vcrd.r42_effective_date <= GETDATE() LEFT OUTER JOIN
-
dbo.co_organization AS co2 WITH (nolock) ON vcrd.r42_club_cst_key = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization_ext AS coe2 WITH (nolock) ON coe2.org_cst_key_ext = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization AS co3 WITH (nolock) ON vcrd.r42_district_cst_key = co3.org_cst_key LEFT OUTER JOIN
-
dbo.vw_client_ri_zone AS vrz WITH (nolock) ON vrz.child_orgid = vcrd.r42_district_cst_key LEFT OUTER JOIN
-
dbo.vw_client_ri_cso_org_status ON co1.org_cst_key = dbo.vw_client_ri_cso_org_status.v03_org_cst_key INNER JOIN
-
dbo.co_customer_x_address WITH (nolock) ON dbo.co_customer_x_address.cxa_cst_key = co1.org_cst_key AND
-
dbo.co_customer_x_address.cxa_delete_flag = 0 LEFT OUTER JOIN
-
dbo.co_address WITH (nolock) ON dbo.co_address.adr_key = dbo.co_customer_x_address.cxa_adr_key AND
-
co1.org_cst_key = dbo.co_address.adr_cst_key_owner AND dbo.co_address.adr_delete_flag = 0 AND
-
dbo.co_address.adr_bad_address_flag = 0 INNER JOIN
-
dbo.co_customer AS co WITH (nolock) ON co1.org_cst_key = co.cst_key AND co.cst_cxa_key = dbo.co_customer_x_address.cxa_key
-
UNION
-
SELECT co1.org_name AS CSO_Name, coe.org_club_id_ext AS CSO_ID, co1.org_cst_key AS CSO_GUID, co1.org_ogt_code AS CSO_Type,
-
co2.org_name AS Sponsoring_Rotary_Club_Name, coe2.org_club_id_ext AS Spon_ID, co2.org_cst_key AS SPON_GUID, co3.org_name AS District,
-
(SELECT cst_sort_name_dn
-
FROM dbo.co_customer
-
WHERE (cst_key = vcrd.r42_zone_cst_key)) AS Zone, dbo.vw_client_ri_cso_org_status.v03_org_status AS Status,
-
coe.org_cso_certifydate_ext AS Organization_Date, co1.org_date_founded AS Certification_Date, coe.org_cso_homehospyn_ext AS [Home Host],
-
coe.org_cso_partnershipprojyn_ext AS [Partnered Club], coe.org_cso_twinclubyn_ext AS [Twin Club], coe.org_cso_clubbase_ext AS Community_School,
-
dbo.fn_client_ri_get_full_address(co1.org_cst_key) AS CSO_Full_Address, dbo.co_address.adr_country AS CSO_Country,
-
(SELECT r00_language_name
-
FROM dbo.client_ri_language
-
WHERE (r00_key = coe.org_language_ext)) AS Language
-
FROM dbo.co_organization AS co1 WITH (nolock) INNER JOIN
-
dbo.co_customer_x_customer AS cxc1 WITH (nolock) ON co1.org_cst_key = cxc1.cxc_cst_key_1 AND co1.org_ogt_code IN ('RCC', 'Rotaract Club') AND
-
cxc1.cxc_delete_flag = 0 INNER JOIN
-
dbo.co_organization_ext AS coe WITH (nolock) ON coe.org_cst_key_ext = co1.org_cst_key LEFT OUTER JOIN
-
dbo.client_ri_r42_org_structure_by_date AS vcrd WITH (nolock) ON vcrd.r42_club_cst_key = cxc1.cxc_cst_key_2 AND
-
(vcrd.r42_end_date >= GETDATE() OR
-
vcrd.r42_end_date IS NULL) AND vcrd.r42_effective_date <= GETDATE() LEFT OUTER JOIN
-
dbo.co_organization AS co2 WITH (nolock) ON vcrd.r42_club_cst_key = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization_ext AS coe2 WITH (nolock) ON coe2.org_cst_key_ext = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization AS co3 WITH (nolock) ON vcrd.r42_district_cst_key = co3.org_cst_key LEFT OUTER JOIN
-
dbo.vw_client_ri_zone AS vrz WITH (nolock) ON vrz.child_orgid = vcrd.r42_district_cst_key LEFT OUTER JOIN
-
dbo.vw_client_ri_cso_org_status ON co1.org_cst_key = dbo.vw_client_ri_cso_org_status.v03_org_cst_key INNER JOIN
-
dbo.co_customer_x_address WITH (nolock) ON dbo.co_customer_x_address.cxa_cst_key = co1.org_cst_key AND
-
dbo.co_customer_x_address.cxa_delete_flag = 0 LEFT OUTER JOIN
-
dbo.co_address WITH (nolock) ON dbo.co_address.adr_key = dbo.co_customer_x_address.cxa_adr_key AND
-
co1.org_cst_key = dbo.co_address.adr_cst_key_owner AND dbo.co_address.adr_delete_flag = 0 AND
-
dbo.co_address.adr_bad_address_flag = 0 INNER JOIN
-
dbo.co_customer AS co WITH (nolock) ON co1.org_cst_key = co.cst_key AND co.cst_cxa_key = dbo.co_customer_x_address.cxa_key
Kindly let me know if you need more information on this.
Kind Regds,
Bhushan
4 1615 ck9663 2,878
Recognized Expert Specialist
1. Check your indexes.
2. If you don't need to remove duplicate, use UNION ALL instead of UNION (only)
3. You have sub-query resulting to a field. This will result to a ROW-BY-ROW processing, actually. Since this query has to run for every record, consider another technique.
4. Re-arrange your condition:
For AND, place the first condition that will probably return FALSE first.
For OR, place the first condition that will probably return TRUE first.
Good luck!!!
--- CK
Hi
Thanks for ur reply!!!
btw i hv removed the union since there was only one difference in where condition of both SQL's as below. - (co1.org_cst_key = cxc1.cxc_cst_key_1 OR
-
co1.org_cst_key = cxc1.cxc_cst_key_2)
So my latest SQL is below. I hv also chked wt indexes but still this SQL getting time to execute. Pls provide me some more good suggestion related wt below SQL. thanks in advance - SELECT co1.org_name AS CSO_Name, coe.org_club_id_ext AS CSO_ID, co1.org_cst_key AS CSO_GUID, co1.org_ogt_code AS CSO_Type,
-
co2.org_name AS Sponsoring_Rotary_Club_Name, coe2.org_club_id_ext AS Spon_ID, co2.org_cst_key AS SPON_GUID, co3.org_name AS District,
-
(SELECT cst_sort_name_dn
-
FROM dbo.co_customer WITH (nolock)
-
WHERE (cst_key = vcrd.r42_zone_cst_key)) AS Zone,
-
(SELECT v03_org_status
-
FROM dbo.vw_client_ri_cso_org_status(nolock)
-
WHERE co1.org_cst_key = v03_org_cst_key) AS Status, coe.org_cso_certifydate_ext AS Organization_Date,
-
co1.org_date_founded AS Certification_Date, coe.org_cso_homehospyn_ext AS [Home Host], coe.org_cso_partnershipprojyn_ext AS [Partnered Club],
-
coe.org_cso_twinclubyn_ext AS [Twin Club], coe.org_cso_clubbase_ext AS Community_School,
-
Rtrim(Ltrim(replace(cxa_mailing_label, CASE
-
(SELECT cst_type
-
FROM co_customer(NOLOCK)
-
WHERE cst_key = co1.org_cst_key) WHEN 'Individual' THEN co.cst_ind_full_name_dn ELSE co.cst_org_name_dn END, ''))) CSO_Full_Address,
-
dbo.co_address.adr_country AS CSO_Country,
-
(SELECT r00_language_name
-
FROM dbo.client_ri_language WITH (nolock)
-
WHERE (r00_key = coe.org_language_ext)) AS Language
-
FROM dbo.co_organization AS co1 WITH (nolock) INNER JOIN
-
dbo.co_customer_x_customer AS cxc1 WITH (nolock) ON ((co1.org_cst_key = cxc1.cxc_cst_key_1 OR
-
co1.org_cst_key = cxc1.cxc_cst_key_2) AND co1.org_ogt_code IN ('RCC', 'Rotaract Club') AND cxc1.cxc_delete_flag = 0) INNER JOIN
-
dbo.co_organization_ext AS coe WITH (nolock) ON coe.org_cst_key_ext = co1.org_cst_key LEFT OUTER JOIN
-
dbo.client_ri_r42_org_structure_by_date AS vcrd WITH (nolock) ON ((vcrd.r42_club_cst_key = cxc1.cxc_cst_key_2 OR
-
vcrd.r42_club_cst_key = cxc1.cxc_cst_key_1) AND (vcrd.r42_end_date >= GETDATE() OR
-
vcrd.r42_end_date IS NULL) AND vcrd.r42_effective_date <= GETDATE()) LEFT OUTER JOIN
-
dbo.co_organization AS co2 WITH (nolock) ON vcrd.r42_club_cst_key = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization_ext AS coe2 WITH (nolock) ON coe2.org_cst_key_ext = co2.org_cst_key LEFT OUTER JOIN
-
dbo.co_organization AS co3 WITH (nolock) ON vcrd.r42_district_cst_key = co3.org_cst_key LEFT OUTER JOIN
-
dbo.co_customer_x_address WITH (nolock) ON (dbo.co_customer_x_address.cxa_cst_key = co1.org_cst_key AND
-
dbo.co_customer_x_address.cxa_delete_flag = 0) LEFT OUTER JOIN
-
dbo.co_address WITH (nolock) ON (dbo.co_address.adr_key = dbo.co_customer_x_address.cxa_adr_key AND
-
co1.org_cst_key = dbo.co_address.adr_cst_key_owner AND dbo.co_address.adr_delete_flag = 0 AND dbo.co_address.adr_bad_address_flag = 0)
-
INNER JOIN dbo.co_customer AS co WITH (nolock) ON (co1.org_cst_key = co.cst_key AND co.cst_cxa_key = dbo.co_customer_x_address.cxa_key)
@ck9663 ck9663 2,878
Recognized Expert Specialist
Try converting those sub-queries into a left join
Happy Coding!!!
--- CK
Try this -
SELECT
-
co1.org_name AS CSO_Name,
-
coe.org_club_id_ext AS CSO_ID,
-
co1.org_cst_key AS CSO_GUID,
-
co1.org_ogt_code AS CSO_Type,
-
co2.org_name AS Sponsoring_Rotary_Club_Name,
-
coe2.org_club_id_ext AS Spon_ID,
-
co2.org_cst_key AS SPON_GUID,
-
co3.org_name AS District,
-
cc.cst_sort_name_dn AS [Zone],
-
vcrcos.v03_org_status AS [Status],
-
coe.org_cso_certifydate_ext AS Organization_Date,
-
co1.org_date_founded AS Certification_Date,
-
coe.org_cso_homehospyn_ext AS [Home Host],
-
coe.org_cso_partnershipprojyn_ext AS [Partnered Club],
-
coe.org_cso_twinclubyn_ext AS [Twin Club],
-
coe.org_cso_clubbase_ext AS Community_School,
-
RTRIM(Ltrim(REPLACE(cxa_mailing_label, CASE cc2.cst_type WHEN 'Individual' THEN co.cst_ind_full_name_dn ELSE co.cst_org_name_dn END, ''))) AS CSO_Full_Address,
-
dbo.co_address.adr_country AS CSO_Country,
-
crl.r00_language_name AS [Language]
-
-
FROM dbo.co_organization AS co1 WITH (NOLOCK)
-
-
--ADDED
-
LEFT JOIN co_customer AS cc2 WITH (NOLOCK)
-
ON co1.org_cst_key = cc2.cst_key
-
-
--ADDED
-
LEFT JOIN dbo.vw_client_ri_cso_org_status as vcrcos WITH (NOLOCK)
-
ON co1.org_cst_key = vcrcos.v03_org_cst_key
-
-
INNER JOIN dbo.co_customer_x_customer AS cxc1 WITH (NOLOCK)
-
ON((co1.org_cst_key = cxc1.cxc_cst_key_1 OR co1.org_cst_key = cxc1.cxc_cst_key_2)
-
AND co1.org_ogt_code IN ('RCC','Rotaract Club')
-
AND cxc1.cxc_delete_flag = 0)
-
-
INNER JOIN dbo.co_organization_ext AS coe WITH (NOLOCK)
-
-
--ADDED
-
LEFT JOIN dbo.client_ri_language AS crl WITH (NOLOCK)
-
ON r00_key = coe.org_language_ext
-
-
ON coe.org_cst_key_ext = co1.org_cst_key
-
-
LEFT OUTER JOIN dbo.client_ri_r42_org_structure_by_date AS vcrd WITH (NOLOCK)
-
-
--ADDED
-
LEFT JOIN dbo.co_customer AS cc WITH (NOLOCK)
-
ON vcrd.r42_zone_cst_key = cc.cst_key
-
-
ON((vcrd.r42_club_cst_key = cxc1.cxc_cst_key_2 OR vcrd.r42_club_cst_key = cxc1.cxc_cst_key_1)
-
AND(vcrd.r42_end_date >= GETDATE() OR vcrd.r42_end_date IS NULL)
-
AND vcrd.r42_effective_date <= GETDATE())
-
-
LEFT OUTER JOIN dbo.co_organization AS co2 WITH (NOLOCK)
-
ON vcrd.r42_club_cst_key = co2.org_cst_key
-
-
LEFT OUTER JOIN dbo.co_organization_ext AS coe2 WITH (NOLOCK)
-
ON coe2.org_cst_key_ext = co2.org_cst_key
-
-
LEFT OUTER JOIN dbo.co_organization AS co3 WITH (NOLOCK)
-
ON vcrd.r42_district_cst_key = co3.org_cst_key
-
-
LEFT OUTER JOIN dbo.co_customer_x_address WITH (NOLOCK)
-
ON dbo.co_customer_x_address.cxa_cst_key = co1.org_cst_key AND dbo.co_customer_x_address.cxa_delete_flag = 0
-
-
LEFT OUTER JOIN dbo.co_address WITH (NOLOCK)
-
ON(dbo.co_address.adr_key = dbo.co_customer_x_address.cxa_adr_key
-
AND co1.org_cst_key = dbo.co_address.adr_cst_key_owner
-
AND dbo.co_address.adr_delete_flag = 0
-
AND dbo.co_address.adr_bad_address_flag = 0)
-
-
INNER JOIN dbo.co_customer AS co WITH (NOLOCK)
-
ON co1.org_cst_key = co.cst_key AND co.cst_cxa_key = dbo.co_customer_x_address.cxa_key
-
-
- need to see logic probably you may combine cc and cc2
- it is very depend on size of all table which participated in query
i
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: C. Alexander |
last post by:
Hey all.
I have a whiteboard type program.
I have 2 picturebox's (needed for their mousemove/mousedown events)
Picture1.Picture = MyPicture
' Here is my hidden picture used to replace (on erase) Picture1
Picture2.Picture = MyPicture
|
by: Joan MacEachern |
last post by:
Join Sun's Chief Strategy Officer, Mark Tolliver on
Wednesday, December 3, 2003 at 8 am PST for Sun's Network
Computing O3-Q4 Event. This event's focus includes product
releases that will help you scale out and drive network
costs down. Videocasts of Sun leaders detailing how to use
network computing for business advantage are also included.
Sign up for an e-mail reminder:
http://sun.com/bignews49
|
by: Jason |
last post by:
Does anyone know? Thanks so much.
|
by: norton |
last post by:
Dear All,
our team is writing a large appz and now we takes minutes to compile the
source code.
May i ask if there are any method to speed up the compile process?
(any settings to tell vs.net to re-compile only for the modified area?)
(is there any parallel process function which allows the host computer to
get cpu rescources from other guest computers)
|
by: |
last post by:
I am trying to improve the robustness and elegance of my parametized sql
statements in ASP 3.0 as they get passed to the sql server SP.
Could anyone tell me if there are weaknessess in the way I have written the
following code? I have included both the asp code and the sql stored
proceducre to tie things togoether....I appreciate any advice on this. It
basically is a application to manage static news stories on our site by
tracking and...
| |
by: gilgantic |
last post by:
HELP!!!
I am trying to fine tune or rewrite my SELECT statement which has a
combination of SUM and CASE statements. The values are accurate, but
the query is slow.
BUSINESS RULE
=============
1. Add up Count1 when FIELD_1 has a value and FIELD_2 is NULL, or both
have a value.
2. Add up Count2 when FIELD_2 has a value and FIELD_1 is NULL.
|
by: Hans Horn |
last post by:
Howdy,
is there a way to speed-up or tune the database creation?
On my laptop (DB2-UDB PE) it takes 2 minutes to create a virgin database.
This virgin, btw. is 30MBytes heavy.
Is there a way to reduce its size?
H.
|
by: kvsnramesh |
last post by:
hi,
I have a problem asked by one of my senior person and finding the
answer .
What is the step by step procedure for tune a large sql query.
OR how do we tune a large SQL query with somany joins
|
by: rshivaraman |
last post by:
Hi :
I have a TableA with around 10 columns with varchar and numeric
datatypes
It has 500 million records and its size is 999999999 KB. i believe it
is kb
i got this data after running sp_spaceused on it. The index_size was
also pretty big in 6 digits.
On looking at the tableA
|
by: travolta001 |
last post by:
Speed up your system, repair registry base, uninstall hidden windows
components and get rid of boring errors with powerful tips and tricks.
Very useful tips from other good sites.
http://windowsxpsp2pro.blogspot.com
|
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...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |