Explain how database indexing works and how you would apply it to optimize queries that filter by specific criteria.