Domanda

can i minimize or shorter (if ever) this query? this query takes too long to load, how can I shorten the execution of this query? thanks.

this is my sql query:

$sql = "
  SELECT
    i.ID
  FROM item_tb i
  WHERE
    i.coID = '". $_SESSION['coID'] ."'
    AND i.isProduct = '1'
    AND i.isBom = '0'
    AND NOT EXISTS(
      SELECT
        s.ID
      FROM
        stocks_tb s
      WHERE
        i.ID = s.itemID
        AND s.brID = '". $brID ."'
   )
";
È stato utile?

Soluzione

I guess that your query converted LEFT JOIN as follows:

SELECT
  i.ID
FROM item_tb i LEFT JOIN stocks_tb s ON i.ID = s.itemID
WHERE
  i.coID = '". $_SESSION['coID'] ."'
  AND i.isProduct = '1'
  AND i.isBom = '0'
  AND s.brID = '".$brId."'
  AND s.itemID IS NULL;

and this is faster than your query.

Altri suggerimenti

You use many AND operator... Use bracket within the AND condition.. That's it

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top