CRUD
SELECT

SELECT Queries - Advanced CRUD explanation part 2

intro

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 the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
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.

A SELECT Query Example.
A SELECT Query Example.

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:

Copy
        
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:

Profiling a SELECT query - first step.
Profiling a SELECT query - first step.

Once that’s done, we need to figure out the ID of the query:

The ID of the SELECT query.
The ID of the SELECT query.

And finally, we profile the query itself:

Profiling a SELECT query.
Profiling a SELECT query.

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:
Explaining a Query.
EXPLAINing a Query.
  • 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:
Wildcards in a SELECT query.
Wildcards in a SELECT query.
  • Write them like so:
No wildcard at the beginning of the search string.
No wildcard at the beginning of the search string.

Finally, make sure to employ a proper SQL client such as the one built by DbVisualizer 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 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 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 – the SQL client with the most user satisfaction. Did we tell you its premium features can be evaluated for free?

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
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, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

DELETE Queries – Advanced CRUD explanation part 4

author Lukas Vileikis tags CRUD DELETE 6 MINS 2023-06-08
title

UPDATE Queries - Advanced CRUD explanation part 3

author Lukas Vileikis tags CRUD UPDATE 7 MINS 2023-06-08
title

INSERT Queries - Advanced CRUD explanation part 1

author Lukas Vileikis tags CRUD INSERT 4 MINS 2023-06-07
title

SQL Transactions in a Flask CRUD Application

author Ochuko Onojakpor tags CRUD SQL TRANSACTIONS 8 MINS 2022-12-16
title

SQL Alias: Everything You Need to Know About AS in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2024-07-15
title

How to Compare SQL Dates

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 4 min 2024-07-11
title

OpenSearch vs. ElasticSearch: What to Choose?

author Lukas Vileikis tags Search 5 min 2024-07-08
title

REGEXP_MATCH SQL Function: The Complete PostgreSQL Guide

author Antonello Zanini tags POSTGRESQL REGEXP SQL 8 min 2024-07-04
title

Types of Databases Explained

author Lukas Vileikis tags MySQL 6 min 2024-07-01
title

MySQL ALTER TABLE Explained

author Lukas Vileikis tags MySQL SQL 7 min 2024-06-27

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗