SELECT Queries - Advanced CRUD explanation part 2
Published: 2023-09-04 07:00:00

**CRUD queries are one of the cornerstones of every database – reading (SELECT) queries help us read data and they’re absolutely crucial to any application. Figure out how they work with us!**

---

### Tools used in this tutorial
[DbVisualizer](https://www.dbvis.com/download/), top rated database management tool and SQL client

---

## Preface
In the first part of the series about CRUD queries, we have walked you through the queries that help us `INSERT` data into our database into our database instances. In this part, we’re walking you through the queries that read it – `SELECT` queries.

`SELECT` queries are necessary for any application – be it web-based or not. Most of the time when our data is being read we don’t even think about the process, but our databases are quietly churning our `SELECT` queries. Here’s what you need to know about them.

## SELECT Queries – the Basics
In many cases, a `SELECT` query looks like this: CRUD-queries are one of the cornerstones of every database, and INSERT-queries are the first ones within the list. Learn everything about them in this article.
<br />
![A SELECT Query Example.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kckk5gma0wz5k8f98itv.png)
<figure><figcaption>A SELECT Query Example.</figcaption></figure>
<br />
In most cases, `SELECT` queries also have a `WHERE` clause that specifies what exactly to search for and with the WHERE clause they look like this:

```
1 SELECT * FROM table_name WHERE column = ‘value’;

```
To dig further into them, let’s enable profiling and look into what we have. First, we run our query with profiling enabled:
<br />
![Profiling a SELECT query - first step.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b828a7l0cpstraylov2l.png)
<figure><figcaption>Profiling a SELECT query - first step</figcaption></figure>
<br />
Once that’s done, we need to figure out the ID of the query:
<br />
![The ID of the SELECT query.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g587drbpy4f3iw1bf4ot.png)
<figure><figcaption>The ID of the SELECT query.</figcaption></figure>
<br />
And finally, we profile the query itself:
<br />
![Profiling a SELECT query.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ayzuskcbrcus6f9u5i4.png)
<figure><figcaption>Profiling a SELECT query.</figcaption></figure>
<br />
The results of the profiler are all the same – those who have read the first blog of these series will remember what each of those mean, but if you don’t, we’ve also provided the same explanations below:

1. `starting` – the query starts and there’s nothing much to explain here.
2. `checking permissions` – the database checks whether there are sufficient permissions for the query to be executed. If there are not, the query stops here and provides an error.
3. `opening tables` – tables need to be open for any queries to have any effect on them.
4. `init` – the query is initializing its processes.
5. `system lock` – the database is checking whether there are any locks on the database.
6. `update` – the query updates data (in our case, inserts data.)
7. `end` – the query is closing its processes and coming to an end.
8. `query end` – the query itself stops here (end refers to the database closing processes necessary for it to run, but not the query itself.)
9. `closing tables` – the database is closing all tables that were affected by this query.
10. `cleaning up` – the database is cleaning up and preparing for the next query to be run.

Contrary to `INSERTs`, partitions and indexes help `SELECTs` be faster as they help them read through less data.

As you can probably tell, there are specific things you will need to consider when working with `SELECT` queries as well. Let’s dig into those.

## The Specifics of SELECT Queries
To speed up `SELECT` queries, follow these tips:

- **Index the columns you’re searching through** – if the columns you’re searching through (columns after a `WHERE` clause) are indexed, your `SELECT` queries will be faster due to the fact that they’ll read through less data. There’s much more to indexes, though – use ordinary (B-Tree) indexes if you don’t need any specific features from them, and if you need to use wildcards or the power of full-text search features, you might want to look into FULLTEXT indexes too. To explain indexing by itself we’d need an entire book though, so for those who are interested, we suggest you read Relational Database Index Design and The Optimizers by Tapio Lahdenmaki and Mike Leach.
- **EXPLAIN your queries** – using the EXPLAIN query will tell you whether your indexes or partitions are actually used by the database. Everything’s simple – insert an EXPLAIN keyword at the beginning of your query and you’re done:
<br />
![EXPLAINing a Query.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sgun9q3pncpqk3nqqia.png)
<figure><figcaption>EXPLAINing a Query.</figcaption></figure>
<br />

- **Make use of partitions** – the more partitions you have, the more space on the disk your table will occupy, however, the faster your `SELECT` queries will be as well.
- **Select as little data as possible** – everything’s simple. `SELECT column` will likely be much faster than selecting everything (`SELECT *`) – do you agree? Only select data that’s absolutely necessary.
- **Avoid wildcards at the beginning of a search string wherever possible** – using a wildcard will likely slow the query down a little, but using a wildcard at the beginning of a search string will tell your database “I’m searching for anything before a certain string.” If you’re searching for anything, the search will likely be slower than if you would say “I’m searching for a string that begins with ABC and then ends with something I don’t know.” That means instead of queries like this:
<br />
![Wildcards in a SELECT query.
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqvo4soqk4fxt21mq2go.png)
<figure><figcaption>Wildcards in a SELECT query.</figcaption></figure>
<br />
Write them like so:
<br />
![No wildcard at the beginning of the search string.
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndmf1oh3gnf9mav9d4um.png)
<figure><figcaption>No wildcard at the beginning of the search string.</figcaption></figure>
<br />
Finally, make sure to employ a proper [SQL client such as the one built by DbVisualizer](https://www.dbvis.com/eval/) when making use of these tips – while SQL clients themselves won’t solve the issues related to your queries, the power of SQL clients cannot be understated. Did we tell you that DbVisualizer is the one SQL client with the highest user satisfaction? It’s used by tech giants such as Netflix, Google, and Meta, as well as other companies such as Tesla and NASA (yes, the one that works on space), and if that doesn’t tell you enough, [simply have a look through its features](https://www.dbvis.com/features/) and make your own decision.

## Summary
In this blog, we have walked you through the power of SELECT queries – another necessary component in the CRUD area. These queries read through data and comprise much of the work for databases across the globe.

Now that you’ve finished with this blog, make sure to explore other blogs in these series and come back [to our blog](https://www.dbvis.com/thetable/) to learn more in the future!

## FAQs

### How do I speed up SELECT queries?

`SELECT` queries can be made faster by making them read through less data. That’s all there is to it – employ any technique (SELECT a column instead of selecting everything, employ indexes or partitions) – the result will still be the same. If your queries read through little data, they will be fast.

### Why do indexes and partitions make SELECT queries faster?

They make SELECT queries faster because they let these queries read through less data.

### Why should I use a SQL client?

SQL clients help you write SQL queries with ease and ensure your database performance, availability, and security by providing multiple features that solve the most pressing problems for developers and DBAs.

[Now, make use of DbVisualizer](https://www.dbvis.com/download/) – the SQL client with the most user satisfaction. [Did we tell you its premium features can be evaluated for free?](https://www.dbvis.com/eval/)

## About the author
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - [BreachDirectory.com](https://breachdirectory.com/), frequently speaks at conferences and blogs in multiple places including his blog over at [lukasvileikis.com](https://www.lukasvileikis.com/).