我在php(替换列)中为cicle生成了这个查询:
select count(`a`) as `countAdd`, `a` as `asd` from `question` where ( (`a` = 1 and ((`v` is not null and `b` not in (1,2)) or (`v` is null and `b` not in (6,7))) or (`a` = 2 and ((`v` is not null and `b` not in (2,3)) or (`v` is null and `b` not in (8,9))) or (`a` = 3 and ((`v` is not null and `b` not in (4,5)) or (`v` is null and `b` not in (8,4))) or (`a` = 4 and ((`v` is not null and `b` not in (4,9)) or (`v` is null and `b` not in (6,2))) or (`a` = 5 and ((`v` is not null and `b` not in (2,6)) or (`v` is null and `b` not in (3,4))) ) and `deleted_at` is null group by `addresseeId`
在php中
foreach ($a as $v) $query .= '(`a` = ? and ((`b` is not null and `c` not in (?)) or (`a` is null `b` not in (?)))';
当我处理更大的数据时,如何避免长时间的查询?
答案 0 :(得分:0)
由于这个问题没有那么多非技术背景,我会给你一般性的建议。
有时候获取所需的所有数据然后在php本身进行排序是有意义的。您正在构建的查询可能会变得非常大,因为我会避免在循环中构建查询。
您的问题的另一个可能的解决方案可能是重新设计您的数据库。如果您在某些时候注意到您的查询变得不合适,那么您的数据库设计可能存在一些问题。检查this link以获取数据库设计(感谢@castis)