我有两个表结构如下:(系统中每个对象类型的多个ai_ $对象表)
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)
答案 0 :(得分: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');