sql query doesn't return enough rows - mysql

I having trouble figuring out this SQL Query (MySql 5 is the server).
My tables are related like this:
My Query looks like this:
select
tblCustomer.customerNo,
tblSalesHeader.invoiceNo,
tblSalesHeader.orderDate,
tblSalesDetail.quantity,
tblSalesDetail.productNo,
tblSalesDetail.description,
tblSalesDetail.unitPrice
FROM
tblSalesHeader
left JOIN tblSalesDetail ON (tblSalesHeader.invoiceNo = tblSalesDetail.invoiceNo)
left JOIN tblCustomer ON (tblSalesHeader.cID = tblCustomer.cid)
left JOIN tblUsers ON (tblSalesHeader.salesmanID = tblUsers.iID)
left JOIN tblProducts ON (tblSalesDetail.productNo = tblProducts.productNumber)
WHERE
tblSalesHeader.invoiceNo=2482
GROUP BY
tblCustomer.customerNo,
tblSalesHeader.invoiceNo,
tblSalesDetail.productNo
I know that there should be 5 rows returned because there are 5 rows in tblSalesDetail where the invoiceNo=2482 like so:
select *
from
tblSalesDetail
left join tblSalesHeader on (tblSalesHeader.invoiceNo = tblSalesDetail.invoiceNo)
where
tblSalesHeader.invoiceNo=2482
I'm sure that my joins are filtering results out but I don't know why.

Get rid of the GROUP BY. You're not using it.

Related

SQL query optimization for speed

So I was working on the problem of optimizing the following query I have already optimized this to the fullest from my side can this be further optimized?
select distinct name ad_type
from dim_ad_type x where exists ( select 1
from sum_adserver_dimensions sum
left join dim_ad_tag_map on dim_ad_tag_map.id=sum.ad_tag_map_id and dim_ad_tag_map.client_id=sum.client_id
left join dim_site on dim_site.id = dim_ad_tag_map.site_id
left join dim_geo on dim_geo.id = sum.geo_id
left join dim_region on dim_region.id=dim_geo.region_id
left join dim_device_category on dim_device_category.id=sum.device_category_id
left join dim_ad_unit on dim_ad_unit.id=dim_ad_tag_map.ad_unit_id
left join dim_monetization_channel on dim_monetization_channel.id=dim_ad_tag_map.monetization_channel_id
left join dim_os on dim_os.id = sum.os_id
left join dim_ad_type on dim_ad_type.id = dim_ad_tag_map.ad_type_id
left join dim_integration_type on dim_integration_type.id = dim_ad_tag_map.integration_type_id
where sum.client_id = 50
and dim_ad_type.id=x.id
)
order by 1
Your query although joined ok, is an overall bloat. You are using the dim_ad_type table on the outside, just to make sure it exists on the inside as well. You have all those left-joins that have NO bearing on the final outcome, why are they even there. I would simplify by reversing the logic. By tracing your INNER query for the same dim_ad_type table, I find the following is the direct line. sum -> dim_ad_tag_map -> dim_ad_type. Just run that.
select distinct
dat.name Ad_Type
from
sum_adserver_dimensions sum
join dim_ad_tag_map tm
on sum.ad_tag_map_id = tm.id
and sum.client_id = tm.client_id
join dim_ad_type dat
on tm.ad_type_id = dat.id
where
sum.client_id = 50
order by
1
Your query was running ALL dim_ad_types, then finding all the sums just to find those that matched. Run it direct starting with the one client, then direct with JOINs.

SQL Using count and sum when joining 3 tables

When I try to join 2 tables and use SUM or COUNT it all works perfectly and as expected. However when I join 3 tables SUM and COUNT makes no sense because the JOIN statements create extra rows for each table to have a unique row so they over count and over sum the required values.
Here is the query:
SELECT PO_club.clubid, PO_club.name, PO_club.pic, PO_club.points, count(PO_club_user.userid), SUM(PO_club_point_log.points)
FROM PO_club
INNER JOIN PO_club_user ON PO_club.clubid = PO_club_user.clubid
LEFT JOIN PO_club_point_log ON PO_club.clubid = PO_club_point_log.clubid
WHERE PO_club.deleted = 0
GROUP BY PO_club.clubid
ORDER BY PO_club.points DESC;
If I run two separate scripts like first join only PO_club and PO_club_user to get COUNT() it works. And then run PO_club JOIN PO_club_point_log to get SUM() its all good. However, I need to run it one script so that i would not need to sort it at the front end. Is there a way to join 3 tables and somehow COUNT() just work on PO_club and PO_club_users while SUM only on PO_club and PO_club_point_log?
Thanks!
How's this? This will include all PO_clubs even if they don't have any users in case they have points. If that's not what you want, change the first LEFT JOIN to INNER JOIN.
SELECT PO_club.clubid, PO_club.name, PO_club.pic, PO_club.points, count(PO_club_user.userid), t.sum_points
FROM PO_club
LEFT JOIN PO_club_user ON PO_club.clubid = PO_club_user.clubid
LEFT JOIN
(SELECT PO_club_point_log.clubid, SUM(PO_club_point_log.points) AS sum_points
FROM PO_club_point_log INNER JOIN PO_club ON PO_club_point_log.clubid = PO_club.clubid
GROUP BY PO_club_point_log.clubid) AS t
ON PO_club.clubid = t.clubid
WHERE PO_club.deleted = 0
GROUP BY PO_club.clubid
ORDER BY PO_club.points DESC

mysql select all rows in left and let null in right table

I have two tables in MySql Database, the tables is:
upper one table is jenis_pohon and the lower one is jtt
how to select all rows in jenis_pohon and join it with jtt with condition jtt.tahun='2016' AND jtt.koperasi_id='4'
I tried the following query:
SELECT * FROM `jenis_pohon` LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id WHERE jtt.tahun='2016' AND jtt.koperasi_id='4'
but no luck.
basicaly I want to have 6 rows returned (this 6 rows is come from jenis_pohon).
I've Created Your tables and tested it, it works.
Use This query :
SELECT * FROM jenis_pohon
LEFT JOIN jtt ON (jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id AND jtt.tahun = '2016' AND jtt.koperasi_id = '4')
Notice that using AND inside join parenthesis make your query mush faster than using WHERE.
The result is :
You can sort it like :
SELECT * FROM jenis_pohon
LEFT JOIN jtt ON (jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id AND jtt.tahun = '2016' AND jtt.koperasi_id = '4')
ORDER BY jenis_pohon.jenis_pohon_id
Check this. Check here Demo
Table jtt with condition jtt.tahun='2016' AND jtt.koperasi_id='4'
it returns only 3 records because jtt.koperasi_id='4' ave only 3 records into tabel 'jtt'
SELECT * FROM `jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id
WHERE jtt.tahun='2016' AND jtt.koperasi_id='4';
O Table jtt with condition onlt jtt.tahun='2016'
SELECT distinct * FROM `jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id
WHERE jtt.tahun='2016'
Try using below Query.
SELECT
*
FROM
`jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id
AND jtt.tahun = '2016'
AND jtt.koperasi_id = '4';
LIVE DEMO
OUTPUT

Slow: Getting the results count of a query using a subquery

All I'd like to know is how many records there are in my query results but MySQL keeps kicking me off saying I have lost my connection. The query itself runs in about a second.
SELECT COUNT(*) FROM
(SELECT my208.eid AS contact, name AS the_status, cid208.lastmod AS status_date, boo208.boo_medium
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id) foo
Why is this taking so long and is there a better way?
Include the count(*) in your inner query itself. See the modified query below.
Give it a try
SELECT my208.eid AS contact,
name AS the_status,
cid208.lastmod AS status_date,
boo208.boo_medium,
COUNT(*) as total_records
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id

MySql Query takes forever

hi I am doing A query to get some product info, but there is something strange going on, the first query returns resultset fast (.1272s) but the second (note that I just added 1 column) takes forever to complete (28-35s), anyone know what is happening?
query 1
SELECT
p.partnumberp,
p.model,
p.descriptionsmall,
p.brandname,
sum(remainderint) stockint
from
inventario_dbo.inventoryindetails ind
left join purchaseorders.product p on (p.partnumberp = ind.partnumberp)
left join inventario_dbo.inventoryin ins on (ins.inventoryinid= ind.inventoryinid)
group by partnumberp, projectid
query 2
SELECT
p.partnumberp,
p.model,
p.descriptionsmall,
p.brandname,
p.descriptiondetail,
sum(remainderint) stockint
from
inventario_dbo.inventoryindetails inda
left join purchaseorders.product p on (p.partnumberp = inda.partnumberp)
left join inventario_dbo.inventoryin ins on (ins.inventoryinid= inda.inventoryinid)
group by partnumberp, projectid
You shouldn't group by some columns and then select other columns unless you use aggregate functions. Only p.partnumberp and sum(remainderint) make sense here. You're doing a huge join and select and then the results for most rows just end up getting discarded.
You can make the query much faster by doing an inner select first and then joining that to the remaining tables to get your final result for the last few columns.
The inner select should look something like this:
select p.partnumberp, projectid, sum(remainderint) stockint
from inventario_dbo.inventoryindetails ind
left join purchaseorders.product p on (p.partnumberp = ind.partnumberp)
left join inventario_dbo.inventoryin ins on (ins.inventoryinid = ind.inventoryinid)
group by partnumberp, projectid
After the join:
select T1.partnumberp, T1.projectid, p2.model, p2.descriptionsmall, p2.brandname, T1.stockint
from
(select p.partnumberp, projectid, sum(remainderint) stockint
from inventario_dbo.inventoryindetails ind
left join purchaseorders.product p on (p.partnumberp = ind.partnumberp)
left join inventario_dbo.inventoryin ins on (ins.inventoryinid = ind.inventoryinid)
group by partnumberp, projectid) T1
left join purchaseorders.product p2 on (p2.partnumberp = T1.partnumberp)
Is descriptiondetail a really large column? Sounds like it could be a lot of text compared to the other fields based on its name, so maybe it just takes a lot more time to read from disk, but if you could post the schema detail for the purchaseorders.product table or maybe the average length of that column that would help.
Otherswise I would try running the query a few times and see you consistently get the same time results. Could just be load on the database server the time you got the slower result.