Sooner or later you run into situation, when it is no more possible to improve performance of your beloved database-interacting project only by changing some postgresql.conf settings. It is time, when you need to optimize some SQL-queries, and such optimization can turn out to be not a last-mile problem, but a long difficult journey. How to choose queries to optimize? OK, I know about explain, but how to interpret its output? How to improve bad queries? How can that affect overall performance? In this tutorial we will guide you through the answers. We will learn how to pick up queries to optimize, will scrutinize their explains and optimize them. Lots of standard techniques, from simple indexing to advanced “hacking” with combinations of techniques such as loose index scan, will be covered. Besides, we will give some tips, how to check your queries before they can cause performance problems.