🧵 Today I decided to tackle a performance issue in our #MySQL Database.

We use timestamp columns for storing states (is paid, is invoiced, is shipped etc.) and clients would like to be able to filter by these states (see screenshot)

Thread below👇🏻
Despite having indexes on all 4 of our timestamp columns, this query can become incredibly slow as data grows. Screenshot from first tweet is an example on 1 million records.

In order to optimize this, we can add a indexed boolean state column that concats all the timestamps.
Notice the leading "1", this is simply to ensure no leading zeroes are stripped. Using a string column to combat this would result in worse performance, as querying integers is much faster.

This change means we can now query by the state column instead of the timestamp column.
What I am seeing is reducing query time by almost 90% (this will vary a lot depending on context and data)

What if I dont want to query by all the columns? Maybe ignore the shipped_at column?

This is where things get slightly tricky:
With a normal query, you just remove the "AND shipped_at IS NOT NULL". Done.

For this, you'd need to change your query to use "WHERE IN" and include both variations.

It would be fairly straightforward to add a scope to your #Laravel model to simplify usage.
Note: This mostly affects COUNT(*) queries (which are called if you use default Laravel Pagination, or any page based pagination really) The main select query will be same-is performance for both cases and wont gain same benefits.
🚨HOWEVER🚨

If you use LIMIT, the new state-based query will be faster again.

SELECT * ... LIMIT:
Timestamp based query: ~60ms🐢
State based query: ~20ms🚀

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Lasse Rafn

Lasse Rafn Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(