Posts

Showing posts with the label T-SQL

JSON Vs. XML! The wrong choice can influence the success or the fail of your project!

Image
SQL Server Performance: JSON vs XML – Which One Is Faster? SQL Server Performance Series – JSON vs XML Hi SQL Server Guys, In performance discussions, usually a very important question came up: Should we use JSON or XML in SQL Server? At first glance this may look like a simple design decision. But in real production systems the choice between JSON and XML can have a significant impact on performance, scalability, and maintainability . In many projects, choosing the wrong format can introduce unnecessary overhead and slow down critical queries . In other words, the decision between JSON and XML can directly influence the success of the system we are building. That is exactly why this comparison is so important. Let’s look at what happens when we compare JSON and XML in SQL Server. JSON vs XML Performance Test By doing internal tests comparing JSON and XML processing, the results i got were quite interesting. When parsing and retrieving values f...

Why SELECT * Is Still Killing SQL Server Performance in 2026?

Image
Why SELECT * Is Still Killing SQL Server Performance in 2026? SQL Server Performance Series – Query Best Practices Hi SQL Server Guys, Every SQL Server developer has heard this advice at least once: Never use SELECT * in production queries. And yet, many applications still use it everywhere. At first glance, SELECT * seems convenient. It saves time when writing queries and automatically returns all columns from a table. But in real production systems, this small shortcut can create serious performance problems. Even in modern SQL Server versions, SELECT * can still negatively affect performance in several ways. 1. Unnecessary Network Cost When you use SELECT *, SQL Server returns every column in the table. But most applications do not actually need all that data. For example: SELECT * FROM Orders WHERE CustomerID = 10 If the Orders table contains 30 columns but the application only needs 3 of them, SQL Server still sends all 30 columns o...

SQL SERVER! SARGability: the One Concept You Absolutely Must Understand!

Image
Hi SQL SERVER Guys, Welcome back to this series about the most important concepts about performance! If you care about SQL Server performance, you cannot miss this post becuase there is one concept you absolutely must understand: SARGability . In a previous post I showed how dangerous some SQL queries can be for performance: The Most Dangerous SQL Server Query Today we look at one of the most important techniques to write faster queries: the SARGable way . What does SARGable mean? SARGable stands for Search ARGument ABLE . It means SQL Server can use an index efficiently to find rows. When a predicate is SARGable SQL Server can perform an: Index Seek (fast) When it is not SARGable SQL Server often performs an: Index Scan (slow) A classic NON-SARGable query SELECT * FROM Orders WHERE YEAR(OrderDate) = 2024; This query looks simple, but it hides a performance problem. Because the column is wrapped in the function YEAR() , SQL Serve...

JSON vs XML Indexing in SQL Server - The Ultimate Performance Showdown! (Benchmark Inside)

Image
JSON vs XML Indexing in SQL Server – The Ultimate Performance Showdown (Real Benchmarks Inside) SQL Server Performance Series – Advanced Indexing Deep Dive Related article in this series: SQL Server Performance Series – Engine-Level Optimization Deep Dive Hi SQL SERVER guys, today we will answer to a question that keeps coming back in modern SQL Server workloads: Should you index XML… or JSON? Which one performs better? Which one scales? Which one burns your CPU? As you usually do let’s benchmark it properly. Why This Matters Modern applications store semi-structured data everywhere: Microservices payloads Audit logs Dynamic attributes External API responses SQL Server supports both XML and JSON standards, but indexing strategies are completely different. How XML Indexing Works XML supports the following types of indexes: Primary XML Index Secondary PATH index Secondary VALUE index Secondary PROPERTY index It shreds XML into inte...

SQL Server, multiple "GROUP BY" in a single statement? The GROUPING SETS explained in a CLEAR way and with EXAMPLES!

Image
Hi Guys!  Welcome back! Today we will talk about GROUPING SETS. As usual I will try to explain what we are talking about in the clearest possible way and we will always give examples . So what is a GROUPING SET? GROUPING SETS can be considered an extension of the GROUP BY clause in fact it gives you the possibility to combine multiple GROUP BY clauses . How many times do we have to show in video a series of data that maybe adding them up and regrouping them for more than one field? How many times have we had to use UNION clauses? Often many clauses union together make the code less readable and less maintainable. The solution is to use GROUPING SETS. Enjoy the reading!   GROUPING SETS  As said, a grouping set can be considered as an extension of the GROUP BY clause. Speaking in a technical way, SQL grouping sets are a way to group data in SQL queries by multiple dimensions. Now, in a typical SQL GROUP BY clause, data is grouped by one or mor...

How to get the n-th row in a SQL table ..in 4 different ways! Simple, clear and with example!

Image
Hi guys, After the deep previous post about the SQL statistic today we will talk about a more more easy topic. Have you ever needed to extract from a table the second row, or the third or the n-th row? This post is for you!    Extract ..Get..Select... the n-th row in a table We can solve this task in more than one way. To follow my example you can simply execute the following command that create and fill the Ord table with some data Create Table Ord (Id int identity (1,1) primary key , Code varchar (10)) Insert into Ord (Code) values ( 'A' ),( 'B' ),( 'C' ),( 'D' ),( 'E' ),( 'F' ),( 'G' ),( 'H' ),( 'I' ) This is our table:   The first way I seen many times is using a derivate table .   Yes, you can solte the request by split the problem in two parts. In the first part you can get the first n-th rows. This will be the derivate table.   Then you can get first row of this derivate table orde...

SQL Server 2022 and windowing improvements. Learn SQL with me!

Image
Hi guys, In the last four posts we talked about the T-SQL news that SQL Server 2022 brought us. SQL Server 2022 and the DATE_BUCKET function. Work more easily with dates  SQL Server 2022 and the GREATEST / LEAST T-SQL commands   SQL Server 2022 and the STRING_SPLIT command .. string splitting made easy!  SQL Server 2022 and the GENERATE-SERIES command .. a T-SQL language enhancement  Today we will talk about another T-SQL extension.  We will talk about the Window clause in the window functions . As always, I will try to be as clear as possible! Enjoy this post!   The Window Clause With SQL Server 2022, Microsoft introduce in the window functions the concept of window . For the sake of truth this is not a feature invented by Microsoft because this concept is part of the ISO/IEC SQL standard. If you doesn't know what a window function is, you can read here: Learn SQL Server Window functions and Running Totals in five minutes  Looking at the example bel...

SQL Server 2022 and the GREATEST / LEAST T-SQL commands

Image
Hi Guys, Welcome back to this little new midweek post. Today we will see two more simple but useful new T-SQL commmands added in SQL Server 2022.   I usually don't like commands that aren't ANSI standard as much, but now these commands are used by more than one database engine. For example, our new GREATEST and LEAST commands are supported by PostgreSQL, MySQL and Oracle. So if they are comfortable, they are welcome! Enjoy the reading! GREATEST / LEAST commands   These new two functions returns the maximum value and then mininum value from a list of one or more expressions .   As paramters they accept a list of comma-separated expressions of any comparable data type . Furthermore these functions requires at least one argument and supports no more than 254 arguments.    For example we can write: SELECT GREATEST   ( 'Brad Pitt' , N 'Angelina Jolie' , 'Tom Hanks' ) AS VALUE; or SELECT LEAST   ( 'Brad Pitt' , N 'Angelina Jo...

SQL Server, speaking about ALIAS ...learn the rule

Image
Hello friends! Welcome back to this short post. We will speak about Alias and only two minute to learn some important things is really a big deal ! So think big and think that Share knowledge is the only way! Alias I think that everyone know what an alias is. We can have a table and call it with another name or we can have a field of a table and call it with another another name. SELECT CODE FROM ORDTES SELECT CODE as CODICE FROM ORDTES as ORDINI  Where 'as' is optional   Using alias if your statement represents a derived table then you can refer to it through the alias: SELECT * FROM ORTDES JOIN (SELECT CODE AS CODICE FROM ORDTES) AS ORDINI ON ORDINI.CODICE = ORDTES.CODE   Tips: You can see the Alias also int the execution plan   And then.. Suppose now to play with our usual ORDTES table. Suppose again you need to write a statement to get the max number of document. Using a not efficient way (i know) we could write: SELECT id FR...