Search code examples
salesforceapex

salesforce isdeleted=false slows down the query rather than improving it


I read that when the records in the recycle bin are too many to exclude those deleted from queries, you can use the condition "where isDeleted = false". But in my batch, monitoring the times, the query is much slower than the one without the explicit condition. At least the first run, then it looks faster. However, the results obtained by developer console were always exciting. Can anyone tell me why and help me, please!


Solution

  • Where you've read it? Looks very suspicious to me. isDeleted = false should have no impact on all normal queries (ones that don't have ALL ROWS at the end) because that's what they do out of the box. If anything it might even slow down the execution because query optimizer would need to consider this field (it's not indexed, it'd be useless to index something that 99% of the time has same value).

    You can experiment with Query Optimizer in the developer console and remember that typically index statistics are recalculated overnight so if you've loaded lots of test data - "today" queries might still run off old statistics.

    You might be overcomplicating it, relying on something that's one-off results because for example the server's load was low at the time you started your experiment. Or maybe whatever this was about is simply undocumented behaviour that changed with one of recent releases. Just select / create a meaningful index, you'll be better off.

    More reading material: