1
I have the following structure:
And my query is that way
SELECT tbl_nfes.*, count(tbl_nfe_itens.pedido > 0) as v, tbl_nfe_itens.status FROM
tbl_nfes
INNER JOIN tbl_nfe_itens ON tbl_nfes.nfe = tbl_nfe_itens.nfe
INNER JOIN tbl_users ON tbl_nfes.id_user = tbl_users.id
WHERE (SELECT COUNT(*) FROM tbl_nfe_itens WHERE position = 0 AND nfe = tbl_nfe_itens.nfe ) = 0
GROUP BY tbl_nfes.nfe ORDER BY tbl_nfes.nfe ASC
Removing select inside Where the query does not delay in showing the result, but loads data that are not accepted, could only return the Nfes that all items have position = 0
. What I must do to make this consultation more effective?
The standard that this way it brings the result I wish that eh show only the nfs where all your items have
position = 0
I’ve tried to putwhere tbl_nfe_itens.position = 0
and it carries the nfs that have at least one item withposition = 0
, has another way to load only if all items haveposition = 0
?– WMomesso
Sorry missed a part of Where already edited, missed `AND nfe = tbl_nfe_items.nfe
– WMomesso
Yes it was time to transcribe the query, but I’ve already put the
,
– WMomesso
Execute that
query
to create an Index and test your query againCREATE INDEX tbl_nfe_itens_nfe ON tbl_nfe_itens(nfe);
– Sorack
Perfect worked great, I’m trying to find some material for me to understand how the indices work. thank you very much.
– WMomesso
Show @Sorak thanks for the availability and if you can post the answer will help many people and I could understand better.
– WMomesso
Beware of subquery in Where. Note that the subquery will be executed for each line found. Imagine the impact on a search with a source of millions of records. Creating index is an outline that can relieve you momentarily. Use sparingly.
– Badaro