Mysql query optimization takes more than 30 sec - mysql

Hi friends can any one please help me to optimize this query ,it takes more than 30 sec.
any suggestion is warmly welcome.
Query :
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
(CASE
WHEN (UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL) THEN 'Opted In'
ELSE (CASE
WHEN (UNSUBSCRIBE = 1) THEN 'Opted Out'
ELSE (CASE
WHEN
((UNSUBSCRIBE = 2 OR UNSUBSCRIBE = 3)
AND CCD.CONTACT_ID NOT IN (SELECT
CONTACT_ID
FROM
CM_SUBSCRIPTION_MAIL_DATA
WHERE
IS_MAIL_SENT = 'Y'))
THEN
'Opt-In Request not Sent'
ELSE 'Opt-In Pending'
END)
END)
END) AS CURR_SUB,
(CASE
WHEN (SUBSCRIBE_FROM = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_FROM,
SUBSCRIBE_FROM,
(CASE
WHEN (SUBSCRIBE_TO = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN
ADDRESS A ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN
CM_SUBSCRIPTION_LOGS CSL ON CSL.CONTACT_ID = CCD.CONTACT_ID
WHERE
1 = 1
GROUP BY CSL.LOG_DATE , CCD.CONTACT_ID
ORDER BY NAME DESC , LOG_DATE DESC
LIMIT 0 , 20

Your case/when can be simplified, but I believe the killer of your query was the correlated NOT IN SELECT clause for your Opt-in request not sent.
I've change your query to do a left-join to your mailing table based on the contact AND the mailed status of "Y". So this simplifies your case to only have to check for IS NULL of the mailing table contact ID.
You can't do too much on performance since your Group by is by columns from different tables that won't take advantage of any index optimization, then all that ordered by the name makes it more of an issue... but the correlated subquery per the field being executed every time to a left-join SHOULD help.
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
CASE WHEN UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL THEN 'Opted In'
WHEN UNSUBSCRIBE = 1 THEN 'Opted Out'
WHEN UNSUBSCRIBE IN (2,3) AND SMD.CONTACT_ID IS NULL THEN 'Opt-In Request not Sent'
ELSE 'Opt-In Pending' END AS CURR_SUB,
CASE WHEN SUBSCRIBE_FROM = 0 THEN 'Opted In'
WHEN SUBSCRIBE_FROM = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_FROM,
SUBSCRIBE_FROM,
CASE WHEN SUBSCRIBE_TO = 0 THEN 'Opted In'
WHEN SUBSCRIBE_TO = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN ADDRESS A
ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN CM_SUBSCRIPTION_LOGS CSL
ON CSL.CONTACT_ID = CCD.CONTACT_ID
LEFT JOIN CM_SUBSCRIPTION_MAIL_DATA SMD
ON CCD.CONTACT_ID = SMD.CONTACT_ID
AND IS_MAIL_SENT = 'Y'
WHERE
1 = 1
GROUP BY
CSL.LOG_DATE,
CCD.CONTACT_ID
ORDER BY
NAME DESC,
LOG_DATE DESC
LIMIT
0, 20

Related

How to fetch data in one row using case condition

I have two tables and i wanted data should come in specific format.
My query
SELECT
DISTINCT ur.suid,
(CASE WHEN ur.sceneID = '1' THEN ur.data ELSE 0 END) AS Scenario1data,
(CASE WHEN ur.sceneID = '1' THEN ss.score ELSE 0 END) AS Scenario1score,
(CASE WHEN ur.sceneID = '2' THEN ur.data ELSE 0 END) AS Scenario2,
(CASE WHEN ur.sceneID = '3' THEN ur.data ELSE 0 END) AS Scenario3,
(CASE WHEN ur.sceneID = '4' THEN ur.data ELSE 0 END) AS Scenario4
FROM tbluserresults AS ur LEFT JOIN tbluserscenescores AS ss ON ss.suid = ur.suid;
Result i wanted
suid Scenario1data Scenario1score Scenario2 Scenario3 Scenario4
1 'abc show1' 334 abc show2 abc show3 abc show 4
2 0 0 0 0.
Check my SQLFIDDLE
There are probably other better ways to do this, but I believe the following does what you want
SELECT results.suid as suid,
(CASE WHEN results.Scenario1data IS NOT NULL THEN results.Scenario1data ELSE 0 END) AS Scenario1data,
(CASE WHEN results.Scenario1score IS NOT NULL THEN results.Scenario1score ELSE 0 END) AS Scenario1score,
(CASE WHEN results.Scenario2 IS NOT NULL THEN results.Scenario2 ELSE 0 END) AS Scenario2,
(CASE WHEN results.Scenario3 IS NOT NULL THEN results.Scenario3 ELSE 0 END) AS Scenario3,
(CASE WHEN results.Scenario4 IS NOT NULL THEN results.Scenario4 ELSE 0 END) AS Scenario4
FROM
(SELECT DISTINCT(suid), #suid:= suid,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '1') AS Scenario1data,
(SELECT ss.score FROM tbluserscenescores ss WHERE ss.suid = #suid AND ss.sceneID = '1') AS Scenario1score,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '2') AS Scenario2,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '3') AS Scenario3,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '4') AS Scenario4
FROM tbluserscenescores) AS results

SUM for more fields in mysql

I have a query,which give me wrong result.Here is my query.
SELECT sum(open1) open1, sum(closed1) closed,sum(pending1) NotSpecified,
status,type,sub_status,created_date,bystatus,lst_type
FROM (
SELECT agent_1_id,status,type,sub_status,created_date,'Open' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
count(*) as open1, 0 closed1, 0 pending1
FROM crm_mydeals
where status = 1 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'Closed' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, count(*) as closed1, 0 pending1
FROM crm_mydeals
where status = 2 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'NotSpecified' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, 0 closed1, count(*) as pending1
FROM crm_mydeals
where status = 3 AND agent_1_id>0 and is_active=1
GROUP BY status
) s
WHERE DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status
If you simplify the query, it might help to find the correct answer?
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0) AS open1,
SUM(CASE WHEN status = 2 THEN 1 ELSE 0) AS closed,
SUM(CASE WHEN Status = 3 THEN 1 ELSE 0) AS NotSpecified,
status,
type,
sub_status,
created_date,
(CASE WHEN status = 1 THEN 'Open'
WHEN status = 2 THEN 'Closed'
WHEN status = 3 THEN 'NotSpecified') AS bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type
FROM crm_mydeals
WHERE status IN (1,2,3)
AND agent_1_id > 0
AND is_active=1
AND DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status, type, sub_status, created_date

Case When with AND?

I want to use Case When with AND condition and it is not calculating the sum properly.
For example:
SELECT DATE(`SubmitDate`),
SUM(CASE status WHEN 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
SUM(CASE status WHEN 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
You need to use CASE WHEN [Condition] THEN... rather than a simple case expression:
SELECT DATE(`SubmitDate`),
SUM(CASE WHEN status = 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
SUM(CASE WHEN status = 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
You should write
CASE WHEN status='New' AND `Type` = 'consumer' THEN 1 ELSE 0 END
Check the syntax of CASE WHEN

SQL query - How to exclude WHERE for specific field

See the SQL query below, it work fine. It calculate the number of Yes, NOT, Other and the number of matching mobile number [Sales field] (D.MobileNo = S.mobile)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I want to exclude WHERE for the Sales field - it should not be part from CheckDate. Meaning it should check any record in the dairy table without CheckDate for the Sales field.
How can that be done?
If you really want those results in only one query, this might do the trick.
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL,
SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
GROUP BY D.Username
ORDER BY TOTAL DESC
Note that "TOTAL" will count all rows (including those who where not within your CheckDate range), TOTALINCHECKDATE return the same value as in your previous query.
Obviously, this can still be optimized.
Assuming username exists also in SALES table
SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
, SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile,
CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes,
CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`,
CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other,
0 As sales, 1 as total
FROM dairy as D
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
) AS a
GROUP BY Username
Also try your original query with date addition
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC

Counting number of Sales from two tables

I am using SUM ( CASE WHEN ) to count number of Yes and No, it work fine.
I am havin problem counting number of matching mobile number from two tables. It dont seem to be counting correctly.
There is a MobileNO field in dairy table and mobile field in the sales table
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
(SELECT SUM(CASE WHEN D.MobileNo = S.mobile THEN 1 ELSE 0 END) from sales as S) as Sales,
COUNT(*) as TOTAL FROM dairy as D
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S ON D.MobileNo = S.mobile
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC