How does database query optimization work?

Technical