This exception is caused when the SOQL query is against on object with more than 200000 records and the query is not efficient. The query takes a longer time to execute and hence the exception.
Ex: Select Name,Email from Contact
Ex: Select Name,Email from Contact where Status__c != NULL (Status__c is a custom non indexed field)
Option 1:
Make the query selective. Use Indexed fields when making the query selective, for example Id, CreatedDate, LastModifiedDate etc are indexed.
Ex: Select Name,Email from Contact where LastModifiedDate >= LAST_N_DAYS:30
To find the indexed fields, use the object manager under setup
Option 2:
If you need a custom field to be indexed, Salesforce support could add a custom index for you. Create a support case with Salesforce to get your custom index created.
When the custom index is created, make your query selective using the custom indexed field.