Chapter 4
Perform Fast Queries with Advanced Postgres Indexes

Our users can now securely log in to Shine using a well-designed login form, which was a great way to get a taste of what Postgres and Bootstrap can offer. In the next few chapters, you’ll implement a customer search feature, digging deeper into Postgres and Bootstrap. You’ll also start learning Angular in Chapter 6, Build a Dynamic UI with Angular.

In this chapter, you’ll implement the basics of our search, which will perform poorly. This allows you to learn about the advanced indexing features of Postgres that will speed this search up without changing any code or setting up new infrastructure. You’ll also learn how to understand query performance in Postgres, so you can be sure that the indexes you create have the performance improvements you want.

Because this is the first bit of code you’re writing for this feature, you’ll also need some UI and middleware logic. Although this chapter is mostly about Postgres, you’ll be working in all parts of the stack. Specifically, you’ll write the basics of our search logic in Rails, and you’ll learn how to style forms in Bootstrap using input groups, as well as how Bootstrap styles tables. In Chapter 5, Create Clean Search Results with Bootstrap Components, you’ll spend more time on the search results themselves.

First, you’ll implement a naive fuzzy search that allows our users to locate customers based on first name, last name, or email. It’ll look something like the figure.

images/naive-search-mockup.png

After that, you’ll look at the performance of Active Record’s SQL query using Postgres’s query plan. You’ll then use a special type of index on our tables to speed up our search, which you’ll then verify by reexamining the new query plan. This all might feel really low-level, but you’ll be surprised just how easy it is to get great performance out of Postgres with just a few lines of code.

Let’s get to it by implementing a naive version of the search.