请帮助我.. 我有一个如下的查询: 这是一个表:
我想要显示在delivery_order/item表中存在但在bst/item表中不存在的商品,根据KODE_BARANG和JUMLAH进行比较。
我有下面的查询语句,但是如果数据已经很多的话,它仍然很慢(慢的部分是在比较KODE_BARANG的行)。 我想问一下,我的朋友们是否有类似的情况,并且如何解决? 提前谢谢,希望这里有人愿意回答。
SELECT del.KODE_DO, deli.KODE_BARANG, deli.NAMA_BARANG, deli.JUMLAH, deli.SATUAN, @DITERIMA := COALESCE (( SELECT SUM( JUMLAH ) FROM bst_item WHERE KODE_PENERIMAAN = deli.KODE_DO AND KODE_BARANG = deli.KODE_BARANG #这行使它变慢 AND `STATUS` <> 0),0) AS DITERIMA, COALESCE ( deli.JUMLAH, 0 ) - @DITERIMA AS SISA FROM delivery_order del INNER JOIN delivery_order_item deli ON del.KODE_DO = deli.KODE_DO WHERE DATE(del.TANGGAL) >= :TGL1 AND DATE(del.TANGGAL) <= :TGL2 AND COALESCE ( deli.JUMLAH, 0 ) - COALESCE (( SELECT SUM( JUMLAH ) FROM bst_item WHERE KODE_PENERIMAAN = deli.KODE_DO AND KODE_BARANG = deli.KODE_BARANG #这行使它变慢 AND `STATUS` <> 0),0) > 0
SELECT del.KODE_DO, deli.KODE_BARANG, deli.NAMA_BARANG, deli.JUMLAH, deli.SATUAN, @DITERIMA := COALESCE (( SELECT SUM( JUMLAH ) FROM bst_item WHERE KODE_PENERIMAAN = deli.KODE_DO AND KODE_BARANG = deli.KODE_BARANG #这行使它变慢 AND `STATUS` <> 0),0) AS DITERIMA, COALESCE ( deli.JUMLAH, 0 ) - @DITERIMA AS SISA FROM delivery_order del INNER JOIN delivery_order_item deli ON del.KODE_DO = deli.KODE_DO WHERE DATE(del.TANGGAL) >= :TGL1 AND DATE(del.TANGGAL) <= :TGL2 AND COALESCE ( deli.JUMLAH, 0 ) - COALESCE (( SELECT SUM( JUMLAH ) FROM bst_item WHERE KODE_PENERIMAAN = deli.KODE_DO AND KODE_BARANG = deli.KODE_BARANG #这行使它变慢 AND `STATUS` <> 0),0) > 0
P粉6525239802023-09-14 14:59:16
INDEX(KODE_PENERIMAAN, KODE_BARANG, `STATUS`)
而且不要在函数调用中隐藏TANGGAL。 (cf "sargable")