0

我有两个表结构如下:(系统中每个对象类型的多个 ai_$object 表)

       ai_$object         ,       ai_ext_ids
--------------------        -------------------------- 
ext_id | hash                    id | ext_id | object_name | sfID

从批量删除中我得到了 sf_ids,并且需要从这两个表中删除记录,这种删除的最愚蠢的方式是这样的:

在伪代码中-->

$idToBeDeleted = SELECT ext_id FROM ai_tr_tbl_extids WHERE objectName='$object' AND sfID ='$s'" ;

DELETE FROM ai_tr_tbl_extids WHERE objectName='$object' AND sfID ='$s';

DELETE FROM ai_$object WHERE ext_id='$idToBeDeleted';

我的问题是如何提高这部分的性能?这段代码在循环中运行,我是否应该使用数组进行批量删除:SELECT * FROM table WHERE ID IN (1,2,3,4)

4

1 回答 1

0

objectName 列应该被索引以获得更好的性能。并尝试以下查询。

$idToBeDeleted = SELECT ext_id FROM ai_tr_tbl_extids WHERE objectName='$object' AND sfID ='$s'" ;

DELETE FROM ai_tr_tbl_extids WHERE ext_id='$idToBeDeleted';

DELETE FROM ai_$object WHERE ext_id='$idToBeDeleted';

但是,如果您能够获得要删除的列表,ext_id则可以删除循环并使用 删除IN

DELETE FROM ai_tr_tbl_extids WHERE ext_id IN ($idToBeDeletedCommaSeparatedValue');

DELETE FROM ai_$object WHERE ext_id IN ($idToBeDeletedCommaSeparatedValue');
于 2013-09-13T15:49:16.743 回答