The sql query below is having some error during inner join part and this is the error message:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'AS a INNER JOIN (SELECT SERIAL_NO FROM STOCK.WAREHOUSE_MVT_LINE
mvtLn INNER ' at line 3
SELECT
wm.CID,
wm.DOC_ID,
wm.DOC_TYPE,
wml.SERIAL_NO,
wml.WH_MVT_NO,
wm.PROD_ID,
wm.LOT_CODE,
wm.WH_CODE,
wm.BIN_CODE,
wml.MVT_DATE,
wm.BATCH_NO,
wm.MVT_TYPE,
wm.PROD_NAME,
wm.COMMENT,
wm.REMARK,
wm.NOTE,
wm.INSTRUCTION
FROM
STOCK.WAREHOUSE_MVT_LINE wml
INNER JOIN
STOCK.WAREHOUSE_MVT wm
ON wml.CID=wm.CID
AND wml.WH_MVT_NO=wm.WH_MVT_NO
WHERE
wml.CID = 70200009
AND (
wm.MVT_TYPE != 'TFOUT'
AND wm.MVT_TYPE != '3TFOUT'
)
AND wml.SERIAL_NO IN (
(
SELECT
SERIAL_NO
FROM
STOCK.WAREHOUSE_MVT_LINE mvtLn
INNER JOIN
STOCK.WAREHOUSE_MVT mvt
ON mvtLn.CID = mvt.CID
AND mvtLn.PFC_CODE = mvt.PFC_CODE
AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO
where
mvtLn.CID = 70200009
AND mvtLn.PFC_CODE ='SG'
AND mvtLn.MVT_DATE >= '2016-04-01 00:00:00.0'
AND mvtLn.MVT_DATE <='2016-06-30 00:00:00.0'
AND mvt.WH_CODE = 'IDJKT'
AND mvt.BIN_CODE = 'IMP'
)AS a
INNER JOIN
(
SELECT
SERIAL_NO
FROM
STOCK.WAREHOUSE_MVT_LINE mvtLn
INNER JOIN
STOCK.WAREHOUSE_MVT mvt
ON mvtLn.CID = mvt.CID
AND mvtLn.PFC_CODE = mvt.PFC_CODE
AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO
where
mvtLn.CID = 70200009
AND mvtLn.PFC_CODE ='SG'
AND mvtLn.MVT_DATE >= '2016-01-01 00:00:00.0'
AND mvtLn.MVT_DATE <='2016-03-31 00:00:00.0'
AND mvt.WH_CODE = 'CL'
AND mvt.BIN_CODE = 'PSADEPOT'
)AS b
ON a.SERIAL_NO = b.SERIAL_NO
)
AND wm.MVT_DATE BETWEEN '2016-01-01 00:00:00.0' AND '2016-06-30 00:00:00.0'
ORDER BY
wml.SERIAL_NO,
wml.MVT_DATE
Do you want this?
SELECT wm.CID, wm.DOC_ID, wm.DOC_TYPE, wml.SERIAL_NO, wml.WH_MVT_NO
, wm.PROD_ID, wm.LOT_CODE, wm.WH_CODE, wm.BIN_CODE, wml.MVT_DATE
, wm.BATCH_NO, wm.MVT_TYPE, wm.PROD_NAME, wm.COMMENT, wm.REMARK
, wm.NOTE, wm.INSTRUCTION
FROM STOCK.WAREHOUSE_MVT_LINE wml INNER JOIN STOCK.WAREHOUSE_MVT wm ON wml.CID = wm.CID
AND wml.WH_MVT_NO = wm.WH_MVT_NO
WHERE wml.CID = 70200009
AND wm.MVT_TYPE != 'TFOUT'
AND wm.MVT_TYPE != '3TFOUT'
AND wml.SERIAL_NO IN (
SELECT a.SERIAL_NO FROM -- You missed this. I thought....
(
SELECT SERIAL_NO
FROM STOCK.WAREHOUSE_MVT_LINE mvtLn
INNER JOIN STOCK.WAREHOUSE_MVT mvt
ON mvtLn.CID = mvt.CID
AND mvtLn.PFC_CODE = mvt.PFC_CODE
AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO
WHERE mvtLn.CID = 70200009
AND mvtLn.PFC_CODE = 'SG'
AND mvtLn.MVT_DATE >= '2016-04-01 00:00:00.0'
AND mvtLn.MVT_DATE <= '2016-06-30 00:00:00.0'
AND mvt.WH_CODE = 'IDJKT'
AND mvt.BIN_CODE = 'IMP'
) a
INNER JOIN (
SELECT SERIAL_NO
FROM STOCK.WAREHOUSE_MVT_LINE mvtLn
INNER JOIN STOCK.WAREHOUSE_MVT mvt
ON mvtLn.CID = mvt.CID
AND mvtLn.PFC_CODE = mvt.PFC_CODE
AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO
WHERE mvtLn.CID = 70200009
AND mvtLn.PFC_CODE = 'SG'
AND mvtLn.MVT_DATE >= '2016-01-01 00:00:00.0'
AND mvtLn.MVT_DATE <= '2016-03-31 00:00:00.0'
AND mvt.WH_CODE = 'CL'
AND mvt.BIN_CODE = 'PSADEPOT'
) b ON a.SERIAL_NO = b.SERIAL_NO )
AND wm.MVT_DATE BETWEEN '2016-01-01 00:00:00.0' AND '2016-06-30 00:00:00.0'
ORDER BY wml.SERIAL_NO, wml.MVT_DATE
Related
I'm trying to join a different table depended on the value of a "task_type" column
What is wrong with the syntax?
Thanks.
SELECT t.*,s.task_name
CASE
WHEN t.task_type = 0 THEN
LEFT JOIN scheduler_tasks s ON s.scheduler_task_id = t.task_id
WHEN t.task_type = 1 THEN
LEFT JOIN invoice_tasks s ON s.uid = t.task_id
END
FROM task_timings t
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND t.start_date <= DATE(?)
I believe you want this:
SELECT t.*,
COALESCE(s.task_name, i.task_name) as task_name
FROM task_timings t LEFT JOIN
scheduler_tasks s
ON s.scheduler_task_id = t.task_id AND
t.task_type = 0 LEFT JOIN
invoice_tasks i
ON i.uid = t.task_id AND t.task_type = 1
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND
t.start_date <= DATE(?);
Can you try like this :
SELECT t.*,s.task_name FROM task_timings t
CASE
WHEN t.task_type = 0 THEN
LEFT JOIN scheduler_tasks s ON s.scheduler_task_id = t.task_id
WHEN t.task_type = 1 THEN
LEFT JOIN invoice_tasks s ON s.uid = t.task_id
END
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND t.start_date <= DATE(?)
How can we use result set of one subquery multiple times in the same query
SELECT
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) be ON joa.referred_by = be.id
) AS applicationcount,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bf ON joa.referred_by = bf.id
AND joa.admin_review = '3'
AND joa.rejection_reason = 'Admin rejected your game'
) AS admin_reject,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bg ON joa.referred_by = bg.id
AND joa. STATUS = '5'
AND joa.admin_review = '2'
) AS employer_reject,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bd ON joa.referred_by = bd.id
AND joa.admin_review = '1'
) AS admin_review,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bc ON joa.referred_by = bc.id
AND joa.admin_review = '5'
) AS accountmanager_review,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) ba ON joa.referred_by = ba.id
AND joa.admin_review = '6'
) AS rp_review,
(
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = 1
) bh ON joa.referred_by = bh.id
AND joa.admin_review = '2'
AND (
joa. STATUS = '' || joa. STATUS = 1 || joa. STATUS = 2 || joa. STATUS = 3 || joa. STATUS = 4
)
) AS other_status
FROM
game_applied ja
JOIN user_user u ON u.id = ja.applied_recruiter_id
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bn ON ja.referred_by = bn.id
GROUP BY
applicationcount
How can we use result set of one subquery multiple times in the same query
the sub query used mulitple times in this query to a single use
(
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bn ON ja.referred_by = bn.id
Try this:
SELECT COUNT(joa.id) AS applicationcount,
SUM(joa.admin_review = '3' AND joa.rejection_reason = 'Admin Rejected your resume') AS admin_reject,
SUM(joa.STATUS = '5' AND joa.admin_review = '2') AS employer_reject,
SUM(joa.admin_review = '1') AS admin_review,
SUM(joa.admin_review = '5') AS accountmanager_review,
SUM(joa.admin_review = '6') AS rp_review,
SUM(joa.admin_review = '2' AND joa.STATUS != '5') AS other_status,
FROM game_refer_to_member jrmm
INNER JOIN game_refer jrr ON jrr.id = jrmm.rid AND jrr.referby_user_id = 2551
INNER JOIN game_applied joa ON jrmm.id = joa.referred_by
INNER JOIN user_user u ON u.id = joa.applied_recruiter_id
WHERE jrmm.STATUS = '1'
OK... I'll try to figure out what the query does first. I'll replace all instances of:
SELECT
COUNT(joa.id)
FROM
game_applied joa
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
with the phrase:
( SELECT count(*) FROM joa JOIN (subselect))
...for clarity.
I also removed the GROUP BY, since it is useless and/or misguided, unless you can explain why it is there.
I assume ja.applied_recruiter_id is a foreign key, which means...
JOIN user_user u ON u.id = ja.applied_recruiter_id
...always returns one row. Since no columns from user_user are actually selected, this join can be removed. Now, this part:
INNER JOIN (
SELECT
jrmm.id
FROM
game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1'
) bn ON ja.referred_by = bn.id
...it is unclear what this does. Since the subselect is the same as the one in the previous queries, it is unlikely that it would filter lines returned by the whole query. I'd say its only effect is to uselessly duplicate lines, which explains why there was a GROUP BY... So, off it goes.
We get:
SELECT
( SELECT count(*) FROM joa JOIN (subselect)) be ON joa.referred_by = be.id ) AS applicationcount,
( SELECT count(*) FROM joa JOIN (subselect)) bf ON joa.referred_by = bf.id
AND joa.admin_review = '3'
AND joa.rejection_reason = 'Admin rejected your game'
) AS admin_reject,
( SELECT count(*) FROM joa JOIN (subselect)) bg ON joa.referred_by = bg.id
AND joa. STATUS = '5'
AND joa.admin_review = '2'
) AS employer_reject,
( SELECT count(*) FROM joa JOIN (subselect)) bd ON joa.referred_by = bd.id
AND joa.admin_review = '1'
) AS admin_review,
( SELECT count(*) FROM joa JOIN (subselect)) bc ON joa.referred_by = bc.id
AND joa.admin_review = '5'
) AS accountmanager_review,
( SELECT count(*) FROM joa JOIN (subselect)) ba ON joa.referred_by = ba.id
AND joa.admin_review = '6'
) AS rp_review,
( SELECT count(*) FROM joa JOIN (subselect)) bh ON joa.referred_by = bh.id
AND joa.admin_review = '2'
AND (joa. STATUS = '' || joa. STATUS = 1 || joa. STATUS = 2 || joa. STATUS = 3 || joa. STATUS = 4)
) AS other_status
FROM
game_applied ja
...And, using the same logic as Sarhash, we simplify this into:
SELECT COUNT(joa.id) AS applicationcount,
SUM(joa.admin_review = '3' AND joa.rejection_reason = 'Admin Rejected your resume') AS admin_reject,
SUM(joa.STATUS = '5' AND joa.admin_review = '2') AS employer_reject,
SUM(joa.admin_review = '1') AS admin_review,
SUM(joa.admin_review = '5') AS accountmanager_review,
SUM(joa.admin_review = '6') AS rp_review,
SUM(joa.admin_review = '2' AND joa.STATUS != '5') AS other_status,
FROM game_refer_to_member jrmm
INNER JOIN game_refer jrr ON jrr.id = jrmm.rid
INNER JOIN game_applied joa ON jrmm.id = joa.referred_by
WHERE jrmm.STATUS = '1' AND jrr.referby_user_id = 2551
(which is the same, minus the useless join to USER and a cleanup in the WHERE, thank you Sarhash, you get all the credit).
I found a working solution using CREATE TEMPORARY TABLE. I'd suggest running two queries back to back in the same session. First, create some temporary tables from the subqueries you want to reference multiple times:
CREATE
TEMPORARY TABLE temp_table1
(SELECT COUNT(joa.id)
FROM game_applied);
CREATE
TEMPORARY TABLE new_table
(SELECT jrmm.id
FROM game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = '1');
After running a simple find-and-replace on your original query using these temporary table names, the new query would look like this:
SELECT
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 be ON joa.referred_by = be.id) AS applicationcount,
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 bf ON joa.referred_by = bf.id
AND joa.admin_review = '3'
AND joa.rejection_reason = 'Admin rejected your game') AS admin_reject,
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 bg ON joa.referred_by = bg.id
AND joa. STATUS = '5'
AND joa.admin_review = '2') AS employer_reject,
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 bd ON joa.referred_by = bd.id
AND joa.admin_review = '1') AS admin_review,
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 bc ON joa.referred_by = bc.id
AND joa.admin_review = '5') AS accountmanager_review,
(SELECT *
FROM temp_table1 joa
INNER JOIN temp_table2 ba ON joa.referred_by = ba.id
AND joa.admin_review = '6') AS rp_review,
(SELECT *
FROM temp_table1 joa
INNER JOIN
(SELECT jrmm.id
FROM game_refer_to_member jrmm
JOIN game_refer jrr ON jrr.id = jrmm.rid
AND jrr.referby_user_id = 2551
AND jrmm. STATUS = 1) bh ON joa.referred_by = bh.id
AND joa.admin_review = '2'
AND (joa. STATUS = '' || joa. STATUS = 1 || joa. STATUS = 2 || joa. STATUS = 3 || joa. STATUS = 4)) AS other_status
FROM game_applied ja
JOIN user_user u ON u.id = ja.applied_recruiter_id
INNER JOIN temp_table2 bn ON ja.referred_by = bn.id
GROUP BY applicationcount
I'm not guaranteeing that this query is without bugs, since I don't have your database and therefore cannot easily test it, but this at least outlines a decent strategy that I think should improve performance, and at very least makes the query about half the size.
Our website sometimes causes extreme server strain due to a complex MySQL query. The site actually goes down.
The webhoster warned that if we don't get this in order they will suspend our account.
Could someone give some pointers at which parts of this query eat the most resources?
Any suggestions on making this better?
SELECT SQL_CALC_FOUND_ROWS wp_s3mv0r_posts.ID
FROM wp_s3mv0r_posts
INNER JOIN wp_s3mv0r_term_relationships ON (wp_s3mv0r_posts.ID = wp_s3mv0r_term_relationships.object_id)
INNER JOIN wp_s3mv0r_postmeta ON ( wp_s3mv0r_posts.ID = wp_s3mv0r_postmeta.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt1 ON ( wp_s3mv0r_posts.ID = mt1.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt2 ON ( wp_s3mv0r_posts.ID = mt2.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt3 ON ( wp_s3mv0r_posts.ID = mt3.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt4 ON ( wp_s3mv0r_posts.ID = mt4.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt5 ON ( wp_s3mv0r_posts.ID = mt5.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt6 ON ( wp_s3mv0r_posts.ID = mt6.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt7 ON ( wp_s3mv0r_posts.ID = mt7.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt8 ON ( wp_s3mv0r_posts.ID = mt8.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt9 ON ( wp_s3mv0r_posts.ID = mt9.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt10 ON ( wp_s3mv0r_posts.ID = mt10.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt11 ON ( wp_s3mv0r_posts.ID = mt11.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt12 ON ( wp_s3mv0r_posts.ID = mt12.post_id )
WHERE 1=1 AND (
wp_s3mv0r_term_relationships.term_taxonomy_id IN (11,24,25)
) AND (
( wp_s3mv0r_postmeta.meta_key = 'acf_house_minprice' AND CAST(wp_s3mv0r_postmeta.meta_value AS SIGNED) BETWEEN '274990' AND '599990' )
AND
( mt1.meta_key = 'acf_house_minlotwidth' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '16' AND '16' )
AND
( mt2.meta_key = 'acf_location_area' AND CAST(mt2.meta_value AS CHAR) IN ('South of river') )
AND
( mt3.meta_key = 'acf_house_bedroom' AND CAST(mt3.meta_value AS CHAR) = '4' )
AND
( mt4.meta_key = 'acf_house_studyroom' AND CAST(mt4.meta_value AS SIGNED) > '0' )
AND
( mt5.meta_key = 'acf_house_theaterroom' AND CAST(mt5.meta_value AS SIGNED) > '0' )
AND
( mt6.meta_key = 'acf_house_alfresco' AND CAST(mt6.meta_value AS SIGNED) > '0' )
AND
( mt7.meta_key = 'acf_house_activityroom' AND CAST(mt7.meta_value AS SIGNED) > '0' )
AND
( mt8.meta_key = 'acf_house_doublegarage' AND CAST(mt8.meta_value AS SIGNED) > '0' )
AND
( mt9.meta_key = 'acf_house_reargarage' AND CAST(mt9.meta_value AS SIGNED) > '0' )
AND
( mt10.meta_key = 'acf_house_islbeninkitchen' AND CAST(mt10.meta_value AS SIGNED) > '0' )
AND
( mt11.meta_key = 'acf_house_frontmasterbedroom' AND CAST(mt11.meta_value AS SIGNED) > '0' )
AND
( mt12.meta_key = 'acf_house_rearmaster' AND CAST(mt12.meta_value AS SIGNED) > '0' )
) AND wp_s3mv0r_posts.post_type = 'house' AND (wp_s3mv0r_posts.post_status = 'publish')
GROUP BY wp_s3mv0r_posts.ID
ORDER BY wp_s3mv0r_posts.post_date DESC
LIMIT 0, 10
How about this:
SELECT p.ID, count(*) AS 'PostCount'
FROM wp_s3mv0r_posts p INNER JOIN wp_s3mv0r_term_relationships r ON (p.ID = r.object_id)
INNER JOIN wp_s3mv0r_postmeta pm ON (p.ID = pm.post_id)
WHERE
r.term_taxonomy_id IN (11,24,25) AND p.post_type = 'house' AND p.post_status = 'publish' AND (
(pm.meta_key = 'acf_house_minprice' AND pm.meta_value BETWEEN '274990' AND '599990') OR
(pm.meta_key = 'acf_house_minlotwidth' AND pm.meta_value BETWEEN '16' AND '16') OR
(pm.meta_key = 'acf_location_area' AND pm.meta_value = 'South of river') OR
(pm.meta_key = 'acf_house_bedroom' AND pm.meta_value = '4') OR
(pm.meta_key IN ('acf_house_studyroom', 'acf_house_theaterroom', 'acf_house_alfresco', 'acf_house_activityroom',
'acf_house_doublegarage', 'acf_house_reargarage', 'acf_house_islbeninkitchen', 'acf_house_frontmasterbedroom',
'acf_house_rearmaster') AND pm.meta_value > '0'))
GROUP BY p.ID
ORDER BY p.post_date DESC
LIMIT 0, 10
this is my query:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id
OR game_link.game_id2 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC
i do think the culprit might be:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
can anybody simplify this query to a much more efficient one?
here's an image for sql process:
https://lh5.googleusercontent.com/--BsUDk8jLnY/UwbKFiCjZCI/AAAAAAAAAi4/MXCQFhs4PeU/w496-h216-no/sqlquery.png
here are some of my table indexes:
game
PRIMARY - game_id
game_link
PRIMARY - game_link_id
game_id1 - game_id1, game_id2
game_bet_settings
PRIMARY - game_bet_settings_id
game_id - game_id
You can try adding this index:
game(game_winner, is_active, open_bet_time, close_bet_time, game_id);
However, the culprit is probably:
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id OR game_link.game_id2 = game.game_id )
Queries with or are hard to optimize.
Try running your query without this condition:
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC
If this works in reasonable time, then try union'ing the two queries together (one with each condition).
EDIT:
The union would look like:
(SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
)
UNION
(SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM game
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id2 = game.game_id )
AND game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND kiyosuku_to_game.kiyosuku_id = '1'
AND game.game_is_active = '1'
AND game_bet_settings.is_disabled !=1
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
)
ORDER BY game_start_time ASC
I would suggest to regroup a bit the query placing conditions to a subquery to restrict amount of records.
SELECT DISTINCT (game.game_id)
AS gid, game_link.is_linked, game_link.game_id1, game_bet_settings.bet_type_id
AS bet_type
FROM (select * from game
where
game.game_id != ''
AND game.game_winner = '0'
AND game.game_open_bet_time <= '2014-02-21 12:12:48'
AND game.game_close_bet_time >= '2014-02-21 12:12:48'
AND game.game_is_active = '1'
)
LEFT JOIN kiosk_to_game ON ( kiosk_to_game.game_id = game.game_id )
LEFT JOIN game_bet_settings ON ( game_bet_settings.game_id = game.game_id )
LEFT JOIN game_link ON ( game_link.game_id1 = game.game_id
OR game_link.game_id2 = game.game_id )
AND kiyosuku_to_game.kiyosuku_id = '1'
AND (game_link.is_linked IS NULL OR game_link.is_linked =1)
ORDER BY game.game_start_time ASC
SELECT datecreated,
totcount
FROM (SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' ') AS datecreated,
COUNT(DISTINCT tof.unique_id) AS totcount
FROM trn_open_files tof
JOIN trn_debtor tb
ON ( tof.unique_id = tb.unique_id
AND tb.debtor_seq_num = 1
AND tb.rec_status = 'R' )
JOIN trn_property tp
ON ( tof.unique_id = tp.unique_id )
LEFT OUTER JOIN trn_parties prty_cccs
ON ( prty_cccs.unique_id = tof.unique_id
AND prty_cccs.party_role = 'CCCS'
AND prty_cccs.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_srv
ON ( prty_srv.unique_id = tof.unique_id
AND prty_srv.party_role = 'SRV'
AND prty_srv.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_vend
ON ( prty_vend.unique_id = tof.unique_id
AND prty_vend.party_role = 'CLMO'
AND prty_vend.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ins
ON ( prty_ins.unique_id = tof.unique_id
AND prty_ins.party_role = 'INS'
AND prty_ins.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ss
ON ( prty_ss.unique_id = tof.unique_id
AND prty_ss.party_role = 'SS'
AND prty_ss.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_realtor
ON ( prty_realtor.unique_id = tof.unique_id
AND prty_realtor.party_role = 'REALTOR'
AND prty_realtor.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci1
ON ( mci1.comp_id = prty_cccs.party_comp_id
AND mci1.comp_type = 'CCCS'
AND mci1.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci
ON ( mci.comp_id = prty_srv.party_comp_id
AND mci.comp_type = 'SRV'
AND mci.rec_status = 'A'
AND mci.pilot_flag = 'Y' )
LEFT OUTER JOIN mst_comp_info mci2
ON ( mci2.comp_id = prty_vend.party_comp_id
AND mci2.comp_type = 'VEND'
AND mci2.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci3
ON ( mci3.comp_id = prty_ins.party_comp_id
AND mci3.comp_type = 'INS'
AND mci3.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci4
ON ( mci4.comp_id = prty_ss.party_comp_id
AND mci4.comp_type = 'SS'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci5
ON ( mci5.comp_id = prty_realtor.party_comp_id
AND mci4.comp_type = 'REALTOR'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_status_codes i
ON ( i.status_code = tof.case_status
AND i.comp_id = tof.comp_id
AND ( i.party_role IS NULL
OR i.party_role = 'VEND' )
AND tof.file_type = i.file_type )
WHERE tof.cur_status = 'A'
AND tof.cms_flag = 'Y'
AND CONVERT(DATE, tof.create_stamp) >= CONVERT(DATE, '2010-06-06')
AND CONVERT(DATE, tof.create_stamp) <= CONVERT(DATE, '2011-10-01')
AND prty_cccs.party_comp_id = 10153
AND prty_cccs.party_comp_id = 10153
GROUP BY REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' '),
prty_cccs.party_comp_id) q1
having this procedure how to show month in proper sequence.
now month is appear as
out put: DateCreated totcount
Sep 11 7
May 11 2
Jun 11 10
Jul 11 40
Aug 11 144
i want month in sequence like DateCreated totcount
May 11 2
Jun 11 10
Jul 11 40
Aug 11 144
Sep 11 7
Not tested but I think you should replace REPLACE(RIGHT(CONVERT(VARCHAR(9), tof.create_stamp, 6), 6), ' ', ' ') with dateadd(month, datediff(month, 0, tof.create_stamp), 0) and do the presentation thing in the outer query. And adding an order by to the outer query ORDER BY q1.datecreated.
SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), datecreated, 6), 6), ' ', ' '),
totcount
FROM (SELECT Dateadd(MONTH, Datediff(MONTH, 0, tof.create_stamp), 0) AS datecreated,
COUNT(DISTINCT tof.unique_id) AS totcount
FROM trn_open_files tof
JOIN trn_debtor tb
ON ( tof.unique_id = tb.unique_id
AND tb.debtor_seq_num = 1
AND tb.rec_status = 'R' )
JOIN trn_property tp
ON ( tof.unique_id = tp.unique_id )
LEFT OUTER JOIN trn_parties prty_cccs
ON ( prty_cccs.unique_id = tof.unique_id
AND prty_cccs.party_role = 'CCCS'
AND prty_cccs.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_srv
ON ( prty_srv.unique_id = tof.unique_id
AND prty_srv.party_role = 'SRV'
AND prty_srv.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_vend
ON ( prty_vend.unique_id = tof.unique_id
AND prty_vend.party_role = 'CLMO'
AND prty_vend.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ins
ON ( prty_ins.unique_id = tof.unique_id
AND prty_ins.party_role = 'INS'
AND prty_ins.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_ss
ON ( prty_ss.unique_id = tof.unique_id
AND prty_ss.party_role = 'SS'
AND prty_ss.rec_status = 'A' )
LEFT OUTER JOIN trn_parties prty_realtor
ON ( prty_realtor.unique_id = tof.unique_id
AND prty_realtor.party_role = 'REALTOR'
AND prty_realtor.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci1
ON ( mci1.comp_id = prty_cccs.party_comp_id
AND mci1.comp_type = 'CCCS'
AND mci1.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci
ON ( mci.comp_id = prty_srv.party_comp_id
AND mci.comp_type = 'SRV'
AND mci.rec_status = 'A'
AND mci.pilot_flag = 'Y' )
LEFT OUTER JOIN mst_comp_info mci2
ON ( mci2.comp_id = prty_vend.party_comp_id
AND mci2.comp_type = 'VEND'
AND mci2.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci3
ON ( mci3.comp_id = prty_ins.party_comp_id
AND mci3.comp_type = 'INS'
AND mci3.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci4
ON ( mci4.comp_id = prty_ss.party_comp_id
AND mci4.comp_type = 'SS'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_comp_info mci5
ON ( mci5.comp_id = prty_realtor.party_comp_id
AND mci4.comp_type = 'REALTOR'
AND mci4.rec_status = 'A' )
LEFT OUTER JOIN mst_status_codes i
ON ( i.status_code = tof.case_status
AND i.comp_id = tof.comp_id
AND ( i.party_role IS NULL
OR i.party_role = 'VEND' )
AND tof.file_type = i.file_type )
WHERE tof.cur_status = 'A'
AND tof.cms_flag = 'Y'
AND CONVERT(DATE, tof.create_stamp) >= CONVERT(DATE, '2010-06-06')
AND CONVERT(DATE, tof.create_stamp) <= CONVERT(DATE, '2011-10-01')
AND prty_cccs.party_comp_id = 10153
AND prty_cccs.party_comp_id = 10153
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0, tof.create_stamp), 0),
prty_cccs.party_comp_id) q1
ORDER BY q1.datecreated