Tom Mitchell Profile picture
Aug 26 9 tweets 2 min read Read on X
5 steps to write SQL queries that actually perform well:
1. Start with your WHERE clause

Why?

Because filtering early reduces the dataset size before expensive
operations like JOINs and GROUP BYs kick in.

Plan your filters first, then build around them.
2. Use Indexes

They help database find specific information much faster without having to search everything.

Pro tip: Composite indexes give you a huge benefit.

If you're regularly filtering on (date, status, region), create one index on all three columns in that order.
3. Don't use SELECT *

SELECT * pulls every column, even ones you don't need.

More data = slower queries.
More data = higher memory usage.

Only select what you actually use.
4. JOINs have an order, and it matters

Start with your smallest table.

Join to progressively larger tables.

Think of it like building a foundation:

- Small table = solid foundation.
- Large table joined to small result = manageable.
- Large table joined to large table = performance nightmare.

Also: INNER JOINs before LEFT JOINs when possible to filter results early.
5. Test with increasing table sizes

Your query runs fast on 1,000 rows?

Great.

Will it run fast on 10 million rows?

Probably not.

Always test with production-sized datasets and split up large queries into supporting tables if needed.
TL;DR:

- Write WHERE clauses first to filter early
- Index every column you filter, join, or sort on
- Select only columns you need (never SELECT *)
- Join smallest tables first, then build up
- Test progressively
I hope you've found this thread helpful.

If you did, I ask for 2 small favours:

1. Follow me @imtommitchell for more like this daily.

2. Click below, jump to the top and share to help someone else.
P.S. If you're interested in data you'll love my weekly newsletter.

I share everything I know about building high-paying data skills from my 8+ years in the industry.

Subscribe for free here:

thedatadose.com

• • •

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

Keep Current with Tom Mitchell

Tom Mitchell 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!

More from @imtommitchell

Aug 25
If you want to work in data, you should know CI/CD.

Here's a dead simple breakdown that'll teach you fast:
What is CI/CD?

Continuous Integration (CI) + Continuous Deployment (CD).

In simple terms: automatically testing and deploying your data pipelines without breaking things.

Quality control for your code.
Why data teams need it:

Your SQL query works on your laptop.

But will it work in production?
Will it break when your teammate pushes code?
Will bad data crash your entire pipeline?

CI/CD prevents these disasters.

The 4 core components:
Read 12 tweets
Apr 30
A Data Analyst who understands segmentation will never be short of work.

Problem is, it's not covered often in courses.

Here's everything I know about customer segmentation condensed:
Customer segmentation helps businesses understand their customers better.

It allows them to tailor marketing strategies, product offerings, and customer experiences to meet the specific needs of each segment.

A segment is a group of customers that share characteristics.

You can segment based on factors like age, gender, location, income, and interests.

Here are some examples of different types of segmentation:
Demographic Segmentation divides customers based on characteristics like age, gender, education, and income.

Behavioural Segmentation categorises customers based on their interactions with your products or services.

Are they loyal, occasional buyers, or inactive customers?

Psychographic Segmentation looks at customer attitudes, values, and lifestyles.
Read 7 tweets
Mar 31
Learn SQL in 10 steps (the simple way):
1. The basics are your best mates.

From SELECT to WHERE, get familiar with them.

They’ll make the advanced stuff much easier to grasp.

Don't go too deep into windows functions etc yet. Plenty of time for that later.
2. Coding is an art.

It takes time and consistency to create masterpieces.

Don't rush it.

Look at how other professionals structure their code.

Readability, maintainability, efficiency is the aim of the game.
Read 12 tweets
Mar 26
To do Data Analysis using Python you must master Pandas.

But this library contains a lot.

Here is what you need to focus on from day 1 👇
Pandas is an open-source Python library built on top of a Python core packages called NumPy (Numerical Python).

Pandas offers Data Analysts an easy way to work with data and provides many tools for extracting maximum value.

Let's get into it...
There are two main concepts in Pandas:

A series and a dataframe.

A Series is a Pandas array that can hold any type of data.

It is a one-dimensional array or a single column of a matrix.

A series is a set of data values that are associated with a specific label, with specific index values attached to each row.
Read 12 tweets
Jan 30
Nobody showed me how to create a data analysis portfolio.

I was lucky to land my first job.

If I were to start again, I'd create one using this 5-step plan: 👇
Step 1: Pick a subject that you are passionate about and enjoy.

You must have a genuine interest in the topic you are researching.

This will allow your mind to wander and be inquisitive, a key element in data analysis.
Step 2: Find some data and come up with questions to answer.

There are many amazing resources for finding data on the internet.

Two of my favourites are:

- Kaggle
- Google Datasets

Start to explore the data and come up with some questions.

If you struggle, ask ChatGPT.
Read 8 tweets
Jan 19
Pivot tables explained in simple terms:
Pivot tables are a way to interactively group, filter, and interrogate large amounts of data.

You might be thinking:

"But Tom. I can do all this in Excel anyway?"

But here's why they're so powerful...
Excel has a maximum row limit of 1.05m.

That means any dataset larger than that will have parts cut off.

A big problem if you're looking to produce accurate and reliable data insights.
Read 12 tweets

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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(