Skip to main content

Why not use Select * in SQL Server

 
Select *

We often use the Select * to fetch data from tables of SQL Server.

Today, we will see why we should avoid Select *


Figure: Imagine yourself handcuffed while writing Select *

I will be using AdventureWorks2019 Database for demonstration.

First, we do the following -

Set Statistics IO ON;

This will output us how many 8 KB pages SQL server read for our request.

Let's use Sales.SalesOrderDetail table. We use the following query : 

Select * 

from Sales.SalesOrderDetail

set statistics IO ON

set statistics IO ON output


We get the result tab and Message tab. If we look into the Message tab, we get -

(121317 rows affected)

Table 'SalesOrderDetail'. Scan count 1, logical reads 1248, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.

Ok! SQL Server had to read 1248 - 8 KB pages. you might know, there are 500 pages in a ream of paper. so SQL server had to go through almost 2.5 reams of paper!

Now, Let's modify our query to select 2 columns

select SalesOrderDetailID, SalesOrderDetailID

from Sales.SalesOrderDetail 

Similarly, We get the result tab and Message tab. If we look into the Message tab, we get -

(121317 rows affected)

Table 'SalesOrderDetail'. Scan count 1, logical reads 276, physical reads 1, page server reads 0, read-ahead reads 288, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.



Now, SQL server had to read 276 - 8KB pages, only about half a ream of paper.

Definitely, that's a relief.

Can I use Select * for tables with few columns?

 If you need all the columns, select each of the column names instead, Why?

Let's say you have 3 columns on your table, so you decided to Select * instead of writing the names of the column. 2-3 years later, you find out, all other developers have added some columns to the table and the table contains 10/12 columns, and your Select * query that you wrote 3 years ago, is paying off heavily on production server now. So, stay away from the mistake you will probably regret 3 years later.

What can happen if I use Select *

Select * can lead to more processing effort to SQL Server and in case of parallel plan execution enabled, you may face Threadpool - a deadly poison wait for SQL Server

No worries, to get rid of this deadly wait, all you need is setting your MaxDop and Cost threshhold for parallelism Properly 

Remember, the best way to deal with your SQL Server is to think like SQL Server and using the best configuration for your System.


Comments

  1. Very informative and easy to CATCH!
    Keep it up sir :)
    Jazakallah :)

    ReplyDelete
    Replies
    1. I am pleased that you liked it. Stay tuned for more of my SQL Server Pain Reliefs !

      Delete

Post a Comment

Most Loved Posts

How to encrypt an entire excel file using AES-128 encryption

What Is AES? AES stands for Advanced Encryption Standard. It is a symmetric block cipher that is used by the U.S. government to protect classified information. AES is used worldwide to protect classified data around the world. AES is essential in cybersecurity, electronic data protection, and computer security. Variations of AES AES is used in three block cipher versions namely AES 128, AES 192, AES 256. AES 128 uses 128-bit key length to encrypt and decrypt block messages. This is a symmetric secret key which means it uses same secret key for encrypting and decrypting message blocks. AES Encryption of Excel File Today we will AES 128 to encrypt an Excel File. The excel file looks like the following image. To encrypt this excel file, we will use C#. First, we need to create a console project. Let's name the project ExcelEncryption. First, we try to understand what we need to achieve. We need a KEY and Initialization Vector (IV) pair for AES 128 encryption. Let’s say, th...

SQL Insider 01 : An Anatomy of SELECT

Introduction When we write queries, we tend to think about the internals very little. In the new series of SQL Insider, I shall try to demonstrate what your SQL Server has to go through when you write a specific query, more specifically a specific operator. In the series, we shall try to cover all the important operators in SQL. Our today's SQL participant in SELECT. SELECT  With the SELECT query, we can select one, some, or all the columns of a SQL table. The typical syntax for SELECT is like this  SELECT * FROM Sales.SalesOrderDetail SELECT sod.OrderQty, sod.UnitPrice FROM Sales.SalesOrderDetail sod Please note that we will not be dealing with WHERE clause in today's episode.  Database We will be using AdventureWorks2019 Database for the demonstration Important Configuration We will be setting STATISTICS IO ON like this - SET STATISTICS IO ON; SET STATISTICS IO ON ; We will turn on Actual Execution Plan to examine the query SQL Insider Let's sta...

How to Backup SQL server like Batman: The Ultimate SQL DBA Guide

How to deal with Slow SQL Server due to Autogrowth issue

  Why you should not stick to SQL Server’s default Initial file size and autogrowth We hear a lot of these statements : My SQL Server is running slow My Production DB was fine when we started, But it is staggeringly slow now My Business end users are frustrated to wait too long Well, there are lots of reasons why your SQL Server might be slow. Setting the Autogrowth option to default is definitely one of the vital ones which we seem to ignore most of the time. Slow SQL Server and Tortoise SQL Server provides you with some default settings for autogrowth when you install it for the first time. These default cases are defined with increment by 8MB or by 10%. You need to change it to suit your own needs. For Small application, this default value might work but as soon as your system grows, you feel the impact of it more often. What Happens SQL Server Files needs more space SQL Server Requests the Server PC for more space The Server PC takes the request and asks the SQL request...

6 Letters for SQL Disaster Emergencies : RPO and RTO