Question: I have a soql query inside my apex class : 

```
SELECT Id, Name, account.name, Special_food_requirements__c,Passport_number__c,account.Relationship__c,Function__c
                     FROM Contact
                     WHERE accountId IN :selectedAccountIds
                     AND Id Not IN(SELECT contact__c FROM Visit_contact__c WHERE Visit__c =:visitId)
                     order by account.Name ASC,Name ASC limit 1000

```

Soumetimes, for some users I get timeout. 

How can I fix it ? Thank you in advance !

Answer: 
Learn about and use the [Query Plan Tool](https://help.salesforce.com/apex/HTViewSolution?id=000199003) to find out what is going wrong and then work on a fix based on that knowledge.
Avoid NOT IN for sure, but you can also set the Timeout depending on what language you are using. This is how in C# for example.

```
private static SforceService binding;

binding.QueryOptionsValue = new QueryOptions();
binding.QueryOptionsValue.batchSize = 2000;
binding.QueryOptionsValue.batchSizeSpecified = true;
binding.Timeout = 180000;

```