Slow query performance in MySQL - mysql

I would like to find out what causes the slow execution of my MySQL query here. It's 1 row fetched in 0.0017s (3.6215s) How can i optimize this?
SELECT hadmlog.hpercode as 'HOSPITAL NUMBER', FLOOR (hadmlog.patage) as 'AGE', CONCAT (hperson.patlast, ', ', hperson.patfirst, ' ',hperson.patmiddle) as 'PROFILE', hcity.ctyname as 'DISTRICT', hadmlog.disdate as 'DISCHARGED DATE'
FROM hadmlog
INNER JOIN hperson ON hadmlog.hpercode=hperson.hpercode
INNER JOIN haddr ON hadmlog.hpercode=haddr.hpercode
INNER JOIN hcity ON haddr.ctycode=hcity.ctycode
WHERE hadmlog.patage BETWEEN '1' AND '4'
AND hperson.patsex = 'M'
AND DATE(hadmlog.disdate) = DATE(curdate())
AND haddr.haddrdte = ( select max(haddrdte)
from haddr
where haddr.hpercode = hperson.hpercode )
ORDER BY Profile;

First, fix the query:
SELECT al.hpercode as HOSPITAL_NUMBER, FLOOR(al.patage) as AGE,
CONCAT_WS(' ', p.patlast, p.patfirst, p.patmiddle) as PROFILE,
c.ctyname as DISTRICT, al.disdate as DISCHARGED_DATE
FROM hadmlog al JOIN
hperson p
ON al.hpercode = p.hpercode JOIN
haddr a
ON al.hpercode = a.hpercode JOIN
hcity c
ON a.ctycode = c.ctycode
WHERE al.patage BETWEEN 1 AND 4 AND
p.patsex = 'M' AND
al.disdate >= curdate() AND
al.disdate < curdate() + INTERVAL 1 day AND
a.haddrdte = (select max(h2.haddrdte)
from haddr h2
where h2.hpercode = p.hpercode
)
ORDER BY Profile;
Some of the changes are cosmetic (such as the table aliases and CONCAT_WS()). The more relevant changes are:
patage appears to be a number, so make the comparisons numbers. Strings can impede the optimizer.
date(disdate) can also impede the optimizer.
Then, for this query, I am guessing that you want an indexes:
hadmlog(disdate, patage)
And then you want indexes on the JOIN keys used for the other tables.

Related

MySQL Find Most Recent/Largest Record Per Group by order by

MySQL Find Most Recent/Largest Record Per Group by order by, and how can i minimize / shorten this query and every time it returns the first row value of the group, whereas i like to select the last row value of a group, and sort the values based on ja.id ? I know this is a bad query can anyone suggest or provide me solution to shorten this query . I have used all the necessary Column Indexes in all the tables. How to shorten the query without the use of union all .both the queries in union all are same expect in the where statement.
SELECT
a.previous_status,
a.rejected_status,
a.rejection_reason_text,
a.rejection_reason,
a.rjaId,
a.refer_applied_status,
a.title,
a.playerId,
a.gameId,
a.gamePostDate,
a.game_referal_amount,
a.country,
a.country_name,
a.state,
a.location,
a.state_abb,
a.game_type,
a.appliedId,
a.appliedStatus,
a.admin_review,
a.is_req_referal_check,
a.memberId,
a.appliedEmail,
a.game_id,
a.referred_id,
a.memStateAbb,
a.memState,
a.memZipcode,
a.memCity,
a.memCountryNme,
a.memCountry,
a.appliedMemberName,
a.first_name,
a.primary_contact,
a.last_name,
a.addressbookImage,
a.userImage,
a.last_login,
a.user_experience_year,
a.user_experience_month,
a.current_designation,
a.current_player,
a.appliedDate,
a.addressbook_id,
a.joiningdate,
a.gameStatus,
a.gameReferalAmountType,
a.gameFreezeStatus,
a.gameFreezeMsg,
a.app_assign_back_to_rp_count,
a.applied_source,
a.max_id,
a.gamesApplied,
a.gamesAppliedId,
SUM(a.totalgameApplied) AS totalgameApplied,
a.application_assign_to_rp_status,
a.rpAppliedSource,
a.applied_on
FROM
(
(
SELECT
ja.previous_status,
ja.rejected_status,
ja.rejection_reason_text,
ja.rejection_reason,
rja.id AS rjaId,
rja. STATUS AS refer_applied_status,
jp.title,
jp.user_user_id AS playerId,
jp.id AS gameId,
jp.posted_on AS gamePostDate,
jp.game_referal_amount,
jp.country,
jp.country_name,
jp.state,
jp.location,
jp.state_abb,
jp.game_type,
ja.id AS appliedId,
IFNULL(ja. STATUS, '') AS appliedStatus,
IFNULL(ja.admin_review, '') AS admin_review,
ja.is_req_referal_check,
usr.id AS memberId,
rja.email AS appliedEmail,
rja.game_id,
rja.referred_id,
mem.state_abb AS memStateAbb,
mem.state AS memState,
mem.zipcode AS memZipcode,
mem.city AS memCity,
mem.country_name AS memCountryNme,
mem.country_code AS memCountry,
usc. NAME AS appliedMemberName,
usc.first_name,
IFNULL(
mem.primary_contact,
usc.phone_number
) AS primary_contact,
usc.last_name,
usc.profileimage_path AS addressbookImage,
usr.profile_image AS userImage,
usr.last_login,
mem.user_experience_year,
mem.user_experience_month,
mem.current_designation,
mem.current_player,
rja.create_date AS appliedDate,
rja.addressbook_id,
IFNULL(ja.joining_date, '') AS joiningdate,
jp. STATUS AS gameStatus,
jp.games_referal_amount_type AS gameReferalAmountType,
jp.game_freeze_status AS gameFreezeStatus,
jp.game_freeze_message AS gameFreezeMsg,
ja.app_assign_back_to_rp_count,
ja.applied_source,
MAX(rja.id) AS max_id,
GROUP_CONCAT(
jp.title
ORDER BY
rja.create_date DESC
) AS gamesApplied,
GROUP_CONCAT(DISTINCT(jp.id)) AS gamesAppliedId,
COUNT(DISTINCT(jp.id)) totalgameApplied,
ja.application_assign_to_rp_status,
1 AS rpAppliedSource,
rja.create_date AS applied_on
FROM
(`refer_gameapplied` AS rja)
JOIN `games_post` AS jp ON `jp`.`id` = `rja`.`game_id`
JOIN `user_socialconnections` AS usc ON `rja`.`addressbook_id` = `usc`.`id`
LEFT JOIN `user_user` AS usr ON `usr`.`email` = `rja`.`email`
LEFT JOIN `user_member` AS mem ON `mem`.`user_id` = `usr`.`id`
LEFT JOIN `game_applied` AS ja ON `ja`.`id` = `rja`.`applied_id`
WHERE
`rja`.`referby_id` = '2389'
GROUP BY
`rja`.`email`
)
UNION ALL
(
SELECT
ja.previous_status,
ja.rejected_status,
ja.rejection_reason_text,
ja.rejection_reason,
jr.id AS rjaId,
jrtm. STATUS AS refer_applied_status,
jp.title,
jp.user_user_id AS playerId,
jp.id AS gameId,
jp.posted_on AS gamePostDate,
jp.game_referal_amount,
jp.country,
jp.country_name,
jp.state,
jp.location,
jp.state_abb,
jp.game_type,
ja.id AS appliedId,
IFNULL(ja. STATUS, '') AS appliedStatus,
IFNULL(ja.admin_review, '') AS admin_review,
ja.is_req_referal_check,
usr.id AS memberId,
jrtm.referto_email AS refappliedEmail,
jr.game_id,
jrtm.id,
mem.state_abb AS memStateAbb,
mem.state AS memState,
mem.zipcode AS memZipcode,
mem.city AS memCity,
mem.country_name AS memCountryNme,
mem.country_code AS memCountry,
usc. NAME AS appliedMemberName,
usc.first_name,
IFNULL(
mem.primary_contact,
usc.phone_number
) AS primary_contact,
usc.last_name,
usc.profileimage_path AS addressbookImage,
usr.profile_image AS userImage,
usr.last_login,
mem.user_experience_year,
mem.user_experience_month,
mem.current_designation,
mem.current_player,
jrtm.refer_on AS appliedDate,
jrtm.referto_addressbookid,
IFNULL(ja.joining_date, '') AS joiningdate,
jp. STATUS AS gameStatus,
jp.games_referal_amount_type AS gameReferalAmountType,
jp.game_freeze_status AS gameFreezeStatus,
jp.game_freeze_message AS gameFreezeMsg,
ja.app_assign_back_to_rp_count,
ja.applied_source,
MAX(jrtm.id) AS max_id,
GROUP_CONCAT(
jp.title
ORDER BY
jr.refer_on DESC
) AS gamesApplied,
GROUP_CONCAT(DISTINCT(jp.id)) AS gamesAppliedId,
COUNT(DISTINCT(jp.id)) totalgameApplied,
ja.application_assign_to_rp_status,
2 AS rpAppliedSource,
jrtm.refer_on AS applied_on
FROM
(`game_refer` AS jr)
JOIN `game_refer_to_member` AS jrtm ON `jrtm`.`rid` = `jr`.`id`
JOIN `games_post` AS jp ON `jp`.`id` = `jr`.`game_id`
JOIN `user_socialconnections` AS usc ON `jrtm`.`referto_addressbookid` = `usc`.`id`
LEFT JOIN `user_user` AS usr ON `usr`.`email` = `jrtm`.`referto_email`
LEFT JOIN `user_member` AS mem ON `mem`.`user_id` = `usr`.`id`
LEFT JOIN `game_applied` AS ja ON `ja`.`referred_by` = `jrtm`.`id`
WHERE
`jrtm`.`status` = '1'
AND `jr`.`referby_user_id` = '2389'
AND `jrtm`.`refer_source` NOT IN ('4')
GROUP BY
`jrtm`.`referto_email`
)
) a
GROUP BY
a.appliedEmail
ORDER BY
a.gamesAppliedId DESC
It sounds like a "groupwise-max" problem. I added a tag that you should research.
At least get rid of the columns that are not relevant to the question.
Try tossing the tables other than jrtm and jr to see if the performance problem persists. (I'm thinking that the LEFT JOINs may be red herrings.)
Try with one part of the UNION, then with the other. This may identify which of the two is more of a burden.
Some indexes to add:
rja: (referby_id, create_date)
jrtm: (status, referto_email)
jrtm: (rid, status, referto_email)
jr: (referby_user_id, refer_on)
DISTINCT is not a function. Don't use parents in DISTINCT(jp.id).

Mysql Query speed takes 50 seconds

I have the below Query which takes around 50 seconds to run, is there a way to make it faster? i have not done any indexing yet and will try to do that in the near future but i am still new in this. the problem is not using indexing.
Select stock.StockID As StockID,
stock.BinLocation As BinLocation,
stock.StockCode As StockCode,
stock.Descr As Descr,
stock.Currency As Currency,
stock.Notes As Notes,
stocklocations.Quantity As Quantity,
stocklocations.SLNID As SLNID,
stock.ConditionA As ConditionA,
stocklocationsname.SLN As SLN,
stock.DateAdded As DateAdded,
(Case When (stock.Currency = 'Euro') Then (1.05 * stock.unitprice)
When (stock.Currency = 'GBP') Then (1.24 * stock.unitprice)
When (stock.Currency = 'USD') Then (1 * stock.unitprice) End) As unitprice,
stock.DepName As DepName,
stockhistory.POID As POID,
stock.POID As POID1,
stock.PartNumber As PartNumber,
vender.VendorName As VendorName,
equiptype.EQName As EQName,
groupname.Name As Name
From ((((((stock
Join stocklocations On stock.StockID = stocklocations.StockID)
Join stocklocationsname On stocklocations.SLNID = stocklocationsname.SLNID)
Left Join stockhistory On stockhistory.SHID = (Select Min(stockhistory.SHID)
From stockhistory Where stockhistory.StockID = stock.StockID))
Left Join po On stockhistory.POID = po.POID)
Left Join vender On po.Vender = vender.VendorID)
Left Join equiptype On stock.EquipType = equiptype.EquipTypeID)
Left Join groupname On stock.GroupNameID = groupname.GroupNameID
Where stocklocations.Quantity > 0 And stock.Status <> 'obsolete'
You should probably have an index on stockhistory.StockID:
ALTER TABLE stockhistory ADD INDEX (StockID);
That should speed up the subqueries on stockhistory. Alternatively, since you're retrieving the minimum value of SHID, you might include that column in the index as well:
ALTER TABLE stockhistory ADD INDEX (StockID, SHID);
Adding indexes does not create any restrictions or logical constraints (with the exception of unique indexes). However, as stated in 8.3 Optimization and Indexes from the MySQL documentation:
Although it can be tempting to create an indexes for every possible column used in a query, unnecessary indexes waste space and waste time for MySQL to determine which indexes to use. Indexes also add to the cost of inserts, updates, and deletes because each index must be updated. You must find the right balance to achieve fast queries using the optimal set of indexes.
Figuring out which indexes to create is something of an art. A good start is to investigate query execution with EXPLAIN, as described in 8.8.1 Optimizing Queries with EXPLAIN.
1 ) Remove unuseful () and avoid nested left join
2 ) don't use where fo column involved in left join because this way became inner join
3 ) instead of a subselect try using a join of for the min value related to stockid in stockhistory
Select
stock.StockID As StockID,
stock.BinLocation As BinLocation,
stock.StockCode As StockCode,
stock.Descr As Descr,
stock.Currency As Currency,
stock.Notes As Notes,
stocklocations.Quantity As Quantity,
stocklocations.SLNID As SLNID,
stock.ConditionA As ConditionA,
stocklocationsname.SLN As SLN,
stock.DateAdded As DateAdded,
Case When stock.Currency = 'Euro' Then 1.05 * stock.unitprice
When stock.Currency = 'GBP' Then 1.24 * stock.unitprice
When stock.Currency = 'USD' Then 1 * stock.unitprice End As unitprice,
stock.DepName As DepName,
stockhistory.POID As POID,
stock.POID As POID1,
stock.PartNumber As PartNumber,
vender.VendorName As VendorName,
equiptype.EQName As EQName,
groupname.Name As Name
From stock
Join stocklocations On stock.StockID = stocklocations.StockID
and stocklocations.Quantity > 0
And stock.Status <> 'obsolete'
Join stocklocationsname On stocklocations.SLNID = stocklocationsname.SLNID
Left Join (
Select stockhistory.StockID, Min(stockhistory.SHID)
From stockhistory
group by stockhistory.StockID ) t_h on t_h StockID = stockhistory.StockID
Left Join po On stockhistory.POID = po.POID
Left Join vender On po.Vender = vender.VendorID
Left Join equiptype On stock.EquipType = equiptype.EquipTypeID
Left Join groupname On stock.GroupNameID = groupname.GroupNameID
be sure you have also proper index on the tables and columns involved in join
Try without brackets which you write along joins and FROM.
Select stock.StockID As StockID,
stock.BinLocation As BinLocation,
stock.StockCode As StockCode,
stock.Descr As Descr,
stock.Currency As Currency,
stock.Notes As Notes,
stocklocations.Quantity As Quantity,
stocklocations.SLNID As SLNID,
stock.ConditionA As ConditionA,
stocklocationsname.SLN As SLN,
stock.DateAdded As DateAdded,
(Case When (stock.Currency = 'Euro') Then (1.05 * stock.unitprice)
When (stock.Currency = 'GBP') Then (1.24 * stock.unitprice)
When (stock.Currency = 'USD') Then (1 * stock.unitprice) End) As unitprice,
stock.DepName As DepName,
stockhistory.POID As POID,
stock.POID As POID1,
stock.PartNumber As PartNumber,
vender.VendorName As VendorName,
equiptype.EQName As EQName,
groupname.Name As Name
From stock
Join stocklocations On stock.StockID = stocklocations.StockID
Join stocklocationsname On stocklocations.SLNID = stocklocationsname.SLNID
Left Join stockhistory On stockhistory.SHID = (Select Min(stockhistory.SHID) From stockhistory Where stockhistory.StockID = stock.StockID)
Left Join po On stockhistory.POID = po.POID
Left Join vender On po.Vender = vender.VendorID
Left Join equiptype On stock.EquipType = equiptype.EquipTypeID
Left Join groupname On stock.GroupNameID = groupname.GroupNameID
Where stocklocations.Quantity > 0 And stock.Status <> 'obsolete'
how much time it takes to execute without this?
Left Join stockhistory On stockhistory.SHID = (Select Min(stockhistory.SHID)
From stockhistory Where stockhistory.StockID = stock.StockID))
If it takes 5-6 secs, then you should convert this min SHID group by StockID to a view, and join with the view first, then stockhistory itself to get the stockhistory.POID

mysql big query optimization

I'd need to optimize the following query which takes up to 10 minutes to run.
Performing the explain it seems to be running on all 350815 rows of the "table_3" table and 1 for all the others.
General rules to place indexes the propper way? Should I think about using multidimensional indexes? Where should I use them at first on the JOINS, the WHERE or the GROUP BY, if I remember right there should be a hierarchy to follow. Also If I have 1 row for all tables but one (in the row column of the explain table) how can I optimize usually my optimization consists in ending up with only one row for all columns but one.
All tables average from 100k to 1000k+ rows.
CREATE TABLE datab1.sku_performance
SELECT
table1.sku,
CONCAT(table1.sku,' ',table1.fk_container ) as sku_container,
table1.price as price,
SUM( CASE WHEN ( table1.fk_table1_status = 82
OR table1.fk_table1_status = 119
OR table1.fk_table1_status = 124
OR table1.fk_table1_status = 141
OR table1.fk_table1_status = 131) THEN 1 ELSE 0 END)
/ COUNT( DISTINCT id_catalog_school_class) as qty_returned,
SUM( CASE WHEN ( table1.fk_table1_status In (23,13,44,65,6,75,8,171,12,166))
THEN 1 ELSE 0 END)
/ COUNT( DISTINCT id_catalog_school_class) as qt,
container.id_container as container_id,
container.idden as container_idden,
container.delivery_badge,
catalog_school.id_catalog_school,
LEFT(catalog_school.flight_fair,2) as departing_country,
catalog_school.weight,
catalog_school.flight_type,
catalog_school.price,
table_3.id_table_3,
table_3.fk_catalog_brand,
MAX( LEFT( table_3.note,3 )) AS supplier,
GROUP_CONCAT( product_number, ' by ',FORMAT(catalog_school_class.quantity,0)
ORDER BY product_number ASC SEPARATOR ' + ') as supplier_prod,
Sum( distinct( catalog_school_class.purch_pri * catalog_school_class.quantity)) AS final_purch_pri,
catalog_groupp.idden as supplier_idden,
catalog_category_details.id_catalog_category,
catalog_category_details.cat1 as product_cat1,
catalog_category_details.cat2 as product_cat2,
COUNT( distinct catalog_school_class.id_catalog_school_class) as setinfo,
datab1.pageviewgrouped.pv as page_views,
Sum(distinct(catalog_school_class.purch_pri * catalog_school_class.quantity)) AS purch_pri,
container_has_table_3.position,
max( table1.created_at ) as last_order_date
FROM
table1
LEFT JOIN container
ON table1.fk_container = container.id_container
LEFT JOIN catalog_school
ON table1.sku = catalog_school.sku
LEFT JOIN table_3
ON catalog_school.fk_table_3 = table_3.id_table_3
LEFT JOIN container_has_table_3
ON table_3.id_table_3 = container_has_table_3.fk_table_3
LEFT JOIN datab1.pageviewgrouped
on table_3.id_table_3 = datab1.pageviewgrouped.url
LEFT JOIN datab1.catalog_category_details
ON datab1.catalog_category_details.id_catalog_category = table_3_has_catalog_minority.fk_catalog_category
LEFT JOIN catalog_groupp
ON table_3.fk_catalog_groupp = catalog_groupp.id_catalog_groupp
LEFT JOIN table_3_has_catalog_minority
ON table_3.id_table_3 = table_3_has_catalog_minority.fk_table_3
LEFT JOIN catalog_school_class
ON catalog_school.id_catalog_school = catalog_school_class.fk_catalog_school
WHERE
table_3.status_ok = 1
AND catalog_school.status = 'active'
AND table_3_has_catalog_minority.is_primary = '1'
GROUP BY
table1.sku,
table1.fk_container;
rows per table :
.table1 960096 to 1.3mn rows
.container 9275 to 13000 rows
.catalog_school 709970 to 1 mn rows
.table_3 709970 to 1 mn rows
.container_has_table_3 709970 to 1 mn rows
.pageviewgrouped 500000 rows
.catalog_school_class 709970 to 1 mn rows
.catalog_groupp 3000 rows
.table_3_has_catalog_minority 709970 to 1 mn rows
.catalog_category_details 659 rows
Too much to put into a single comment, so I'll add here and adjust later as possibly needed... You have LEFT JOINs everywhere, but your WHERE clause is specifically qualifying fields from the Table_3, Catalog_School and Table_3_has_catalog_minority. This by default changes them to INNER JOINs.
With respect to your where clause
WHERE
table_3.status_ok = 1
AND catalog_school.status = 'active'
AND table_3_has_catalog_minority.is_primary = '1'
Which table / column would have the smallest results based on these criteria. ex: Table_3.Status_ok = 1 might have 500k records but table_3_has_catalog_minority.is_primary may only have 65k and catalog_school.status = 'active' may have 430k.
Also, some of your columns are not qualified with the table they are coming from. Can you please confirm... such as "id_catalog_school_class" and "product_number"
SOMETIMES, changing the order of the tables, with good knowledge of the makeup of the data and in MySQL adding a "STRAIGHT_JOIN" keyword can improve performance. This was something I've had in the past working with gov't database of contracts and grants with 20+ million records and joining to about 15+ lookup tables. It went from hanging the server to getting the query finished in less than 2 hrs. Considering the amount of data I was dealing with, that was actually a good time.
AFTER dissecting this thing some, I restructured a bit more for readability, added aliases for table references and changed the order of the query and have some suggested indexes. To help the query, I tried moving the Catalog_School table to the first position and added the STRAIGHT_JOIN. The index is based on the STATUS first to match the WHERE clause, THEN I included the SKU as it is first element of the GROUP BY, then the other columns used to join to the subsequent tables. By having these columns in the index, it can qualify the joins without having to go to the raw data.
By changing the group by to the Catalog_School.SKU instead of table_1.SKU the index from catalog_school can be used to help optimize that. It is the same value since the join from the catalog_school.sku = table_1.sku. I also added index references for table_1 and table_3 that are suggestions -- again, to preemptively qualify the joins without going to the raw data pages of the tables.
I would be interested in knowing the final performance (better or worse) from your data.
TABLE INDEX ON...
catalog_school ( status, sku, fk_table_3, id_catalog_school )
table_1 ( sku, fk_container )
table_3 ( id_table_3, status_ok, fk_catalog_groupp )
SELECT STRAIGHT_JOIN
CS.sku,
CONCAT(CS.sku,' ',T1.fk_container ) as sku_container,
T1.price as price,
SUM( CASE WHEN ( T1.fk_table1_status IN ( 82, 119, 124, 141, 131)
THEN 1 ELSE 0 END)
/ COUNT( DISTINCT CSC.id_catalog_school_class) as qty_returned,
SUM( CASE WHEN ( T1.fk_table1_status In (23,13,44,65,6,75,8,171,12,166))
THEN 1 ELSE 0 END)
/ COUNT( DISTINCT CSC.id_catalog_school_class) as qt,
CS.id_catalog_school,
LEFT(CS.flight_fair,2) as departing_country,
CS.weight,
CS.flight_type,
CS.price,
T3.id_table_3,
T3.fk_catalog_brand,
MAX( LEFT( T3.note,3 )) AS supplier,
C.id_container as container_id,
C.idden as container_idden,
C.delivery_badge,
GROUP_CONCAT( product_number, ' by ',FORMAT(CSC.quantity,0)
ORDER BY product_number ASC SEPARATOR ' + ') as supplier_prod,
Sum( distinct( CSC.purch_pri * CSC.quantity)) AS final_purch_pri,
CGP.idden as supplier_idden,
CCD.id_catalog_category,
CCD.cat1 as product_cat1,
CCD.cat2 as product_cat2,
COUNT( distinct CSC.id_catalog_school_class) as setinfo,
PVG.pv as page_views,
Sum(distinct(CSC.purch_pri * CSC.quantity)) AS purch_pri,
CHT3.position,
max( T1.created_at ) as last_order_date
FROM
catalog_school CS
JOIN table1 T1
ON CS.sku = T1.sku
LEFT JOIN container C
ON T1.fk_container = C.id_container
LEFT JOIN catalog_school_class CSC
ON CS.id_catalog_school = CSC.fk_catalog_school
JOIN table_3 T3
ON CS.fk_table_3 = T3.id_table_3
JOIN table_3_has_catalog_minority T3HCM
ON T3.id_table_3 = T3HCM.fk_table_3
LEFT JOIN datab1.catalog_category_details CCD
ON T3HCM.fk_catalog_category = CCD.id_catalog_category
LEFT JOIN container_has_table_3 CHT3
ON T3.id_table_3 = CHT3.fk_table_3
LEFT JOIN datab1.pageviewgrouped PVG
on T3.id_table_3 = PVG.url
LEFT JOIN catalog_groupp CGP
ON T3.fk_catalog_groupp = CGP.id_catalog_groupp
WHERE
CS.status = 'active'
AND T3.status_ok = 1
AND T3HCM.is_primary = '1'
GROUP BY
CS.sku,
T1.fk_container;

very weird behavior on mysql query

I have two queries.
The first one is..
INSERT INTO balik
( balik_date,
balik_time,
balik_cardID,
balik_status,
balik_type )
select
current_date(),
'00:00:00',
L_CardID,
'BELUM BALIK',
L_Type
FROM
logdetail t1
LEFT JOIN balik t2
ON t1.L_CardID = t2.balik_cardID
WHERE
t1.L_Type = 'IN'
and t1.L_Date = current_date()
AND t2.balik_cardID IS NULL"
and another one is..
UPDATE balik blk
INNER JOIN logdetail ld
ON blk.balik_cardID = ld.L_CardID
and blk.balik_date = current_date()
SET
blk.balik_status = 'SUDAH BALIK',
blk.balik_type = 'OUT',
blk.balik_time = ld.L_Time
WHERE
ld.L_Type = 'OUT'
and ld.L_Date = current_date()
My problem is that when I execute the first and second query nothing happens unless I delete all records in my table beforehand. In that case both queries are executed and work like a charm.
Why is this happening?
From what I remember, you can't do an insert into a table that is a result of a query that may be an impact of what you are inserting... So, it MIGHT help if the select was "wrapped" inside another layer so the inner query is completely processed FIRST and thus not impacted by the inserts.
INSERT INTO balik
( balik_date,
balik_time,
balik_cardID,
balik_status,
balik_type )
select
PreQuery.newDate,
PreQuery.newTime,
PreQuery.newCardID,
PreQuery.newStatus,
PreQuery.newType
from
( select
current_date() newDate,
'00:00:00' newTime,
L_CardID newCardID,
'BELUM BALIK' newStatus,
L_Type newType
FROM
logdetail t1
LEFT JOIN balik t2
ON t1.L_CardID = t2.balik_cardID
WHERE
t1.L_Type = 'IN'
and t1.L_Date = current_date()
AND t2.balik_cardID IS NULL" ) PreQuery
I would alter the syntax on the update to more closely match that of the SQL Syntax per documentation... list the tables comma separated, then apply join conditions in the WHERE clause
UPDATE balik blk, logdetail ld
SET
blk.balik_status = 'SUDAH BALIK',
blk.balik_type = 'OUT',
blk.balik_time = ld.L_Time
WHERE
blk.balik_cardID = ld.L_CardID
and blk.balik_date = current_date()
and ld.L_Type = 'OUT'
and ld.L_Date = current_date()
Yeah, essentially the same thing, but might be interpretation issues.

length of mysql result

I made this query and i cant figure out why number of characters in chal_rews.rewards field on line 37 returns exactly 341 characters where there are more than 400 characters.
SELECT
GROUP_CONCAT( users.email ) AS group_emails,
GROUP_CONCAT(
CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
invitations.wined,'|',
invitations.accepted,'|',
invitations.id
)
) AS participants,
owner.id AS owner_id,
owner.email,
owner.facebook_id,
owner.access_token,
owner.first_name,
CONCAT( IFNULL(owner.first_name,owner.email),' ',IFNULL(owner.last_name,'') ) AS owner_fullname,
challenges.id AS challenge_id,
challenges.challenge_type_id,
challenges.period_id,
challenges.period_amount,
challenges.category_item_id,
challenges.metric_amount,
challenges.owner_user_id,
CASE
WHEN challenges.status=2 THEN 'complete'
WHEN ( challenges.status=1 AND challenges.end_date<NOW() ) THEN 'waiting_for_winner'
ELSE challenges.status
END AS status,
DATE_FORMAT(challenges.start_date,'%d %M %Y # %H:%i') AS start_date,
DATE_FORMAT(challenges.end_date,'%d %M %Y # %H:%i') AS end_date,
DATE_FORMAT(challenges.created_at,'%d %M %Y # %H:%i') AS created_at,
DATE_FORMAT(challenges.updated_at,'%d %M %Y # %H:%i') AS updated_at,
challenges.inform,
chal_rews.rewards,
chal_rews.reward_names AS reward_name,
category_items.item_name,
LOWER( CONCAT(fixed_verb,' ',IF(metric_amount=-1,'',CONCAT(metric_amount,' ')),fixed_metric,' ',fixed_text,' ',challenges.period_amount ,' ',periods.name) ) AS challenge_string
FROM
invitations
Left Join users ON invitations.user_id = users.id
Left Join challenges ON invitations.challenge_id = challenges.id
Left Join users AS owner ON owner.id = challenges.owner_user_id
Left Join challenge_types ON challenges.challenge_type_id = challenge_types.id
Left Join periods ON challenges.period_id = periods.id
Left Join category_items ON challenges.category_item_id = category_items.id
Left Join (
SELECT
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at,
GROUP_CONCAT(CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.access_token,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
IFNULL(chal_rews.id,' '),'|',
IFNULL(chal_rews.reward_item_id,' '),'|',
IFNULL(reward_items.name,' ')
)) AS rewards,
GROUP_CONCAT(reward_items.name) AS reward_names
FROM
chal_rews
Left Join users ON users.id = chal_rews.user_id
Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
GROUP BY
chal_rews.challenge_id
) AS chal_rews ON challenges.id = chal_rews.challenge_id
WHERE 1=1
while if i run only this part of query then rewards field returns exactly what it should i.e. full concatenated string.
SELECT
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at,
GROUP_CONCAT(CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.access_token,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
IFNULL(chal_rews.id,' '),'|',
IFNULL(chal_rews.reward_item_id,' '),'|',
IFNULL(reward_items.name,' ')
)) AS rewards,
GROUP_CONCAT(reward_items.name) AS reward_names
FROM
chal_rews
Left Join users ON users.id = chal_rews.user_id
Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
GROUP BY
chal_rews.challenge_id
I cant figure out what is wrong. SOS plz...
Bit of a punt this but it might help. If it doesn't then, well, sorry! You might get some joy by tightening up the group by clause at the end of the chal_rews sub-select. Hence:
SELECT
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at,
GROUP_CONCAT(CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.access_token,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
IFNULL(chal_rews.id,' '),'|',
IFNULL(chal_rews.reward_item_id,' '),'|',
IFNULL(reward_items.name,' ')
)) AS rewards,
GROUP_CONCAT(reward_items.name) AS reward_names
FROM
chal_rews
Left Join users ON users.id = chal_rews.user_id
Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
GROUP BY
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at
It's a little tricky to be sure without the actual tables and data in front of me but I think you will get a more consistent result in the rewards field if you do this. I think the columns that are coming back in the sub-select are inconsistent when you run it as part of the main query compared to when you run it in isolation. I suspect this is being caused by letting MySQL 'choose' a value for the chal_rews.user_id and chal_rews.reward_item_id (which in turn are used to outer join onto the users and reward_items tables - hence bringing back inconsistent data). More formally I reckon you are getting an indeterminate results set when excluding non-aggregated columns from the GROUP BY that are present in the SELECT.
Good luck and post how you get on!