can someone help me make it simple and fast?
select k1.sto, k1.mm,k1.dd, k1.yy, k1.sto_name, k1.trannum,
count(k2.barcode)
from trans012020 as k1 , trans012020 as k2
where k1.barcode=123456789
and k1.mm=k2.mm
and k1.dd=k2.dd
and k1.yy=k2.yy
and k1.sto=k2.sto
and k1.trannum=k2.trannum
group by k1.trannum
having count(k2.barcode)=1;
if I run it, it should display all the details I need where count(barcode)=1
.
it displays what I need but it took 6mins to display 5 rows of data.
also it took 6mins to display me an empty data
3
Answers
So, here are my thoughts, didn’t do a POC, but from documentation
The select is evaluating everything until the group by, this mean is grouping everything in your database regardless of the having condition.
my suggestion is for you to try to do it like
Hopefully that gets you the desire result
Here is some documentation on how these are evaluated
Using
is a convient way to join where the column names are identical. Asusing
andon
are incompatible options I’ve put thebarcode
criteria inwhere
and because its an inner join this has the same effect as a join criteria.MySQL allows a count alias like
k2count
to be used later. So a partial simplification is:Some more work here is required as if you are grouping by
trannum
, assuming that isn’t the primary key, whichk1.{sto,mm,dd,yy,sto_name}
fields do you expect it to display. (see Don’t disable only_full_group_by.Correct indexing will help with the query. See Rick’s ROT, indexing, or add the
show create table trans012020
into your question.Keep in mind how
JOIN
andGROUP BY
work together. First theJOINs
are done. This explodes into a big temp table. Second theGROUP BY
shrinks id down to essentially what you started with. Let’s avoid that "inflate-deflate":If
barcode
isNOT NULL
, changecount(k2.barcode)
to simplyCOUNT(*)
.k1 needs an
INDEX
(or thePRIMARY KEY)
beginning withbarcode
.If k2count can never be more than 1, then there is an even better way:
And, yes, this is desirable for
k2
:(The order of the columns is not important for this query.)
Note that the
GROUP BY
went away.