dbTalk Databases Forums  

Nasty Query Here

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Nasty Query Here in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mtek
 
Posts: n/a

Default Nasty Query Here - 04-09-2008 , 12:51 PM







Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

SELECT c.email || '|' || ca.first_name || '|' || ca.last_name ||
'|' || 'ZACKS' || '|' ||
TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') || '|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' ||
v.adid || '|' || cd.day_phone line, 'HOUSE' list_type
FROM customer c, customer_account ca, visitor v,
subscr_email_product s, customer_address cd
WHERE c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT email
FROM customer c, customer.subscriptions s, customer.product p,
customer_address ca, customer_account ct, visitor v
WHERE ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT c.email || '|' || ca.first_name || '|' || ca.last_name ||
'|' || 'ZACKS' || '|' ||
TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') || '|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' ||
v.adid || '|' || cd.day_phone line, 'AMERITRADE' list_type
FROM customer c, customer_account ca, visitor v,
subscr_email_product s, customer_address cd
WHERE c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT email
FROM customer c, customer.subscriptions s, customer.product p,
customer_address ca, customer_account ct, visitor v
WHERE ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

Reply With Quote
  #2  
Old   
Charles Hooper
 
Posts: n/a

Default Re: Nasty Query Here - 04-09-2008 , 07:30 PM






On Apr 9, 1:51*pm, Mtek <m... (AT) mtekusa (DOT) com> wrote:
Quote:
Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. *I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

Are you wanting the query to be smaller, or execute faster?

Slightly reformatted:
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'HOUSE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'AMERITRADE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

* You are using a UNION between the two halves of the SQL statement,
when there is no chance of duplicates between the two halves - use
UNION ALL instead if possible.
* You are using TRUNC(start_date) in the subquery, if there is an
index on this column, it will not be used, unless it is a function
based index on TRUNC(start_date). If there is a time component to the
start_date column, consider using the following:
AND start_date BETWEEN p_start_date AND (p_end_date + 0.999)
* The two halves of the SQL statement are nearly the same except that
one half is looking for the email address to be in the subquery, and
the other with the email address not in the same subquery. Consider
converting the subquery into an inline view (will need to add
DISTINCT) and outer joining it to the main query, and use either NVL2
or DECODE to return either 'AMERITRADE' or 'HOUSE' depending on if the
email returned from the inline view is null or not.
* You are selecting from a large number of tables - do you need to
select from all of those tables, and are all restrictions specified to
minimize the number of rows returned?
* You are not specifying from which table all columns will be
retrieved - you may want to fix that.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.


Reply With Quote
  #3  
Old   
Charles Hooper
 
Posts: n/a

Default Re: Nasty Query Here - 04-09-2008 , 07:30 PM



On Apr 9, 1:51*pm, Mtek <m... (AT) mtekusa (DOT) com> wrote:
Quote:
Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. *I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

Are you wanting the query to be smaller, or execute faster?

Slightly reformatted:
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'HOUSE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'AMERITRADE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

* You are using a UNION between the two halves of the SQL statement,
when there is no chance of duplicates between the two halves - use
UNION ALL instead if possible.
* You are using TRUNC(start_date) in the subquery, if there is an
index on this column, it will not be used, unless it is a function
based index on TRUNC(start_date). If there is a time component to the
start_date column, consider using the following:
AND start_date BETWEEN p_start_date AND (p_end_date + 0.999)
* The two halves of the SQL statement are nearly the same except that
one half is looking for the email address to be in the subquery, and
the other with the email address not in the same subquery. Consider
converting the subquery into an inline view (will need to add
DISTINCT) and outer joining it to the main query, and use either NVL2
or DECODE to return either 'AMERITRADE' or 'HOUSE' depending on if the
email returned from the inline view is null or not.
* You are selecting from a large number of tables - do you need to
select from all of those tables, and are all restrictions specified to
minimize the number of rows returned?
* You are not specifying from which table all columns will be
retrieved - you may want to fix that.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.


Reply With Quote
  #4  
Old   
Charles Hooper
 
Posts: n/a

Default Re: Nasty Query Here - 04-09-2008 , 07:30 PM



On Apr 9, 1:51*pm, Mtek <m... (AT) mtekusa (DOT) com> wrote:
Quote:
Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. *I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

Are you wanting the query to be smaller, or execute faster?

Slightly reformatted:
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'HOUSE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'AMERITRADE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

* You are using a UNION between the two halves of the SQL statement,
when there is no chance of duplicates between the two halves - use
UNION ALL instead if possible.
* You are using TRUNC(start_date) in the subquery, if there is an
index on this column, it will not be used, unless it is a function
based index on TRUNC(start_date). If there is a time component to the
start_date column, consider using the following:
AND start_date BETWEEN p_start_date AND (p_end_date + 0.999)
* The two halves of the SQL statement are nearly the same except that
one half is looking for the email address to be in the subquery, and
the other with the email address not in the same subquery. Consider
converting the subquery into an inline view (will need to add
DISTINCT) and outer joining it to the main query, and use either NVL2
or DECODE to return either 'AMERITRADE' or 'HOUSE' depending on if the
email returned from the inline view is null or not.
* You are selecting from a large number of tables - do you need to
select from all of those tables, and are all restrictions specified to
minimize the number of rows returned?
* You are not specifying from which table all columns will be
retrieved - you may want to fix that.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.


Reply With Quote
  #5  
Old   
Charles Hooper
 
Posts: n/a

Default Re: Nasty Query Here - 04-09-2008 , 07:30 PM



On Apr 9, 1:51*pm, Mtek <m... (AT) mtekusa (DOT) com> wrote:
Quote:
Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. *I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

Are you wanting the query to be smaller, or execute faster?

Slightly reformatted:
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'HOUSE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'AMERITRADE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

* You are using a UNION between the two halves of the SQL statement,
when there is no chance of duplicates between the two halves - use
UNION ALL instead if possible.
* You are using TRUNC(start_date) in the subquery, if there is an
index on this column, it will not be used, unless it is a function
based index on TRUNC(start_date). If there is a time component to the
start_date column, consider using the following:
AND start_date BETWEEN p_start_date AND (p_end_date + 0.999)
* The two halves of the SQL statement are nearly the same except that
one half is looking for the email address to be in the subquery, and
the other with the email address not in the same subquery. Consider
converting the subquery into an inline view (will need to add
DISTINCT) and outer joining it to the main query, and use either NVL2
or DECODE to return either 'AMERITRADE' or 'HOUSE' depending on if the
email returned from the inline view is null or not.
* You are selecting from a large number of tables - do you need to
select from all of those tables, and are all restrictions specified to
minimize the number of rows returned?
* You are not specifying from which table all columns will be
retrieved - you may want to fix that.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.


Reply With Quote
  #6  
Old   
patrick
 
Posts: n/a

Default Re: Nasty Query Here - 04-11-2008 , 12:46 PM




Very confusing
query has customer_account ca
subquery has customer_address ca
while ca is now customer_account

May want to look at rewriting the subquery as exists/not exists
something like
and not exists (select 'x' from customer c2,
customer.subscriptions s2,
customer.product p2,
customer_address ca2,
customer_account ct2,
visitor v2
where c2.customer_id = c.customer_id
and c2.email = c.email
and ca2.customer_id = c2.customer_id
and ca2.address_type_id = 1
and s2.customer_id = c2.customer_id
and s2.status = 1
and s2.sell_rep_id IN (201, 202)
and p2.produst_id = s2.produst_id
and p2.produst_id = 1
and TRUNC(start_date) BETWEEN p_start_date AND
p_end_date
and ct2.customer_id = c2.customer_id
and v.customer_id (+) = c2.customer_id)

Also are customer_account ct2 and visitor v2 really
needed in the subquery.

====>Pat




Reply With Quote
  #7  
Old   
patrick
 
Posts: n/a

Default Re: Nasty Query Here - 04-11-2008 , 12:46 PM




Very confusing
query has customer_account ca
subquery has customer_address ca
while ca is now customer_account

May want to look at rewriting the subquery as exists/not exists
something like
and not exists (select 'x' from customer c2,
customer.subscriptions s2,
customer.product p2,
customer_address ca2,
customer_account ct2,
visitor v2
where c2.customer_id = c.customer_id
and c2.email = c.email
and ca2.customer_id = c2.customer_id
and ca2.address_type_id = 1
and s2.customer_id = c2.customer_id
and s2.status = 1
and s2.sell_rep_id IN (201, 202)
and p2.produst_id = s2.produst_id
and p2.produst_id = 1
and TRUNC(start_date) BETWEEN p_start_date AND
p_end_date
and ct2.customer_id = c2.customer_id
and v.customer_id (+) = c2.customer_id)

Also are customer_account ct2 and visitor v2 really
needed in the subquery.

====>Pat




Reply With Quote
  #8  
Old   
patrick
 
Posts: n/a

Default Re: Nasty Query Here - 04-11-2008 , 12:46 PM




Very confusing
query has customer_account ca
subquery has customer_address ca
while ca is now customer_account

May want to look at rewriting the subquery as exists/not exists
something like
and not exists (select 'x' from customer c2,
customer.subscriptions s2,
customer.product p2,
customer_address ca2,
customer_account ct2,
visitor v2
where c2.customer_id = c.customer_id
and c2.email = c.email
and ca2.customer_id = c2.customer_id
and ca2.address_type_id = 1
and s2.customer_id = c2.customer_id
and s2.status = 1
and s2.sell_rep_id IN (201, 202)
and p2.produst_id = s2.produst_id
and p2.produst_id = 1
and TRUNC(start_date) BETWEEN p_start_date AND
p_end_date
and ct2.customer_id = c2.customer_id
and v.customer_id (+) = c2.customer_id)

Also are customer_account ct2 and visitor v2 really
needed in the subquery.

====>Pat




Reply With Quote
  #9  
Old   
patrick
 
Posts: n/a

Default Re: Nasty Query Here - 04-11-2008 , 12:46 PM




Very confusing
query has customer_account ca
subquery has customer_address ca
while ca is now customer_account

May want to look at rewriting the subquery as exists/not exists
something like
and not exists (select 'x' from customer c2,
customer.subscriptions s2,
customer.product p2,
customer_address ca2,
customer_account ct2,
visitor v2
where c2.customer_id = c.customer_id
and c2.email = c.email
and ca2.customer_id = c2.customer_id
and ca2.address_type_id = 1
and s2.customer_id = c2.customer_id
and s2.status = 1
and s2.sell_rep_id IN (201, 202)
and p2.produst_id = s2.produst_id
and p2.produst_id = 1
and TRUNC(start_date) BETWEEN p_start_date AND
p_end_date
and ct2.customer_id = c2.customer_id
and v.customer_id (+) = c2.customer_id)

Also are customer_account ct2 and visitor v2 really
needed in the subquery.

====>Pat




Reply With Quote
  #10  
Old   
Mtek
 
Posts: n/a

Default Re: Nasty Query Here - 04-16-2008 , 10:30 AM



On Apr 9, 7:30 pm, Charles Hooper <hooperc2... (AT) yahoo (DOT) com> wrote:
Quote:
On Apr 9, 1:51 pm, Mtek <m... (AT) mtekusa (DOT) com> wrote:

Hi,

I have a REAL nasty query here which I'm hoping to make smaller by
using some joins. I got help from here before, so while I'm trying to
do it, I was hoping some others can look at it also.

Thank you,

Are you wanting the query to be smaller, or execute faster?

Slightly reformatted:
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'HOUSE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email NOT IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+))
UNION
SELECT
c.email || '|' || ca.first_name || '|' || ca.last_name || '|' ||
'ZACKS' || '|' || TO_CHAR(c.date_registered, 'mmddrr hh24:mi:ss') ||
'|' ||
TO_CHAR(TRUNC(SYSDATE-1),'MM-DD-YYYY') || '|' || v.adid || '|' ||
cd.day_phone line,
'AMERITRADE' list_type
FROM
customer c,
customer_account ca,
visitor v,
subscr_email_product s,
customer_address cd
WHERE
c.date_registered BETWEEN p_start_date AND p_end_date
AND c.customer_id = ca.customer_id(+)
AND c.customer_id = s.customer_id
AND c.customer_id = v.customer_id
AND c.customer_id = cd.customer_id(+)
AND s.email_product_id = 'HL'
AND email IN (
SELECT
email
FROM
customer c,
customer.subscriptions s,
customer.product p,
customer_address ca,
customer_account ct,
visitor v
WHERE
ca.address_type_id = 1
AND s.status = 1
AND s.sell_rep_id IN (201, 202)
AND p.produst_id = 1
AND TRUNC(start_date) BETWEEN p_start_date AND p_end_date
AND p.produst_id = s.produst_id
AND c.customer_id = s.customer_id
AND c.customer_id = ca.customer_id
AND c.customer_id = ct.customer_id
AND c.customer_id = v.customer_id (+));

* You are using a UNION between the two halves of the SQL statement,
when there is no chance of duplicates between the two halves - use
UNION ALL instead if possible.
* You are using TRUNC(start_date) in the subquery, if there is an
index on this column, it will not be used, unless it is a function
based index on TRUNC(start_date). If there is a time component to the
start_date column, consider using the following:
AND start_date BETWEEN p_start_date AND (p_end_date + 0.999)
* The two halves of the SQL statement are nearly the same except that
one half is looking for the email address to be in the subquery, and
the other with the email address not in the same subquery. Consider
converting the subquery into an inline view (will need to add
DISTINCT) and outer joining it to the main query, and use either NVL2
or DECODE to return either 'AMERITRADE' or 'HOUSE' depending on if the
email returned from the inline view is null or not.
* You are selecting from a large number of tables - do you need to
select from all of those tables, and are all restrictions specified to
minimize the number of rows returned?
* You are not specifying from which table all columns will be
retrieved - you may want to fix that.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.
I'll have to look more at it. I think it is where the info is coming
from which why there are so many tables......

I was hoping to make it smaller too, it is just so long.....


Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.